在C中规范化2d数组时值不正确

时间:2015-05-17 18:11:51

标签: c arrays random malloc 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 %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);
}

2 个答案:

答案 0 :(得分:0)

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

    建议不要
  1. int max = NULL;。请改用简单的0
  2. 请不要投放malloc()
  3. 的结果
  4. 这里有pointpoint1是什么?你可以删除它们。
  5. printf("d is: %d\n", d);错了。您需要%Lf使用long double
  6. 最后printf ("e is: %f\n", e);也是错误的。您需要使用正确的转换说明符。将%Lf用于long double
  7. 尽管如此,main()的推荐签名是int main(void)

答案 1 :(得分:0)

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

它干净地编译和工作

它包含错误检查

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

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

请注意,显示的值(最小值和最大值)仅取决于从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