Exercise 1
Write a program that computes and prints all the prime numbers below 100. Reminder: prime
numbers are integers greater than one with no factors other than
themselves and one.
Exercise 2
Write a program that accepts seat reservations and cancellations for a single performance in a theatre, accepting requests from the keyboard and printing information to the screen. The theatre has 50 seats, numbered from 0 to 49. The program should operate as follows:-
The program should loop, each loop performing the following steps:
Optional extra: when a reservation added, first check whether the seats can be reserved as a single contiguous block.
Exercise 3
Write a program to multiply a vector by a matrix. As an example, try to compute
(2 3 4) ( 8) (5 6 7) ( 9) (10)
for which the correct answer is
( 83) (164)
Use integer or floating point arithmetic as you prefer.
NOTE: write the program so that it is easy to change the size of the matrix & vector, i.e. use for loops, and #define or const declarations to specify the dimensions.
Exercise 4
Implement a doubly-linked list module using the Structures in the notes (see week 2 supplementary). Change the type of DataT to a string type able to hold strings up to 20 characters long. Rename IntLLT to StrLLT.
Implement the constructor and destructor functions
StrLLT Cons(void);
and
void Dest ( StrLLT list );
Add an additional function
void Add ( StrLLT list, char *s );
that adds a link to the start of the list with string s as its data. Hint: use malloc() to create the new link.
Then write a function
void PrintList ( StrLLT list );
to print all the strings contained in the list.
Write a small main() function to test your functions, such as
int main ( int argc, char *argv[] ) { StrLLT list; /* construct empty list of strings */ list = Cons(); /* add some strings to the list */ Add ( list, "Fluff" ); Add ( list, "badger" ); Add ( list, "boa constrictor" ); /* print contents of list */ PrintList ( list ); /* destroy list */ Dest ( list ); /* we've finished */ return 0; }
which should print out
boa constrictor badger Fluff