NEED URGENT HELP!!
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Create a Matlab function that simulates any number of rolls of a die with any number of sides and computes the total and average of the number of rolls. Also create a main program that tells the user input the number of sides of the die they want to use and number of rolls. In other words, you should turn in two .m files for this problem. The values for the number of sides and number of rolls are fed into the function and then the main program then outputs the total and the average value of the rolls.
For example, if the user wants to roll a 6-sided die 3 times and got 4,2,6 as their rolls, the function would calculate and the main program would output the total of 12 and the average of 4.
An outline of how these two files should work together is as follows:
-In your main program, the user should be prompted to input the number of sides of the die they want to roll (for example 4,6,10, etc) and the number of rolls they want to make with that die.
-The number of sides and the number of rolls are sent to the dice roll function which is saved as a separate .m file. The dice roll function should:
-take as an input from the user the number of sides of the die and the number of rolls
-Have as outputs the sum of the values of the rolls and the average value of the rolls
-Use the randi function to roll the die. The format for using this function is:
VariableName= randi(numberofsides);
where Variablename is the name of your variable storing the dice role and numberofsides is the number of sides of the die. This function will generate a random integer between 1 and numberofsides and can be used to simulate each roll
-Roll the die the specified number of times. Hint: there are at least two ways ways to program this. Think about what two types of code blocks we can use if we want to repeat the same code a certain number of times. Either of those two options will work for this case since we know how many rolls we will need to make, just be sure to set up the version you choose correctly. Keep a running total of the sum of the rolls inside of this block of code
-Once you have done the rolls and have the sum of the values of the rolls you can calculate the average by dividing the total by the number of rolls
-Send the total and the average to the main program
-output the total and the average in the main program
2 Kommentare
John D'Errico
am 27 Apr. 2023
Bearbeitet: John D'Errico
am 27 Apr. 2023
@Geovany - that you had problems doing your homework is your problem, not ours. In fact, nobody should have given you the answer when you offered no attempt on your own. That you need help is not relevant. I wouldhave closed your qiuestion immediately had I seen it, and I will do so in the future if you continue to post homework assignments with no effort shown.
As well, please don't use flags for the wrong purpose. That you really, really want help is not a valid purpose. Talk to your teacher.
Antworten (2)
alexander Mcghee
am 27 Apr. 2023
Bearbeitet: alexander Mcghee
am 27 Apr. 2023
Haha is this homework
run this one as a script
% main_dice_roll.m
% Prompt the user to input the number of sides and rolls
num_sides = input('Enter the number of sides of the die: ');
num_rolls = input('Enter the number of rolls you want to make: ');
% Call the dice_roll function with the given inputs
[total, average] = dice_roll(num_sides, num_rolls);
% Output the total and average values of the rolls
fprintf('The total of the rolls is: %d\n', total);
fprintf('The average value of the rolls is: %.2f\n', average);
dice roll functions
% dice_roll.m
function [total, average] = dice_roll(num_sides, num_rolls)
% Initialize the total
total = 0;
% Roll the die the specified number of times and update the total
for i = 1:num_rolls
roll_value = randi(num_sides);
total = total + roll_value;
end
% Calculate the average value of the rolls
average = total / num_rolls;
end
1 Kommentar
John D'Errico
am 27 Apr. 2023
Please don't do homework assignments on this site for students. This does not help the student any more than teaching tham that there will always be someone willing to do their work for them. That is a bad thing to teach.
It does not help the site, hurting it in fact, because now we have a student who feels that if they just plead desperation, they will con someone into doing their homework for them.
Worse, it hurts the site because other students will come to the same conclusion.
Answers is not a service where we do homeworkk for students. If we allow it to be that, then the site will quickly become overrun with students bombing it with homework assignments.
albara
am 27 Apr. 2023
To achieve this, you will create two .m files. The first one will be the main program, and the second one will be the dice roll function. Below is the code for both files:
- main_program.m:
% Main program for dice roll simulation
% Prompt user for the number of sides and rolls
numSides = input('Enter the number of sides of the die: ');
numRolls = input('Enter the number of rolls: ');
% Call the dice roll function with the user inputs
[total, average] = diceRollFunction(numSides, numRolls);
% Display the total and average of the rolls
fprintf('Total of the rolls: %d\n', total);
fprintf('Average of the rolls: %.2f\n', average);
2. diceRollFunction.m:
function [total, average] = diceRollFunction(numSides, numRolls)
% Dice roll function to simulate rolling a die with a given number of sides and rolls
% Initialize the total of the rolls
total = 0;
% Roll the die the specified number of times and update the total
for i = 1:numRolls
roll = randi(numSides);
total = total + roll;
end
% Calculate the average of the rolls
average = total / numRolls;
end
Here's how the two files work together:
- The main program (main_program.m) prompts the user to input the number of sides of the die and the number of rolls.
- The main program then calls the dice roll function (diceRollFunction.m) with these inputs.
- The dice roll function simulates the dice rolls using a for loop and the randi function, calculates the total and average, and returns them as outputs.
- The main program displays the total and average of the rolls.
To run the main program, open MATLAB and navigate to the folder containing both .m files. Then, type "main_program" in the command window and press Enter. Follow the prompts to input the number of sides and rolls, and the program will display the total and average of the rolls.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes
1 Kommentar
John D'Errico
am 27 Apr. 2023
Please don't do homework assignments on this site for students. This does not help the student any more than teaching tham that there will always be someone willing to do their work for them. That is a bad thing to teach.
It does not help the site, hurting it in fact, because now we have a student who feels that if they just plead desperation, they will con someone into doing their homework for them.
Worse, it hurts the site because other students will come to the same conclusion.
Answers is not a service where we do homeworkk for students. If we allow it to be that, then the site will quickly become overrun with students bombing it with homework assignments.
Siehe auch
Kategorien
Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!