1. example 1 – averages 2. example 2 – rolling dice 3. example 3 – number analysis 4. example...

Post on 04-Jan-2016

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Ex 1: average, display result

%initialize variablessum = 0;average = 0;

%loop 10 timesfor 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 averagefprintf(‘The average is %.3f.\n’, sum/10);

3

Ex 1: average, Output Result

4

Second run:

>>value 01 is 0.971value 02 is 0.957value 03 is 0.485value 04 is 0.800value 05 is 0.142value 06 is 0.422value 07 is 0.916value 08 is 0.792value 09 is 0.959value 10 is 0.656The average is 0.710.>>

First run:

>>value 01 is 0.906value 02 is 0.127value 03 is 0.913value 04 is 0.632value 05 is 0.098value 06 is 0.278value 07 is 0.547value 08 is 0.958value 09 is 0.965value 10 is 0.158The average is 0.558.>>

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 total5

Ex 2: rolling dice, display

% prompt user how many dice to rolltotalDice = input(‘How many dice will you roll? ’);sumDice = 0; %set sum to zero

% loop that many timefor roll = 1:totalDice

%generate one value (between 1 and 6)dieValue = ceil(rand*6);%display value of each diefprintf(‘Roll %d gave %d.\n’, roll, dieValue);%compute running totalsumDice = sumDice + dieValue;

end

fprintf(‘Sum = %d\n’,sumDice); %display total 6

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 ValuesAnalyzer

Nb. of odd values

Nb. of even values

% of odd values

% of even values

SCREEN

(NO INPUTS)

Ex 3: An algorithm

%loop to generate 200 whole numbersfor

%generate 1 random integer [1-100]if % oddIncrement odd counter

else %default to evenIncrement even counter

endend% Compute odd and even percentages% Print out results

9

Note: counters need to be INITIALIZED to 0. Before or inside the loop?

Ex 3: An algorithm

%set up both counters at zero CRUCIAL%loop to generate 200 whole numbersfor

%generate 1 random integer [1-100]if % oddIncrement odd counter

else %default to evenIncrement even counter

endend% Compute odd and even percentages% Print out results

10

Ex 3: Solution (1/2), analyzing

% set up both counters at zeroodd_cnt = 0;even_cnt = 0;

%loop to create/analyze numbersfor ct = 1:200

value = floor(rand*100) + 1; %generate integerif mod(value,2) == 1 %odd numberodd_cnt = odd_cnt + 1;

else %had to be even by default!even_cnt = even_cnt + 1;

endend

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

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

Ex 4: algorithm

number = 0; % start number at zeronb_displayed = 0; % set how many nb have been displayed to zero

% while 10 nbs have not been displayedwhile 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 +1nb_displayed = nb_displayed +1;

end

number = number +1; %go to next numberend

14

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

top related