CMSI 182 -  Introduction to Computer Science

Week 6

References

Biermann's class notes:

Fractals

Examples

program myTree2; { p. 142, Exercise 5
  This program uses subroutines to simplify the code. }
uses WinCrt;
var answer : string;

procedure recommend(message : string);
{note: We don't need to put var before message in the parameter
  list, because this procedure does not need to change the value
  of message.  I.e., this procedure simply uses the value of
  message; it does not change its value.}
begin
write('Based on your responses, ');
writeln('I recommend that you ' + message + '.');
end;

procedure recommendAMovie;
var answer : string;
begin
writeln('Are you a SF fan (y/n)?');
readln(answer);
if answer = 'y' then
   begin
   recommend('see Galaxy Quest');
   end
else
   begin
   recommend('see Patch Adams');
   end;
end;

procedure recommendANonMovieActivity;
var answer : string;
begin
writeln('Do you like dining out (y/n)?');
readln(answer);
if answer = 'y' then
   begin
   recommend('dine at Bruno''s Ristorante');
   end
else
   begin
   recommend('go bowling');
   end;
end;

begin
writeln('Do you like movies (y/n)?');
readln(answer);
if answer = 'y' then
   begin
   recommendAMovie;
   end
else
   begin
   recommendANonMovieActivity;
   end;
end.
 

program search2;  { search2.pas --
        search an array of integers; calls searchInt }
uses WinCrt;
type
    intArray100 = array[1..100] of integer;

procedure searchInt(var A : intArray100;
   var sizeA, target, position : integer);
   { search an integer array }
   var
      i : integer;
   begin
   i := 1;
   position := 0;
   while (i <= sizeA) and (position = 0) do
      begin
      if A[i] = target then
         begin
         position := i;
         end;
      i := i + 1;
      end;
   end;

var
    intArray : intArray100;
    i, n, item, where : integer;
    continue : string;
begin

{ fill the array }
writeln('How many elements?');
readln(n);
i := 1;
while i <= n do
   begin
   writeln('Enter integer number ', i);
   readln( intArray[i] );
   i := i + 1;
   end;

{ search for values user enters }
continue := 'y';
while continue = 'y' do
  begin
  writeln('Enter the integer to search for.');
  readln(item);
  searchInt(intArray, n, item, where);
  if where <> 0 then
     writeln('Your number was found at position ', where)
  else
     writeln('Your number was not found.');
  write('Continue? (y/n): ');
  readln(continue);
  end;
end.

Homework #5

Note well:  Be sure to save your files in your folder within the  U:\cmsi182  folder.

p. 137   Exercise 2

p. 142  Exercise 5

Read

Chapter 5: p. 202-207 What Will It Look Like? (C)

Study for test 1 which will be next week. Know all material covered in the first six weeks. The test will be similar to the homework exercises.  I.e., you will be asked to write a program (or two) that performs some task; the program(s) will involve concepts and Pascal features you have used in your homework assignments.


Ken Geddes Home Page | CMSI 182 Index | Week 5 | Week 7

Last Modified February 18, 2000
kengeddes@earthlink.net