我试图基于几个给定的方程来规范化二维数组。使用rand()函数将数组填充为0-255之间的值。问题是我们使用两个给定方程对数组进行归一化,归一化值为0或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);
}
我可以在您的代码中找到的问题,
int max = NULL;
不推荐。使用简单的0
代替。malloc()
。point
与point1
在这里?您可以删除它们。printf("d is: %d\n", d);
是错的。您需要使用%Lf
的long double
printf ("e is: %f\n", e);
也是错误的。您需要使用正确的转换说明符。使用%Lf
了long double
。也就是说,推荐的签名main()
是int main(void)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句