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
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.
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.
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.
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.
Since we have dealt with array in class, we'll use four arrays to store our information:
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.
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.
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 ballpart b.