Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
767 views
in Technique[技术] by (71.8m points)

c - Hadamard matrix code

I'm trying to construct a hadamard matrix of dimensions N*N, and then print it. Code compiles but when I run it it doesn't even get to the part asking for the N input. Any ideas what is wrong with it?

#include <stdio.h>

void main(void) {

int i, N;

scanf("Input N value:   %d", &N);

char **h = (char**) calloc(N, sizeof(char*));

for ( i = 0; i < N; i++ ) {
    h[i] = (char*) calloc(N, sizeof(char));
}

int ii, xx, yy;

h[0][0]='1';


for(ii=2; ii<=N; ii*=2) {
    //Top right quadrant.
    for(xx=0; xx<(ii/2); ++xx) {
        for(yy=(ii/2); yy<ii; ++yy){
            h[xx][yy]=h[xx]yy-(ii/2)];                          
        }
    }
    //Bottom left quadrant.
    for(yy=0; yy<(ii/2); ++yy) {
        for(xx=(ii/2); xx<ii; ++xx) {
            h[xx][yy]=h[xx-(ii/2)][yy];
        }
    }
    //Bottom right quadrant, inverse of other quadrants.
    for(xx=(ii/2); xx<ii; ++xx) {
        for(yy=(ii/2); yy<ii; ++yy) {
            h[xx][yy]=h[xx-(ii/2)][yy-(ii/2)];
            if(h[xx][yy]=='1') {
                h[xx][yy]='0';
            }
            else {
                h[xx][yy]='1';
            }
        }
    }
}


//Printing matrix.
for(xx=0; xx<N; ++xx) {
    for(yy=0; yy<N; ++yy) {
        printf("%c",h[xx][yy]); 
    }
    printf("
");
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

scanf does NOT put out a prompt message such as "Input N value: "
Nothing in the documentation of scanf suggests that it does.

Instead you want:

printf("Input N value: ");
scanf("%d", &N);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...