Answer to Question #201337 in C for Halil Esendaglı

Question #201337

1.Roll a dice 100 times and store the results in an array.

2.Roll a dice 100 times again and store the results in a different array.

3. The sums of the faces in each time are stored in another array.

4. Show the frequencies of the sum array on the screen. 


1
Expert's answer
2021-06-01T00:06:16-0400
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int rollDie()
{
    int roll;
    int min = 1;
    int max = 6;




    roll = rand() % (max - min + 1) + min;




    return roll;
}
int sumFaces(int arr[100]){
    int a1=0; int a2=0; int a3=0; int a4=0; int a5=0; int a6=0;
    
    for(int i=0;i<100;i++){
        if (arr[i]==1)
            a1++;
        else if (arr[i]==2)
            a2++;
        else if (arr[i]==3)
            a3++;
        else if (arr[i]==4)
            a4++;
        else if (arr[i]==5)
            a5++;
        else if (arr[i]==6)
            a6++;
    }
    int sum1[6]={a1,a2,a3,a4,a5,a6};
    printf("\nFrequencies:\n");
    for(int i=0;i<6;i++){
        printf("Frequency of %d is %d \n", i+1,sum1[i]);
    }
}
int main()
{
    srand(time(0));
    int arr1[100];
    int arr2[100];
    for(int i=0;i<100;i++)
    {
        arr1[i]=rollDie();
    }
    for(int i=0;i<100;i++)
    {
        arr2[i]=rollDie();
    }
    sumFaces(arr1);
    sumFaces(arr2);
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS