Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. Example 1 – Averages 2. Example 2 – Rolling Dice 3. Example 3 – Number Analysis 4. Example 4 - Divisibility ( while ) Additional Examples on for Loops.

Similar presentations


Presentation on theme: "1. Example 1 – Averages 2. Example 2 – Rolling Dice 3. Example 3 – Number Analysis 4. Example 4 - Divisibility ( while ) Additional Examples on for Loops."— Presentation transcript:

1 1. Example 1 – Averages 2. Example 2 – Rolling Dice 3. Example 3 – Number Analysis 4. Example 4 - Divisibility ( while ) Additional Examples on for Loops 1

2 Ex 1: average, algorithm Generate 10 random values (between 0 and 1) and compute average %initialize variables %loop 10 times %generate 1 random value ]0-1[ %display the value %calculate running total %calculate, display average 2

3 Ex 1: average, display result %initialize variables sum = 0; average = 0; %loop 10 times for x = 1:10 value = rand; %generates 1 random value ]0-1[ fprintf(‘value %02d is %.3f\n’, x, value); sum = sum + value; %running total end %calculate, display average fprintf(‘The average is %.3f.\n’, sum/10); 3

4 Ex 1: average, Output Result 4 Second run: >> value 01 is 0.971 value 02 is 0.957 value 03 is 0.485 value 04 is 0.800 value 05 is 0.142 value 06 is 0.422 value 07 is 0.916 value 08 is 0.792 value 09 is 0.959 value 10 is 0.656 The average is 0.710. >> First run: >> value 01 is 0.906 value 02 is 0.127 value 03 is 0.913 value 04 is 0.632 value 05 is 0.098 value 06 is 0.278 value 07 is 0.547 value 08 is 0.958 value 09 is 0.965 value 10 is 0.158 The average is 0.558. >>

5 Ex 2: rolling dice, algorithm Prompt the user for the number of dice to roll. Generate that many random numbers (each between 1 and 6). Indicate all rolls on the screen, and display the sum of all dice. % prompt user how many dice to roll % set sum to zero % loop that many times %generate one value (between 1 and 6) %display value of each die %compute running total %display total 5

6 Ex 2: rolling dice, display % prompt user how many dice to roll totalDice = input(‘How many dice will you roll? ’); sumDice = 0; %set sum to zero % loop that many time for roll = 1:totalDice %generate one value (between 1 and 6) dieValue = ceil(rand*6); %display value of each die fprintf(‘Roll %d gave %d.\n’, roll, dieValue); %compute running total sumDice = sumDice + dieValue; end fprintf(‘Sum = %d\n’,sumDice); %display total 6

7 Ex 2: Output Result >> How many dice will you roll? 3 Roll 1 gave 2 Roll 2 gave 4 Roll 3 gave 6 Sum = 12 >> 7

8 Ex 3: Combine for / rand / mod Develop a program that generates 200 random integers between 1 and 100 both included. Count the nb. of odd values, nb. of even values, Also compute the percentages of odd and even values. Display all results to the screen. 8 Random Values Analyzer Nb. of odd values Nb. of even values % of odd values % of even values SCREENSCREEN (NO INPUTS)

9 Ex 3: An algorithm %loop to generate 200 whole numbers for %generate 1 random integer [1-100] if % odd Increment odd counter else %default to even Increment even counter end % Compute odd and even percentages % Print out results 9 Note: counters need to be INITIALIZED to 0. Before or inside the loop?

10 Ex 3: An algorithm %set up both counters at zero  CRUCIAL %loop to generate 200 whole numbers for %generate 1 random integer [1-100] if % odd Increment odd counter else %default to even Increment even counter end % Compute odd and even percentages % Print out results 10

11 Ex 3: Solution (1/2), analyzing % set up both counters at zero odd_cnt = 0; even_cnt = 0; %loop to create/analyze numbers for ct = 1:200 value = floor(rand*100) + 1; %generate integer if mod(value,2) == 1 %odd number odd_cnt = odd_cnt + 1; else %had to be even by default! even_cnt = even_cnt + 1; end 11 Notice the lack of semicolons (;) in FOR and IF statements Mod by 2 mod(2,2)0 mod(3,2)1 mod(4,2)0 mod(5,2)1 mod(6,2)0 mod(15,2)1

12 Ex 3: Solution (2/2), show results %calculate/display results odd_pct = 100* odd_cnt / 200; even_pct = 100 * even_cnt / 200; fprintf(‘%.2f% were odd, ’, odd_pct); fprintf(‘and %.2f% were even\n’, even_pct); 12

13 Ex 4: Display the first ten multiples of “5 and 7” Clear up the problem Start counting at 0 and display the first ten multiples of “5 and 7” together. For example: 35 is both a multiple of 5 and 7. The number of times the loop needs to count is unknown unless you previously solve the problem!!! Therefore a while loop is used. %while 10 numbers have not been displayed  this implies keeping track of how many numbers have been displayed. Use mod(.., 5) and mod(.., 7) combined. 13

14 Ex 4: algorithm number = 0; % start number at zero nb_displayed = 0; % set how many nb have been displayed to zero % while 10 nbs have not been displayed while nb_displayed <10 %could be: nb_displayed!=10 %if divisible by both 5 AND 7 if mod(number,5)==0 && mod(number,7)==0 disp(number) % display number % increment how many nb displayed by +1 nb_displayed = nb_displayed +1; end number = number +1; %go to next number end 14

15 Wrapping Up Go ahead, try to code these. See if you can make it run. Change them a bit: generate 20 high-scores (0 to 50) Find the maximum. Only count the top 5. Note that most examples required some nested construct within the loop. Only practicing with random little ‘dumb’ examples like these will help you… 15


Download ppt "1. Example 1 – Averages 2. Example 2 – Rolling Dice 3. Example 3 – Number Analysis 4. Example 4 - Divisibility ( while ) Additional Examples on for Loops."

Similar presentations


Ads by Google