jeudi 27 octobre 2016

Trying to store randomly generated floating point numbers in an array.But cann't understand why the array is taking Garbage value

Random No is Generated correctly but when I'm storing it to an array sometimes it accepting garbage value

void main()
{
    int x = 0, y = 0, i = 0;
    srand(time(NULL));
    int r = rand();
    /*Generating Random Number in Floating Point. For calculating Dimension of Rectangle*/
    srand((unsigned int)time(NULL));

    float a = 5.0, s[20], sum = 0, x_rand;
    for (i=0; i<20; i++)
    {

        x_rand = (((float)rand()/(float)(RAND_MAX)) * a) + sum;
        sum += 1.53;
        //s[i] = x_rand;
        if(i != 0)
        {
           if(x_rand>s[i-1])
                s[i] = x_rand;
        }
        else
        {
            s[i] = x_rand;
        }
        printf("%f\n", x_rand);
    }
    printf("--------------------------\n");
    for(i =0 ; i<20; i++)
        printf("%.5f\n",s[i]);
}

Output of The Program

Aucun commentaire:

Enregistrer un commentaire