AllFreePapers.com - All Free Papers and Essays for All Students
Search

Computer Science

Autor:   •  April 11, 2011  •  Study Guide  •  457 Words (2 Pages)  •  1,998 Views

Page 1 of 2

Solve w/ while loop

100 feet of fencing

Should be a post every 10 feet

How many posts do you need?

Post= 0;

Fence=0;

While Fence ≤ 100

Post=Post+1;

Fence=Fence +10;

End

Disp(Post)

Using the while loop construct, create a program that generates a random integer from 1 to 10 and prompt user to repeatedlt make attempts to guess the number. The program should continue to prompt for guesses until the user makes the correct one, at which point the program should congratulate the user and reveal the random number.

X=ceil(rand()*9+1);

Guess=input(‘Guess the number');

While guess~=x

Guess=input(‘Wrong, Try again');

End

Disp(‘Correct");

FOR LOOP

A picket fence can be described by two arrays. Each array has five entries representing the five fence posts. The first array, Heights, describes the height of each fence post. The second array, Locations, describes the distance of each fence post from the first one. Working together as a group, create two separate MATLAB functions for dealing with a picket fence. The first MATLAB function should find the highest fence post. The second MATLAB function should find the smallest distance between fence posts. Save the functions in m-files "fp1" and "fp2" followed by the first and last initials of all group members.

Height=[5,6,4.5,5.5,7];

Locations = [0,4,10,13,20];

Function [result]=fp1(heights)

Result=heights(1);

For i=2:length(heights)

If heights(i)>result

Result=heights(i);

End

End

Function[result]=fp2(locations);

Result=locations(2)-Locations(1);

For i=3:length(locations)

If (locations(i) – locations(i-1))<result

...

Download as:   txt (3 Kb)   pdf (69.8 Kb)   docx (11.4 Kb)  
Continue for 1 more page »