mercredi 24 février 2021

Trouble with storing random number and Yes No loop

I have written a c code to generate random numbers(each time generate a different set of random numbers) and need to ask the user if want to generate another random number. If the user reply yes, the program need to generate another random number; if the user replies No, the program needs to calculate the total and average of the random numbers.

My problem is I don't know how to store the random number and there is a problem with my loops.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

main() 
{
    int sum=0, i, num[100], arr[100];
    float avg;
    char choice;
    
    srand(time(0));

        for(i=0; i<100; i++)
        {
            arr[i] = rand();
            printf("Random number generated: %d", arr[i]);
            fflush(stdin);
            printf("\nGenerate another random number(y/n)? ");
            scanf("%c", &choice);
            if(choice == 'n' || choice == 'N')
            {
                sum += arr[i];
                avg = sum / i;
        
                printf("\n::TOTAL     : %d", sum);
                printf("\n::AVERAGE   : %.2f", avg); 
            }
        }    
    system("PAUSE");
}

The correct output should be like this: Correct output




Aucun commentaire:

Enregistrer un commentaire