Question

4.1 [2 pts. int rollO Define a function roll() that takes no arguments, and returns a random integer from 1 to 6. Before proceeding further, we recommend you write a short main method to test it out and make sure it gives you the right values. Comment this main out after you are satisfied that roll() works. Remenber:texibook Seciion 49 is on randormness, and Chapier 5is on functions. int getKandomNumber return 4 l chosen by fair dice roll. ll guaranteed to be random. HTTP:/IXKCD.COM/221 (NOTE: THIS FUNCTION IS A JoKE...DONT USE IT)4.2 [3 pts.] void printHistogram (int countsLI) Before moving on to the logic to add up rolls and count them, we will implement a function that encapsulates the output display of our sums. You will write a function void printHistogram(int counts[I) that takes an array of integers (assumed to be of 2 Last Revised: 1/27/2016 CS 103 Lab 4 - Arrays and Functions size 21 elements), then prints out sequential labelled lines from 4 to 24 with as many X symbols on that line as is indicated in the appropriate element of the array (i.e. countsil) We start our labels at 4 since rolling 4 dice cant produce a sum of 03 (i.e index 0 of the array corresponds to roll totaling 4, index 1 to a roll totaling 5, index 2 a roll totaling 6,., and index 20 to a roll totaling 24). Lets test our printHistogram function by writing a sample main() that calls it. We will fill in a dummy array and just ask printHistogram to print it out. You should try the same and ensure your printHistogram is working with our dummy main(): int main() f int testcounts [21]; // 21 options of sum of 4 dice -> [4,24] for (int i-e; i++) i〈21; testcounts[i] і/2; printHistogram(testcounts); /I call your method // fill the array it should produce the following output: 4: 5:4.3 [4 pts.] main function Finally, write the main function of the program. (Comment out all the other test main methods you used earlier.) It should Ask the user for a number, call it n .Run n experiments, where each experiment rolls 4 dice and adds them up . Throughout the experiments, keeps track of how often each possible sum (from 4 to 24) occurred Prints out the histogram showing how many times each sum occurred (so in total it should print out n of the X symbols) Your program should give different results each time it runs. Make sure you seed the random number generator appropriately.

3 Background Information and Notes The normal distribution is a very important family of probability distributions. If you take any random event that produces numerical values, and repeat it often, the average of those values, although random, behaves in a predictable way. In this lab we are using only a small number of rolls (4), which is not infinite, but enough to yield a curve that resembles the bell curve. Here is a sample run of your program: How many rolls do you want? 5ee (this is the user input) 4: 5:X 6: XXX 10: x000000000x000xx000000xxxxxxx 11:X 18:xxxxxx0xxxxbo 21:XXX 22:XX 23: 24:X

Thank you!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

-----------------------

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

int roll()
{
return rand()%5+1;
}


void printHistogram(int counts[21])
{
for (int i=0; i<21; i++)
{
printf("\n%d:",i+4);
for (int j =0 ; j<counts[i]; j++)
{
printf("X");
}
}
}

int main()
{
int i, n, r1, r2, r3, r4;
printf("How many rolls do you want? ");
scanf("%d",n);
int testCounts[21] = {0};
for(i=0; i<n; i++)
{
r1 = roll();
r2 = roll();
r3 = roll();
r4 = roll();
int total = r1+r2+r3+r4;
printf("%d ",total);
testCounts[total-4] += 1;
}
printHistogram(testCounts);
return 0;
}

Output

-----------------

How many rolls do you want? 50
4:
5:
6:
7:XX
8:XXXXX
9:X
10:XXXXX
11:XXXXXXXXXXX
12:XXXX
13:XXXXXX
14:XXXXXX
15:XXXX
16:XXX
17:X
18:X
19:
20:X
21:
22:
23:
24:

Add a comment
Know the answer?
Add Answer to:
Thank you!!! 4.1 [2 pts. int rollO Define a function roll() that takes no arguments, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • What is wrong with the following function definition? void fill(const int a[], int size) { for...

    What is wrong with the following function definition? void fill(const int a[], int size) { for (int i = 0; i < size; i++) {     a[i] = 0; } return; } Answers: The function cannot change the array parameter. The array should be passed as a call-by-reference, not call-by-value. The for loop causes an out-of-bounds indexing error. Nothing, the code works fine as is.

  • This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the...

    This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the game of craps, a shooter rolls 2 dice and adds the dots on the upper most faces of the dice. 7 or 11 on the first roll wins, 2, 3, or 12 on the first roll loses, andthing else is call the point and the player rolls again The following program fragment uses 1-way if statements simulate the 1st roll of the dice. Replace...

  • dont use switch E) 120 pts.] Write another function int main ) in C++(and comment the...

    dont use switch E) 120 pts.] Write another function int main ) in C++(and comment the main in Q1.D) that displays the following menu using a do while loop: please choose from the following Menu: I: Populate array with 5 elements 2: Print Array 3: Reverse Array 0: Exit Based, on the user choice, you should call the appropriate function. For example, if the user: - chooses 1, then in function main you should call function populateArray (A, 5); -...

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std;...

    Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std; int read_function(int array[], int, int); int main() { int array[300], numba; read_function(array[300]); cout << "Enter a whole number between 2-20: " << endl; cin >> numba; read_function(numba); return 0; } int read_funtion (int arr[300], int num, int Values) { int sum=0; ifstream array_file; array_file.open("Lab8.dat"); for(int i=0; i < 300; i++) {   array_file >> arr[i];   cout << arr[i];   sum += i; } cout << sum;...

  • Practice: • function • loops • if condition • random Roll a dice many times, you...

    Practice: • function • loops • if condition • random Roll a dice many times, you will see that each face value will count of roughly 1/6 • write a function • roll a dice once, return value will be 1-6, • use your functin I call your function 6000 times • count how many times of face 1 • count how many times of face 2 • print out the numbers of total face 1, 2, ..., 6 you...

  • // This program creates a simulated player who takes one turn in the // Pig dice...

    // This program creates a simulated player who takes one turn in the // Pig dice game. The simulated player keeps rolling the die until // the total for the turn is 20 or greater, or until a 1 is rolled. // If a 1 is rolled, the player's score for the turn is 0. Otherwise // the player's score is the sum of the rolls for the turn. // ///////////////////////////////////////////////////////////////////// #include<iostream> #include<cstdlib> #include<time.h> using namespace std; int randNumGen(int upper,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT