CSCI 261 Programming Concepts (C++)

Fall/Winter 2011

Homework 21: Data Matrix

Important: You should download the project files here and extract the contents into your apps/homework folder.

Concepts

This assignment exercises your ability to think about and declare two-dimensional arrays.

Two-Dimensional Arrays

You should have witnessed some examples using two-dimensional arrays in class. Let's summarize what you've learned here.

int ratings[8]; // A one-dimensional array of 8 values
int reds[8][8]; // a two-dimensional array of 64 values
reds[0][5] = 100; // Location "0,5" in the array has the value 100
cout << reds[0][5]; // Prints the value in "0, 5" to the screen

It's absolutely fine to think of two-dimensional arrays as rows and columns. Furthermore, if we have N rows of M values, how can we loop over every NxM value?

const int ROWS = 8;
const int COLUMNS = 8;
int reds[ROWS][COLUMNS]; // declare an 8x8 array

for (int i = 0; i < ROWS; ++i) {
    for (int j = 0; j < COLUMNS; ++j) {
        reds[i][j] = someValue();
    }
}

What happened? We counted from 0 to 7. And each time we "counted" a number, we counted another number from 0 to 7. If nested for loops could talk to themselves, the loop above would say to itself, "0, 0-1-2-3-4-5-6-7. 1, 0-1-2-3-4-5-6-7. 2, 0-1-2-3-4-5-6-7 ... 7, 0-1-2-3-4-5-6-7. I really should stop talking to myself."

Notice how inside the for loop, the counter values are used to reference each location in the array. How exciting!

Passing Two-Dimensional Arrays to Functions

Passing two-dimensional arrays to functions seems like it should be no big deal, but the fact is that doing so comes with a little "baggage": we must declare the bounds for all dimensions except the first. In other words, we have to include the size of the "interior array" or "columns" when declaring our function headers. Consider the following:

void populateMatrix(int data[]);

This isn't correct, since we're telling the function that it should accept a one-dimensional array. "No duh, ours is two dimensional," you may think, and therefore try this:

void populateMatrix(int data[][]);

Which seems like it should work, but it does not. The compiler needs to be provided a size for the second dimension. So, you would declare the function like this:

void populateMatrix(int data[][10]);

Or better yet, assuming you have a global int variable called DATA_LENGTH:

void populateMatrix(int data[][DATA_LENGTH]);

Now, your compiler is happy and won't whine. At least, not about your function prototype.

Instructions

Important: You should download the project files here and extract the contents into your apps/homework folder.

Write a program that declares a two-dimensional array, populate it with random numbers (0 - 9), and then display the data in the array as a grid of numbers. It should also display the sum of values for each row.

An example interaction looks like this:

Hooray, arrays!
7 9 3 8 0 2 4 8 3 9 53
0 5 2 2 7 3 7 9 0 2 37
3 9 9 7 0 3 9 8 6 5 59
7 6 2 7 0 3 9 9 9 1 53
7 2 3 6 5 5 8 1 4 7 48
1 3 8 4 8 0 4 6 0 3 37
2 6 9 4 1 3 7 8 8 3 51
8 1 5 3 5 4 3 6 5 9 49
5 4 9 1 7 5 5 4 1 8 49
8 3 5 2 2 6 6 7 8 4 51

Your program should exhibit good style (spacing, etc). In addition, it should:

Requirements and Rubric

A friendly message from The Terminator, our grading program

*bzzzt* Hel-lo. You will be happy to know that I run on BSD Unix and not Windows. I once dated a fellow ro*bzzt*bot who ran Windows. It died after we made out. Lesson learned? *bzzt* Don't date a Windows-based robot.


Your program must generate a data matrix as displayed above.

I will check for a matrix consisting of numbers from 0 - 9.

I will check to make sure the totals are correct for each row.


It is a *bzzzt* pleasure serving you. Please come again.

This work is worth 115 points.

Requirement Points Notes
Place your name in the comment header in main.cpp 3
Correct submission of src directory as a .zip file. 2
Correctly declares global variable DATA_LENGTH 10
Correct implementation of populateMatrix 20 nested for loops!
Correct implementation of printMatrix 20 nested for loops!
main declares 2d array of appropriate size 10
main correctly calls populateMatrix 10
main correctly calls printMatrix 10
Correct display of matrix values and row totals 30

Concepts Exercised: collections of things (2d arrays), repeated tasks, I/O