MIME-Version: 1.0 Server: CERN/3.0 Date: Monday, 06-Jan-97 20:49:38 GMT Content-Type: text/html Content-Length: 7375 Last-Modified: Tuesday, 08-Oct-96 21:43:57 GMT Solutions for Problems in MidTerm I ------------------------------------PROBLEM #1---------------------------------------------

Only a single Pascal statement is required, although the computation could be done with a couple of statements instead.

program problem1;
var
 s1, s2, s3 : string;
begin
  writeln('enter s1, the substring');
  readln(s1);
  writeln('enter s2, the superstring');
  readln(s2);
  s3:=copy(s2, pos(s1,s2), length(s2) - pos(s1,s2) + 1);  { <<<< the statement}
  writeln('s3 = ',s3);
  readln;
end.


------------------------------------PROBLEM #2---------------------------------------------

The procedure is all that is needed.

program problem2;
var
  s : string;
  n : integer;

procedure vowelCount (var s: string; var numberOfVowels: integer);
var
  i : integer;
  chr : string;
begin
  numberOfVowels := 0;
  i :=i 1;
  while i <= length(s) do
  begin
    chr := copy(s,i,1);
    if (chr='a') or (chr='e') or (chr='i') or (chr='o') or (chr='u') then
      numberOfVowels := numberOfVowels + 1;
    i := i + 1;
  end;
end;

begin
 writeln('enter a string');
 readln(s);
 vowelCount(s,n);
 writeln('number of vowels is: ',n);
 readln;
end.


------------------------------------PROBLEM #3---------------------------------------------

The procedure is all that is needed.

program problem3;
type
 integerArray = array [1..100] of integer;
var
 A : integerArray;
 n, i : integer;

procedure fib(var fibArray: integerArray; var n: integer);
var
 i : integer;
begin
 i := 3;
 fibArray[1] := 1;  { don't worry that n might be less than 2 }
 fibArray[2] := 1;
 while i <= n do
 begin
   fibArray[i] := fibArray[i-1] + fibArray[i-2];
   i := i + 1;
 end;
end;

begin
 writeln('enter an integer');
 readln(n);
 fib(A, n);
 writeln('fibonacci of ',n,' is: ');
 i := 1;
 while i <= n do
 begin
   writeln(A[i]);
   i := i + 1;
 end;
 readln;
end.


------------------------------------PROBLEM #4---------------------------------------------

No, not every Pascal program is an algorithm. All Pascal program meet the first few requirements because, to a Pascal Machine, Pascal operations are well ordered, unambiguous, and effectively computable. However, not all Pascal programs meet the last constraint. That is, some Pascal programs fail to halt in finite time because they contain an infinite loop, such as the following program:

program infiniteLoop;
var i : integer;
begin
  i := 1;
  while i = 1 do writeln('I am looping ... ');
end.


------------------------------------PROBLEM #5---------------------------------------------

Since we have dealt with array in class, we'll use four arrays to store our information:

We also need a variable to keep track of the number of unique CDs in the inventory (to know when to stop the loop.

To print out report a procedure called Print is used.
parameters: title, artist, recording_studio :stringarray, no_in_stock: integerarray, index:integer
Function: Given an index (location), it prints out the contents of all four arrays at that index.

To search, we define a procedure search.
Parameters: item_searched: string; array_searched: stringarray; position : integer;
Function: It goes through the array_searched and gets the position where it finds a match with item_searched. It then calls Print to output a report and does this for the entire array.

By calling search with different parameters (say title_of_CD, and the array title etc.) we can perform all the three searches required in this case.

------------------------------------PROBLEM #6---------------------------------------------

A Database Machine can be built "on top of" a Pascal Machine by writing a Pascal program that runs on the Pascal Nachine and implements the operations of the Database Machine. These operations might include : add-record, delete-record, search-for-record, and so on.

------------------------------------PROBLEM #7---------------------------------------------

program problem7;
type
  realArray = array [1..100] of real;
var
  Arr : realArray;
  n: integer;
  sum, avg : real;
begin
  readArray(Arr, n);
  sumArray(Arr, n, sum);
  avg := sum / n ;
  writeln('average is: ',avg);
  printArray(A, n);
end.


------------------------------------PROBLEM #8---------------------------------------------

program problem8;
type
  integerArray = array [1..100] of integer;
var
  Arr : integerArray;
  n: integer;
begin
 {Initialize the array to zeros}
 n := 1;
 while n <= 100 do
 begin
   A[n] := 0;
   n := n + 1;
 end;
 {enter integers and count their frequency}
 writeln('enter an integer between 1 and 100, or -1 to stop');
 readln(n);
 while n <> -1 do
 begin
   A[n] := A[n] + 1;
   writeln('enter an integer between 1 and 100, or -1 to stop');
   readln(n);
 end;
 {write out the frequency of integers that occurred more than once}
 n := 1;
 while n <= 100 do
 begin
   if A[n] > 1 then
      writeln(n, ' occurred ', A[n], ' times.');
   n := n + 1;
 end;
 readln;
end.


------------------------------------PROBLEM #9---------------------------------------------

A procedure's formal parameters may have different names than the actual parameters used in a call to the procedure. This is important for making the procedure modular and enabling its reuse. A program may use a procedure without knowing the names of the variables that the procedure uses. Also, the procedure may be used by many programs, each of which calls the procedure with different actual parameters.

------------------------------------PROBLEM #10---------------------------------------------

part a.

<sentence> -> <noun phrase>             <verb phrase>
                                |                               |
                                v                               v
                  <determiner><noun>      <verb><noun phrase>
                         |                |                 |              |
                         v                v                 v              v
                        the              girl             chases     <determiner><noun>
                                                                              |              |
                                                                              v              v
                                                                              a             ball


part b.

It is conceivable that a grammar checker could be written using such rules. The grammar checker would attempt to generate each sentence written with the word processor. Those sentences that can be generated using the rules would be deemed to be correct. Those sentences that could not be generated with the rules would be deemed incorrect.

However, the set of rules would be very large. A complete set of rules for English has not been developed.