CMSI 182 -  Introduction to Computer Science

Week 4

References

Biermann's class notes:

Examples

program sphere;  { p89n1  sphere.pas --
        calculate the volume of a sphere }
uses WinCrt;
var
   r, V : real;
begin
writeln('Enter the radius.');
readln(r);
V := 4.0 / 3.0 * 3.14159 * r * r * r;
writeln('The volume is ', V : 8:2);
end.

page 93 Exercise 1 - partial solution

a) English description:  the function receives the radius of a sphere and returns its volume.

b) computer program: see the  sphere  program above.

c) table:
 
r
V(r)
1
4.19
2
33.51
3
113.10
4
268.08
...
...

(Note: The function could also be described using mathematical notation or a graph.)

a loop for the square function

program square;  { square.pas --
        calculate the squares of the first twenty integers }
uses WinCrt;
var
   i, isqr : integer;
begin
writeln('i      i squared'); { write headings }
i := 1;
while i <= 20 do
   begin
   isqr := i * i;
   writeln(i, '      ', isqr);
   i := i + 1;
   end;
end.
 

program lowestValue;  { p98n2  lowest.pas --
        find the lowest value of the given function }
uses WinCrt;
var
   x, f, lo, hi : real;
begin
writeln('Enter lo: ');
readln(lo);
writeln('Enter hi: ');
readln(hi);
writeln('   x         f'); { write headings }
x := lo;
while x <= hi do
   begin
   f := x * x - 5 * x + 4;
   writeln(x : 6:2, '    ', f : 6:2);
   x := x + 1.0;
   end;
end.
 

program findBest2;  { p102n1  findbst2.pas --
        maximize the volume of a cylinder }
uses WinCrt;
var
   r, h, V, previousV, increase : real;
begin
writeln('What is the initial value of r?');
readln(r);
writeln('How much should r be increased each time?');
readln(increase);
previousV := 0;
V := 500 * r - 3.14159 * r * r * r;
while V >= previousV do
   begin
   r := r + increase;
   previousV := V;
   V := 500 * r - 3.14159 * r * r * r;
   writeln(r : 10:3, V : 10:3);
   end;
r := r - increase;  { previous r was best }
h := (1000 - 2 * 3.14159 * r * r) / ( 2 * 3.14159 * r);
writeln('r = ', r : 10:3, '    h = ', h : 10:3);
writeln('maximum volume = ', previousV : 10:3);
end.
 

program temperatureArray;  { temparr.pas --
        store 24 temperatures in an array
        and allow user to query array }
uses WinCrt;
type
    tempArray = array[1..24] of real;
var
    temp : tempArray;
    i : integer;
begin

{ store 24 temperatures in an array }
i := 1;
while i <= 24 do
   begin
   writeln('Enter temp ', i);
   readln(temp[i]);
   i := i + 1;
   end;

{ allow user to query array }
while i <> 0 do
   begin
   writeln('Which temp do you want? (1 - 24; 0 to stop)');
   readln(i);
   if (i >= 1) and (i <= 24) then
      begin
      writeln('temp ', i, ' is ', temp[i] : 5:2);
      end
   else
      begin
      if i <> 0 then
         begin
         writeln(i, ' is out of range.');
         end;
      end;
   end;
end.

 

program search;  { p110n2  search.pas --
        search an array of integers }
uses WinCrt;
type
    intArray = array[1..10] of integer;
var
    A : intArray;
    i, n : integer;
    found : string;
begin

{ fill the array }
i := 1;
while i <= 10 do
   begin
   writeln('Enter integer number ', i);
   readln( A[i] );
   i := i + 1;
   end;

{ search the array }
writeln('Enter the integer to search for.');
readln(n);
i := 1;
found := 'false';
while (found = 'false') and (i <= 10) do
   begin
   if n = A[i] then
      begin
      writeln('FOUND');
      found := 'true';
      end;
   i := i + 1;
   end;
if found = 'false' then
   begin
   writeln('NOT FOUND');
   end;
end.

Homework

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

p. 89   Exercise 2

p. 98   Exercise 1 p. 102   Exercise 2 p. 110   Exercise 1
Ken Geddes Home Page | CMSI 182 Index | Week 3  | Week 5

Last Modified September 22, 1999
kengeddes@earthlink.net