Assignment II

Due date question 1, Monday October 9. Programming assignment due October 16.


1. A bivariate polynomial is a sequence of terms, each of which is a monomial of the form Ax^n y^m where A is a floating point number, and n and m are integers. Each term is described by a record with three components. A polynomial is a list of such records.

a) In Ada and in your second favorite language , write the type declarations for a term and for a polynomial.

b) Write a function Evaluate, that takes a polynomial and two floating point numbers that represent specific values of x and y, and returns the value of the polynomial at that point.

c) Write a function Mult1, that takes a polynomial P1 and a term T1, and computes the product P1*T1, i.e. returns the list that represents this product. P1 and T1 are not affected by the computation.



2. We want to perform some simple editing on a piece of text. In particular, we want to locate the lines of text that contain a certain word, and replace this word with another. For this purpose, we will represent the text as a list of pointers to strings.

a) Write a procedure that reads a text file and constructs the list of lines.

b) Write a function Find, with two parameters Line and Word, which are pointers (access) to String. Find returns the first location within Line where Word is found. Find returns 0 if the word does not appear.

c) Write a function Replace (Line, Old_Word, New_Word, Pos). The first three are pointers to strings, the last is a positive number, indicating the position at which Old_Word appears in Line, The function returns a pointer to a string where Old_Word has been replaced with New_Word in the body of Line. Note that Old_Word and New_Word need not have the same length.

d) Put it all together: read a file, the old word, and the new word, and print the file after replacement.