Answer to Question #200902 in C++ for Halil Esendaglı

Question #200902

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-05-31T00:53:58-0400
#include <iostream>
#include <cstdlib>
using namespace std;


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};
    cout<<"\nFrequencies:\n";
    for(int i=0;i<6;i++){
        cout<<"Frequency of "<<i+1<<" is: "<<sum1[i]<<endl;
    }
}
int main()
{
    srand(time(0));
    int arr1[100];
    int arr2[100];
    //cout<<"\nFirst array:\n";
    for(int i=0;i<100;i++)
    {
        arr1[i]=rollDie();
        //cout<<arr1[i]<<" ";
    }
    //cout<<"\nSecond array:\n";
    for(int i=0;i<100;i++)
    {
        arr2[i]=rollDie();
        //cout<<arr2[i]<<" ";
    }
    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