Solved by Expert Tutors
Solved by Expert Tutors
Question

1. Arrays and Functions Introduction In this lab you will write a program to plot out a simple version of the &quot;bell curve&quot; or
“normal distribution&quot; based on the sum of4 random dice rolls. What you will learn After completing this lab you should be able to: I Prototype and write the implementation of a function
I Understand arrayr indexing
I Understand how to use loops to iterate over arrays I Introduce randomness into your program
Background Information and Notes
The &quot;normal distribution&quot; 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? 500 (this is the user input)
4 :
5:X
6 : XXX
7 : XXXXXXXXXXXXXX
8 : XXXXXXXXXXXXXX
9 : XXXxxxxxxxxxxxxxxxxxxxx
10 : XXXXxxxxxxxxxxxxxxxxxxxxxxxxxxxx
11 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
12 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
13 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
14 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
15 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
16 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
17 : XXXXXXXXxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
18 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
19 : XXXXxxxxxxxxxxxxxx
20 : XXXXXXXXXX
21 : XXX
22 : XX
23:
24:X
In this example, we asked the program to simulate 500 4-dice rolls. The sum was
never four, the sum was five on 1 of the 500 trials, the sum was six on 3 of the
trials, etc.
[2 pts.] int roll()
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.
Overview of the rest of the lab: The rest of your lab will be to write a program to
simulate 500 rolls of 4-dice.
Array index:
0
2
20
Count of how
Count of how
Count of how
Count of how
many 4-dice rolls many 4-dice rolls many 4-dice rolls
many 4-dice rolls
added up to 4
added up to 5
added up to 6
added up to 24
[3 pts.] void printHistogramlint counts) 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 printHistogramtint oounte[ 1] that takes an array of integers {assumed to be
of size 21 elements], then prints out sequential labelled lines from 4 to 24 with as
many 1-: symbols on that line as is indicated in the appropriate element of the array
li.e. oounto[i] :I. We start our labels at 4 since rolling 4 dice can't produce a sum of
0- 3 {Le 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]. Let's test our printﬂietogram function by writing a sample main: ;. that calls it. We will ﬁll in a
dummy array and just ask printHistogram to print it out. You should try the same
and ensure your printﬂisttogram is working with our dummy main” : int main {
int teetCounts[21]; If 21 options of sum of 4 dice =} [4,24]
for {int i=0; 1:21; i++]
testCounte[i] = ifﬂ: f? fill the array
printHietogram(teetcounte1: ff call your method }
it should produce the following output:
4:
5:
6 :X
7 :X
8 : XX
24 : XXXXXXXXXX
[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 howr often each possible sum {from
4 to 24} occurred Prints out the histogram showing howlr mam;r 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.

The student who asked this found it Helpful

nec facilisis. Pellentesque dapibus efficitur laoreet

dictum vitae odio. Done

a molestie consequ

ctum vitae odio.

acinia pulvinar

onec aliqu

o

ng elit. N

ce dui lectus, congue vel laoreet ac, dic

ultrices a

et, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur la

ng elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Na

congue vel laoreet ac, dictum vitae odio. Do

facilisis. Pellentesque dapib

amet, co

ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit

ultrices ac magna. Fusce dui lectus, con

s ante, dapibus

ongue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem

ec aliquet. Lorem

sum dolor

iscing elit. Nam lacinia pulvinar t

amet, co

ce dui lectus, congue v

sque dapibus efficitur laoreet. Nam risus ante,

ctum vitae odio. Donec aliquet. Lorem ipsu

ac, dictum vitae odio. Done

sum dolor

ipiscing elit. Na

o

### Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

• ### -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents