- #1
zack7
- 55
- 0
Homework Statement
Write a program to simulate rolling a six-sided “fair” die with one dot on one side, two dots on another side, three dots on another side, and so on. Allow the user to enter the number of rolls. Use first while loops to trap the illegal input number first (negative, zero, or float numbers). Use second loop to count the percentage distribution of each dot in the pay. Print the number of rolls that gave one dot, the number of rolls that gave two dots, and so on.
What should be the percentage distribution of the number of dots from the rolls?
2. Relevant condtions
1) use rand function and ceil or floor function to generate a whole number between 1-6
2) Use while loop and switch-case statement to count the times of each number in the play.
3) Use fprintf to display the number of dots generated and the percent distribution of number of dots from the rolls.
4) Trying to give a larger input number to loop more times. What should be the percentage distribution of each dot(s) on each side from the rolls?
The Attempt at a Solution
clc
clear
%User input
user=input('How many times would you want to roll the dice ? :');
%Error Statement
while user <=0 || mod(user,2)~=0 || mod(user,2)~=1;
disp('Error input,Please try again');
user=input('How many times would you want to roll the dice ? :');
end
% Intilization
counter=0;
d1=0;
d2=0;
d3=0;
d4=0;
d5=0;
d6=0;
%To determine the number of dots
while counter< user;
count=round(rand*5+1)
switch count
case (1)
d1=fprintf('The number of one dot is %2.f \n ',d1)
percentage1=count/user*100
percentage1=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage1);
case (2)
d2=fprintf('The number of two dot is %2.f \n ',d2)
percentage2=count/user*100
percentage2=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage2);
case (3)
d3=fprintf('The number of three dot is %2.f \n ',d3)
percentage3=count/user*100
percentage3=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage3);
case (4)
d4=fprintf('The number of three dot is %2.f \n ',d4)
percentage4=count/user*100
percentage4=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage4);
case (5)
d5=fprintf('The number of three dot is %2.f \n ',d5)
percentage5=count/user*100
percentage5=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage5);
otherwise
d6=fprintf('The number of three dot is %2.f \n ',d6);
percentage6=count/user*100
end
end
Thank you for all the help