而在C中正常化二维数组不正确的值 [英] Incorrect values while normalizing 2d array in C
本文介绍了而在C中正常化二维数组不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图根据正常化的一对夫妇给定方程的二维数组。该阵列已经被填充值,使用rand()函数从0至255不等。的问题是,我们我正常化阵列,使用几个给定方程组,该归一化值可以是0或者255。
的#include< stdio.h中>
#包括< stdlib.h中>
#包括< time.h中>
主要 ()
{
INT X,Y;
函数srand(时间(NULL));
INT I,J = 0;
INT最大= NULL;
INT分钟= 255;
长双D;
长双E;
的printf(你好用户\ñ。);
的printf(请输入数组的水平尺寸\ñ。);
scanf函数(%D,与放大器,X);
的printf(请输入阵列的垂直尺寸\ñ。);
scanf函数(%D,与安培; Y);
INT MYARRAY [X] [Y]
浮MyArray1 [X] [Y]
INT *点=(INT *)malloc的(X * Y *的sizeof(INT));
浮动*点1 =(浮动*)malloc的(X * Y *的sizeof(浮动));
对于(I = 0; I&其中; =(X-1);我+ +)
{
为(J = 0; J&其中; =(Y-1); J ++)
{
MYARRAY [I] [J] =(INT)(RAND()%256);
的printf(int是:\ N);
的printf(\ t%D \ N,MYARRAY [I] [J]);
如果(MYARRAY [I] [J] GT;最大)
{
最大= MYARRAY [I] [J]。
}
如果(MYARRAY [I] [J] LT;分)
{
分= MYARRAY [I] [J]。
}
}
}
对于(I = 0; I&其中; =(X-1);我+ +)
{
为(J = 0; J&其中; =(Y-1); J ++)
{
D =(MYARRAY [I] [J] - 分钟);
的printf(d为:%D \ N,D);
E =(D /(最大值 - 最小值));
E =(E * 255);
的printf(e为:%F \ N,E);
MyArray1 [I] [J] = E;
}
}
的printf(最大的是:%D \ N,最大值);
的printf(最小为:%D \ N,分);
免费(点);
免费(点1);
}
解决方案
我可以在你的code发现问题,
-
INT最大= NULL;
不推荐使用。用一个简单的0
代替。 - 请不要投的结果
的malloc()
。 - 什么是具有的的点的
点
和点1
在这里?你可以删除它们。 -
的printf(d为:%D \ N,D);
是错误的。你需要使用%LF
为长双
- 的最后的
的printf(e为:%F \ N,E);
也是错误的。您需要使用正确的转换说明。使用%LF
为长双
。
这是说,推荐的签名的main()
是 INT主要(无效)
i am trying to normalize an 2D array based on a couple of given equations. The array has being filled with values ranging from 0-255 using the rand() function. The problem is that we i normalize the array, using a couple of given equations, the normalized value is either 0 or 255.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main ()
{
int x,y;
srand(time(NULL));
int i,j = 0;
int max = NULL;
int min = 255;
long double d;
long double e;
printf("Hello user.\n");
printf("Please enter the horizontal dimension of the array.\n");
scanf("%d", &x);
printf("Please enter the vertical dimension of the array.\n");
scanf("%d", &y);
int MyArray[x][y];
float MyArray1[x][y];
int *point = (int *) malloc(x * y * sizeof(int));
float *point1 = (float *) malloc(x * y * sizeof(float));
for (i = 0; i <= (x-1); i++)
{
for (j = 0; j <= (y-1); j++)
{
MyArray[i][j] = (int) (rand() % 256);
printf("the int is:\n");
printf("\t %d\n", MyArray[i][j]);
if (MyArray[i][j] > max )
{
max = MyArray[i][j];
}
if (MyArray[i][j] < min)
{
min = MyArray[i][j];
}
}
}
for (i = 0; i <= (x-1); i++)
{
for (j = 0; j <= (y-1); j++)
{
d = (MyArray[i][j] - min);
printf("d is: %d\n", d);
e =( d / (max - min));
e = (e * 255);
printf ("e is: %f\n", e);
MyArray1[i][j] = e;
}
}
printf("the max is: %d\n", max);
printf("the min is: %d\n", min);
free(point);
free(point1);
}
解决方案
The issue I can find in your code,
int max = NULL;
is not recommended. use a simple0
instead.- Please do not cast the result of
malloc()
. - What is the point of having
point
andpoint1
here? You can remove them. printf("d is: %d\n", d);
is wrong. You need to use%Lf
for along double
- Finally,
printf ("e is: %f\n", e);
is also wrong. You need to use the correct conversion specifier. use%Lf
forlong double
.
That said, the recommended signature of main()
is int main(void)
这篇关于而在C中正常化二维数组不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文