CSCI 1320 - Assignment #5


For this assignment I'm going to give you some fairly short and sweet problems that mainly test your ability to work with files. They will each also involve something with arrays, pointers, or pass-by-reference.

Basic Editor:

My first programming experience was with the BASIC programming language in the original line-numbered format. I want you to write a program that does the beginnings of a BASIC environment. There are only 5 "commands" you have to handle.

If the user types in a line that begins with a number, that should be seen as a line number for a line of code. You'll want to store the line and the number.

The "save <filename>" command will save the "program" to the specified file as text.

The "load <filename>" command should load in a "program" from the specified text file.

The "list" command should print the program to screen (very like save but to standard out instead of a file).

The "quit" command terminates the program.

When you list a program it should be displayed in sorted order by line number. See the sample input and output for examples. No line will be more than 80 characters and you only have to handle 100 lines at most. In addition to atoi, you might find the strcmp and strcpy functions helpful for this assignment. Use man to see what they do. You can use gets for reading full lines or use fgets and pass it stdin if you don't want to see warnings.

input : output : saved file

Matrix Math:

This problem will have you playing with some matrices. It will lead into code later on that will solve the chemical equations if you did the chemistry option in assignment #4. Your program will handle two NxN matrices, we'll call them A and B, and it should have the following menu options. The matrices will never be bigger than 10x10.

1. Enter matrix size.

2. Enter matrix A.

3. Enter matrix B.

4. Write matrices to file.

5. Read matrices from file.

6. Print sum of matrices.

7. Print product of matrices.

8. Quit

The output file should be a text file where the first line has the matrix size, N. that is followed by N lines, each with N numbers giving the A matrix and after that are N lines with N numbers giving the B matrix.

input : output : saved file

Graphics:

Here we will play with lighting. This assignment doesn't depend on the earlier graphics options, but we are now getting close to having all the pieces to put together so that we can do a full ray tracing program. You will need to determine the intensity of lighting on each of the tree corners of a triangle given a single light source. Your program only had to deal with one triangle and one light source at a time. The menu for the program will look like this.

1. Set light location.

2. Set triangle corners.

3. Write geometry to file.

4. Read geometry from file.

5. Print intensity at corners.

6. Quit

To determine the intensity of the light on a point of a triangle you need functions to do both cross products and dot products as well as distance between points. The intensity at a point (P) from a single light source (L), where P and L are points in 3-D space, is given by the dot product of the vector from P to L with the unit normal at P (n), divided by the cube of the distance (or you can make the separation a unit vector and divide by the square). The normal of a triangle can be easily found by taking the cross product of the vectors along two legs of the triangle. You need to divide all components of that by the length of the vector to get the unit normal (a normal vector of length 1). If you don't remember how to do dot and cross products, ask in class. Assume all three points have the same normal. Report the absolute value of the intensity. The dot product can give you a negative, but that only matters if you assume the triangle is single sided. We won't assume that here.

The text file should have four lines each with 3 numbers. The first line is the position of the light (lx,ly,lz) and the other lines are the positions of the three points of the triangle.

input : output : saved file