在 C 中规范化二维数组时值不正确

Incorrect values while normalizing 2d array in C

我正在尝试根据几个给定的方程式对二维数组进行归一化。该数组已使用 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);
}

我可以在你的代码中找到问题,

  1. int max = NULL; 不推荐。请改用简单的 0
  2. 请不要转换 malloc() 的结果。
  3. 这里有 pointpoint1 是什么?您可以删除它们。
  4. printf("d is: %d\n", d); 是错误的。您需要使用 %Lf 作为 long double
  5. 最后printf ("e is: %f\n", e);也错了。您需要使用正确的转换说明符。使用 %Lf 作为 long double.

也就是说,main()推荐的签名是int main(void)

这里是发布代码的一个版本。

它可以干净地编译和工作

它包括错误检查

此版本不包含任何未使用的内容

也不会向用户显示任何内容。

请注意,显示的值(最小值和最大值)仅取决于从 rand()%256

返回的值
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
    unsigned int x; // horizontal array dimension
    unsigned int y; // vertical array dimension

    srand(time(NULL));
    int i,j = 0; // loop counters
    int max = 0;
    int min = 255;


    printf("Hello user.\n");
    printf("Please enter the horizontal dimension of the array.\n");

    if( 1 != scanf("%u", &x) )
    { // then scanf failed
        perror( "scanf for x failed" );
        exit( -1 );
    }

    // implied else, scanf successful

    printf("Please enter the vertical dimension of the array.\n");

    if( 1 != scanf("%d", &y) )
    { // then scanf failed
        perror( "scanf for y failed" );
        exit( -1 );
    }

    // implied else, scanf successful

    int MyArray[x][y];

    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];
            }
        }
    }

    printf("the max is: %d\n", max);
    printf("the min is: %d\n", min);
    return(0);
} // end function: main