Assignments

Assignment 1:


Assignment 2:

Expand on your program from Assignment1.

Assignment 3:

Write a function to add matrices.

Assignment 4:

Write a function SwitchRows(matrix*,short, short) which takes a matrix and two row numbers and switches the two rows. Write a similar function SwitchCols(matrix*,short, short) which switches two columns.

Assignment 5:

Copy NumToString.c and NumToString.h from the Web-page to your math211hw directory.

Assignment 6:

Go to your math211hw directory and type A6 and then hit enter. You should get a new directory called A6 in your space. If you already have a directory called A6 here, please rename it. Once you have this A6 directory, please do not move it. Inside A6 will be five files. You will need to add code to the files main.c and Qsort.c to get a working program. To compile your program use
cc main.c Qsort.c LibMatrix.c
To test your program, use a.out < t_file

The file t_file is a file of matrices so ReadMatrix can be used to read the file. Read the file into memory and sort the matrices according to the rule below. Then print out a list of numbers separated by spaces. At the end of the list, print a newline so the prompt appears immediately below your list of numbers. The first number is the number in the file of the "smallest" matrix, the next number is the number of the next smallest, etc. The first matrix in the file is number 1.

Given two matrices, matrix1 is less than matrix2 provided

This assignment is due at midnight on Thursday, April 9.

Assignment 7:

Change the word count program to handle upper and lower case together. Get each word and check if it is already on the list. If it is, increment the upper case count if the word is upper case. If the word is lower case, increment the lower case count. Use the file "tree.c" from last Wednesday. This file can be copied from the web site. You will need to modify the structure and the routines that add a node and print the tree. You will also need to fix the string-copy function to store the word as lower case and you will need to write a new function to replace strcmp. As for printing, print your data as follows. Each line should begin with a word in lower case. Follow the word by a space and then an integer giving the number of times the word appeared in lower case, another space and finally the number of times the word appeared in upper case. Print _ if one of the cases is 0 rather than printing 0.
Name the file "tree.c".
This assignment is due at midnight on Tuesday, April 21.

Assignment 8:


Your last assignment is to write a function which balances trees. You are to write a function
struct tnode *balance(struct tnode*);
which takes a pointer to the root of a tree and returns a pointer to a possibly new root for the same tree with the pointers changed so that the tree is balanced. Call the file
balance.c
and leave it in your math211hw directory. Declare the structure "struct tnode" in a header file,
balance.h
and leave it in your math211hw directory as well. You may setup any node structure you like but you must have pointers left and right to the two branches. As a practical matter, you probably want to use the structure we've been using lately in class so you can test your function with a minimum of additional work, but suit yourself.

To explain "balanced" requires two notions. The first point is that a binary tree orders the data: everything down the left node comes before the current node; then comes the current node; and finally comes everything down the right node. The function treeprint is an example: it prints the nodes in this order.

There is also the notion of the height of a tree and the level of a node. Level is defined inductively: the root has level 0 and if a node has level n then both the right and left nodes have level n+1 (unless they are NULL). The height of a tree is the maximum level of any node. Notice by definition, if there are x nodes of level n, then there are at most 2x nodes of level n+1.

Two trees are equivalent if they induce the same order on the nodes. A "balanced" tree is a tree with the minimal height for the total number of nodes. It will have 1 node of level 0, 2 nodes of level 1, etc. down to 2^{n-1} nodes of level n-1 and finally it will have however many nodes of level n it needs to get the total right.

It is a theorem that any tree is equivalent to a balanced tree and the point of the exercise is to write a function that finds a balanced tree for a given tree.

Your function should work as follows. It should begin with a pointer to the root of the given tree and should return a pointer to the root of your balanced tree. It should also CHANGE the left&right pointers in the given tree so that the end result is balanced. Your function should make no assumptions about what kind of data is being organized, merely assume that if p points to a node, p->left and p->right point to the left and right nodes below p.

This assignment constitutes the take-home part of the final. It is due at the start of the final, Friday May 8 at 8am. I will do a pick-up on Monday, May 4 at 8am and if you have successfully completed the assignment I will let you know.
Back.