Suppose that a user wants to simulate rolls of 6-sided dice with a program. In particular, the user wants to know how often a roll of two dice produce snake-eyes, which means that both dice roll to one. 2a [24 points] Write an algorithm to design a program that will find the percent chance of rolling snake-eyes by determining (100)(successful rolls)/(total rolls). 2b [25 points] Complete the code in Problem 2b to write a program that will report the chance of rolling snake eyes. Your algorithm does not have to match this code, but it will likely be similar. 2c [1 point] What is the exact value of chance of rolling snake-eyes on any given roll? *************************************************************** % 2b Solution: % Initialize variables: maxrolls = 100; % max number of allowed rolls count = 0; % number of rolls so far snakeeyes = 0; % number of snake-eyes so far % Roll both dice and count snakeeyes for maxrolls: for count=1:maxrolls count=count+1; roll1=floor(rand*6)+1; % roll 1st die roll2=floor(rand*6)+1; % roll 2nd die % Count snake-eyes: if roll1==1 & roll2==1 snakeeyes=snakeeyes+1; end end % Report chance of rolling snake-eyes disp(['Chance of rolling snake-eyes: ',num2str(100*snakeeyes/maxrolls),'%']); ****************************************************************** Problem 3 [30 points] Repetition Statements Problem: Write a MATLAB program that prints out all of the even numbers between and including two numbers that a user inputs. Assume that the user inputs two legal numbers, but your program will have to determine which number is bigger than the other. Print the minimum and maximum values only if they are even. You may use logical arrays, if you wish. Be sure to supply brief comments. Example Session: >> problem3 Enter a number: 7 Enter another number: 2 2 4 6 ****************************************************************** % Initialize variables: value1 = input('Enter a number: '); value2 = input('Enter another number: '); % Pick max and min: if value1>value2 max=value1; min=value2; else max=value2; min=value1; end % Report even values between min and max: for ii=min:max % Find even number: if mod(ii,2)==0 disp(num2str(ii)); end end ****************************************************************** Problem 1 [20 points] Nested loops, Character graphics Task: Write a program that prints a trapezoid of stars (’*’) for a depth supplied by the user. This trapezoid will always have three stars on top. For instance, the following trapezoid has a depth of five: *** ***** ******* ********* *********** Approaches: There many ways to do this problem. You might wish to follow this algorithm: • Start with a row. • For the current row, print the columns. - Print a space until you reach “the end,” which varies as the difference depth-row. - Print stars until you reach spaces again. • Go to the next row until you exceed the depth. ******************************************************************* for row=1:depth % print whitespace: for col=1:depth-row fprintf(' '); end % print stars: for col=1:row*2+1 fprintf('*'); end % advance to the next line: fprintf('\n'); end ********************************************************************