Computer Science 130
Programming Assignment 8 Hints

USE THE REQUIRED CODE!!! It really helps and the TA's will be a lot more willing if you use the base code, so be sure to use it.

The required code doesn't work and I hate you now!!!
This is a really dumb problem we discovered with the Microsoft compiler. The required code gives errors if it is in a file that has a name that ends with .c (like myprogram.c), but it works if the file ends with .cpp (like myprogram.cpp). Don't ask me why, because I don't know.

Help!!! I don't understand the required code.
I know. That's why we supplied it. Seriously, though, it's fairly annoying C code to write, and this program wasn't meant to be about input from a file. Here's an overview of what it does:
1.) It asks whether you want to enter data (a matrix) from a file.
2.a.) If yes, it asks for the file name, and makes sure the file is there.
2.b.) If no, it asks you to input the data.

All of the data is input into matrix, which is a 50x51 array of doubles. The actual size of the matrix (which is probably much smaller than 50x51), is stored in a variable called matrix_size, which is a double.

In other words, matrix and matrix_size are variables that you will be using in your program.

What is the Matrix?
No one can tell you what the Matrix is. You have to see it for yourself. (Never mind.)
Actually, a matrix is something we borrow from linear algebra--check the Lab 8 description for a pretty fair description of what we are trying to do with this matrix.

Why the heck would I want to solve a matrix?
Because it's the assignment, I guess. Actually, matrices are used to find the values of multiple variables given multiple equations. They're an important part of 3D Graphics work, too, although we're not doing any of that in here...

I don't get it. What am I trying to do here anyway?
The following will probably be insanely boring, but what I will try to do is illustrate (using a sample matrix) one way of doing this program. Mine is slightly different from the main program description in that I clear the numbers both above and below the diagonal at the same time rather than clearing the top first, then the bottom. Here goes nothing:

Original Matrix:
0.00 2.00 3.00 31.00
4.00 3.00 -2.00 26.00
10.00 -1.00 5.00 47.00

after swap--It had to swap to get the 0 off the diagonal: The diagonal is any space where the row is the same as the column--(0,0), (1,1), etc.
4.00 3.00 -2.00 26.00
0.00 2.00 3.00 31.00
10.00 -1.00 5.00 47.00

after divideThru--Now there's a 1 on the diagonal, and all values on the row are divided by whatever the old value on the diagonal was.
1.00 0.75 -0.50 6.50
0.00 2.00 3.00 31.00
10.00 -1.00 5.00 47.00

Clearing beneath diagonal--Nothing happens here because the space below the diagonal was already zero.
1.00 0.75 -0.50 6.50
0.00 2.00 3.00 31.00
10.00 -1.00 5.00 47.00

Clearing beneath diagonal--The bottom row has the space below the diagonal set to zero. This was done by subtracting (1*10) (1 was the value in the diagonal, and 10 was the value in the row that was beneath the diagonal). Also, -8.50 = -1.00 - (10*0.75), 10=5-(10*-.5), and -18=47-(10*6.5)
1.00 0.75 -0.50 6.50
0.00 2.00 3.00 31.00
0.00 -8.50 10.00-18.00

after swap--We check to see if a swap is needed, but there's no need as the diagonal is not zero
1.00 0.75 -0.50 6.50
0.00 2.00 3.00 31.00
0.00 -8.50 10.00-18.00

after divideThru--again, the diagonal is one, and you will notice that the numbers to the left of the diagonal will always be zero
1.00 0.75 -0.50 6.50
0.00 1.00 1.50 15.50
0.00 -8.50 10.00-18.00

Clearing beneath diagonal--we use the same division and subtraction as before, although this time the factor is -8.50
1.00 0.75 -0.50 6.50
0.00 1.00 1.50 15.50
0.00 0.00 22.75 113.75

Clearing above diagonal--you will notice that every non-diagonal space in the matrix in the completed columns is zero.
1.00 0.00 -1.63 -5.13
0.00 1.00 1.50 15.50
0.00 0.00 22.75 113.75

after swap--As you can only swap with rows above you, there will obviously be no swapping here. However, if the number in the diagonal were zero, it would be unsolvable.
1.00 0.00 -1.63 -5.13
0.00 1.00 1.50 15.50
0.00 0.00 22.75 113.75

after divideThru
1.00 0.00 -1.63 -5.13
0.00 1.00 1.50 15.50
0.00 0.00 1.00 5.00

Clearing above diagonal
1.00 0.00 0.00 3.00
0.00 1.00 1.50 15.50
0.00 0.00 1.00 5.00

Clearing above diagonal
1.00 0.00 0.00 3.00
0.00 1.00 0.00 8.00
0.00 0.00 1.00 5.00

all done
1.00 0.00 0.00 3.00
0.00 1.00 0.00 8.00
0.00 0.00 1.00 5.00

I don't know if this helps--I hope it does, but if it doesn't, hey, I tried.

How on the green earth did you do that?
Sorry, I can't give you the code... But I can tell you that you will want to use for loops. I used lots of them.

Return to Block 3 Assignment 1

Return to the Block 3 Index

Return to the Lab Index

Return to the CS 130 Homepage