dimanche 17 mai 2015

Incorrect values while normalizing 2d array in C

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);

}




Aucun commentaire:

Enregistrer un commentaire