Classwork wrote: |
---|
Write a program that assists in a study of rolls of a die. The program should ask the user to roll a die and enter the result. The number of rolls should be entered at the start. Based upon the number entered, a looping procedure of your choice (for, while, do-while) is used to generate the question: Roll die and enter result (1, 2, 3, 4, 5, or 6) #_: When complete, the program should display the number and the percentage of 1's, 2's, 3's, 4's, 5's, and 6's. Program output should look similar to: How many rolls to anaylze? "5" Roll die and enter result (1, 2, 3, 4, 5, or 6) #1: "1" Roll die and enter result (1, 2, 3, 4, 5, or 6) #2: "3" Roll die and enter result (1, 2, 3, 4, 5, or 6) #3: "1" Roll die and enter result (1, 2, 3, 4, 5, or 6) #4: "2" Roll die and enter result (1, 2, 3, 4, 5, or 6) #5: "5" Results: 2 1's, or 40% 1 2's, or 20% 1 3's, or 20% 0 4's, or 0% 1 5's, or 20% 0 6's, or 0% |
|
|
output wrote: |
---|
How many rolls to analyze? 5 Roll die and enter result (1, 2, 3, 4, 5, or 6) #1: 1 Roll die and enter result (1, 2, 3, 4, 5, or 6) #2: 3 Roll die and enter result (1, 2, 3, 4, 5, or 6) #3: 1 Roll die and enter result (1, 2, 3, 4, 5, or 6) #4: 2 Roll die and enter result (1, 2, 3, 4, 5, or 6) #5: 5 Roll die and enter result (1, 2, 3, 4, 5, or 6) #6: 6 Results: |
rolls>=1 &&
in the while statement. Do the checking for what the user enters at the point of entry. You could use
|
|
whitenite1 wrote: | ||
---|---|---|
Get rid of the <= before rolls, and just use <. Also, you could remove the rolls>=1 && in the while statement. Do the checking for what the user enters at the point of entry. You could use
|
Rohit Saluja wrote: |
---|
You should initialize the variable rollnum in the program as might be taking garbage values and may cause the unexpected behavior of the while loop |
int dice[6];
whitenite1 wrote: |
---|
The best way I can think of, is first create an array of 6. Like int dice[6]; And initialize it to zeros. Then for each roll, increase dice[result-1], by 1. You need to subtract one, since arrays start at zero, not one. Then, when the user is finished throwing the die, get the percentage of each array location, and print them out. |