Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Urgent help needed in c++ program ! Thanx :) In C++, you can generate random num

ID: 3586912 • Letter: U

Question

Urgent help needed in c++ program ! Thanx :)

In C++, you can generate random numbers by calling a function named rand () which is defined in header file. To generate random numbers between 1 and an integer N use the expression: (rand ( ) % n + 1) . Write a program that simulates the tossing of 100 dices and prints a histogram of the results. The following is sample outcome: One: Two: Three: Four: Five * * Six: Represent your histogram as an array of bars each of which has the following structure: enum Sidef ONE, TWO, THREE, FOUR, FIVE, SIX struct Bar int value; Side label; Your program should create and use at least the following three functions string getSideLabel (Side s) / returns the string label for each void generateData (Bar hist[], int tossesCount= 100); // Randomly // of the enumerated sides // generate the dice tosses. By default 100 tosses will be // generated. The results are saved into the array histI) string getAistogram (Bar hist[], char c = '*'); // Generate the whole // histogram and returns it as a string to be printed out in // the main function. Be default * will be used to create bars

Explanation / Answer

PLEASE REFER BELOW CODE

#include<iostream>
#include<cstdlib>
#include<string>

using namespace std;

enum Side{ONE, TWO, THREE, FOUR, FIVE, SIX};

typedef struct Bar
{
int value;
Side label;
}Bar;


void generateData(Bar hist[], int tossesCount)
{
for(int i = 0; i < tossesCount; i++)
{
int n = (rand() % 6) + 1; //generating random number between 1 and 6

hist[i].value = n; //adding value to histogram
}
}
string getSideLabel(Side s)
{
string enum_string[6] = {"One", "Two", "Three", "Four", "Five", "Six"};
return(enum_string[s]); //returing corresponding string for enum values
}
string getHistogram(Bar hist[], char c)
{
string result,one,two,three,four,five,six;
//histogram is starting with One:, Two: etc;
one.append(getSideLabel(ONE));
two.append(getSideLabel(TWO));
three.append(getSideLabel(THREE));
four.append(getSideLabel(FOUR));
five.append(getSideLabel(FIVE));
six.append(getSideLabel(SIX));

//appending : to output
one.append(": ");
two.append(": ");
three.append(": ");
four.append(": ");
five.append(": ");
six.append(": ");

//for each random generator adding corresponding * into the string i.e. if 1 is generated then added it in one
for(int i = 0; i < 100; i++)
{
if(hist[i].value == 1)
{
one.append("*");
}
else if(hist[i].value == 2)
{
two.append("*");
}
else if(hist[i].value == 3)
{
three.append("*");
}
else if(hist[i].value == 4)
{
four.append("*");
}
else if(hist[i].value == 5)
{
five.append("*");
}
else if(hist[i].value == 6)
{
six.append("*");
}
else
break;
}
//printing histogram shown in problem statement
result = one + " " + two + " " + three + " " + four + " " + five + " " + six + " ";
return result;

}
int main()
{
Bar *hist = new Bar[100];
string label,histogram;
generateData(hist, 100); //calling function with n=100 and histogram array
histogram = getHistogram(hist,'*'); //printing histogram
cout<<histogram<<endl;
return 0;
}

PLEASE REFER BELOW OUTPUT(output may vary as random number will be different for each run)

One: *******************
Two: ******************
Three: ************
Four: **************
Five: ************************
Six: *************


Process returned 0 (0x0) execution time : 0.027 s
Press any key to continue.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote