哈马德矩阵code [英] Hadamard matrix code
本文介绍了哈马德矩阵code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图构建尺寸N * n的阿达玛矩阵,然后打印出来。 code编译但是当我运行它,它甚至没有获取到部分要求N个输入。任何想法有什么错呢?
的#include<&stdio.h中GT;无效的主要(无效){INT I,N;scanf函数(输入N值数:%d,&安培; N);焦炭** H =(字符**)释放calloc(N,sizeof的(字符*));对于(i = 0; I< N;我++){
H [i] =(字符*)释放calloc(N,的sizeof(字符));
}INT二,XX,YY;H [0] [0] ='1';
用于(ⅱ= 2;ⅱ&下; = N;ⅱ* = 2){
//右上象限。
为(XX = 0; XX≤(二/ 2); ++ XX){
对于(YY =(II / 2); YY<二; ++ YY){
H [XX] [YY] = H [XX] YY-(II / 2)];
}
}
//左下象限。
为(YY = 0; YY≤(二/ 2); ++日){
对于(XX =(II / 2); XX<二; ++ XX){
H [XX] [YY] = H [XX-(II / 2)] [YY]
}
}
//右下象限,其它象限的倒数。
对于(XX =(II / 2); XX<二; ++ XX){
对于(YY =(II / 2); YY<二; ++ YY){
H [XX] [YY] = H [XX-(II / 2)] [YY-(II / 2)];
如果(H [xx]的[YY] =='1'){
H [XX] [YY] ='0';
}
其他{
H [XX] [YY] ='1';
}
}
}
}
//打印矩阵。
对于(XX = 0; XX * LT; N ++ XX){
对于(YY = 0; YY< N ++ YY){
的printf(%C,H [XX] [YY]);
}
的printf(\\ n);
}
}
解决方案
scanf函数
不打印字符串,这只是用来检查输入的格式。
尝试:
的printf(输入N值:);
scanf函数(%d个,&安培; N);
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("\n");
}
}
解决方案
scanf
doesn't print that string, that's just used to check the format of the input.
Try:
printf("Input N value: ");
scanf("%d", &N);
这篇关于哈马德矩阵code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文