在C中标准化2D阵列时不正确的值



我试图根据几个给定的方程式将2D数组归一化。该数组的值填充在使用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 %dn", 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: %dn", d);
        e =( d / (max - min));
        e = (e * 255);
        printf ("e is: %fn", e);
        MyArray1[i][j] = e;
    }
}
printf("the max is: %dn", max);
printf("the min is: %dn", min);
free(point);
free(point1);
}

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

  1. 不建议使用int max = NULL;。改用简单的0
  2. 请不要施放malloc()的结果。
  3. 在此处具有pointpoint1是什么?您可以删除它们。
  4. printf("d is: %dn", d);是错误的。您需要将%Lf用于long double
  5. 最后printf ("e is: %fn", e);也是错误的。您需要使用正确的转换说明符。将%Lf用于long double

也就是说,main()的建议签名是int main(void)

这是已发布代码的版本。

它干净地编译和工作

它包括错误检查

此版本不包含任何未使用的东西

也没有任何未向用户显示的东西。

请注意,显示的值(min和max)仅取决于从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 %dn", 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: %dn", max);
    printf("the min is: %dn", min);
    return(0);
} // end function: main

相关内容

  • 没有找到相关文章

最新更新