2-d Array and Functions

The question is...
Your program should ask the user for data to (partially) fill a 2-d array.
Ask them how many rows, how many columns, and then ask for the appropriate
number of values to fill the array.

You will then total each row in the array and determine which row has the
largest total and what that total is.

Then you will output the maximum and the row number the maximum occured in.
(The row number should be output in terms that a non-programmer would
understand -- if it's in the 1st row -- the one with subscript 0 -- output
it as row 1.)

This program has to be done using functions. This is what I have so far.

#include <iostream>
using namespace std;

const int MAXROWS = 100;
const int MAXCOLS = 100;

void GetData(int scores[][MAXCOLS], int RowsUsed, int ColsUsed);
void CalcMax(int scores[][MAXCOLS], int RowsUsed, int ColsUsed, int total, int Max, int rownum);
void PrintMax(int scores[][MAXCOLS], int Max, int rownum);

int main()
{
int RowsUsed;
int ColsUsed;
int total;
int Max;
int rownum;
int scores[MAXROWS][MAXCOLS];

GetData(scores, RowsUsed, ColsUsed);
CalcMax(scores, RowsUsed, ColsUsed, total, Max, rownum);
PrintMax(scores, Max, rownum);

return 0;
}

void GetData(int scores[][MAXCOLS], int RowsUsed, int ColsUsed)
{
cout << "How many rows would you like?" << endl;
cin >> RowsUsed;
cout << "How many columns would you like?" << endl;
cin >> ColsUsed;

for(int i = 0; i < RowsUsed; i++)
{
for(int j = 0; j < ColsUsed; j++)
{
cout << "Enter number for row " << i + 1 << " and column "
<< j + 1 << endl;
cin >> scores[i][j];
}
}
}
void CalcMax(int scores[][MAXCOLS], int RowsUsed, int ColsUsed, int total, int Max, int rownum)
{
for (int i = 0; i < RowsUsed; i++)
{
total = 0;
for (int j = 0; j < ColsUsed; j++)
{
total += scores[i][j];
}
if(total > Max)
{
Max = total;
rownum++;
}
}
}
void PrintMax(int scores[][MAXCOLS], int Max, int rownum)
{
cout << "A maximum of " << Max << " occurs at row " << rownum << endl;
scores[MAXROWS][MAXCOLS]++;
}

I am getting these errors and I don't know how to fix them.
program05.cpp: In function int main():
program05.cpp:33:39: warning: RowsUsed is used uninitialized in this function [-Wuninitialized]
program05.cpp:33:39: warning: ColsUsed is used uninitialized in this function [-Wuninitialized]
program05.cpp:34:59: warning: total is used uninitialized in this function [-Wuninitialized]
program05.cpp:34:59: warning: Max is used uninitialized in this function [-Wuninitialized]
program05.cpp:34:59: warning: rownum is used uninitialized in this function [-Wuninitialized]

I need help, please.
You pass uninitialized variables as arguments to the functions.
How can I fix that? I just started learning about this stuff.
You should write at first one function GetData and test it. In my opinion it should be declared as


void GetData( int scores[][MAXCOLS], int &RowsUsed, int &ColsUsed );
That worked! Thank you so much.
This worked because he passed by reference the variable addresses for RowsUsed and ColsUsed. Meaning that the same memory address that variables declared and initialized in the the main function is used when the function uses them. So anything that you do the the variable inside the function is done to the variables in the main function. Could be good or bad depending on what you are trying to do. Also I agree with his statement to write one function at a time and test before you continue on to the next function. It will save you a lot of grief.
Edit : Glaring Typo.
Last edited on
Topic archived. No new replies allowed.