I cannot figure out why I am getting these warnings? Any help much appreciated.
Description:
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.)
The program should allow the user to run as many times as they like.
The input of the rows, columns, and the array should be done in a function
named GetData.
The determination of the maximum, and where it occurs should be done in
a function named CalcMax.
The output of the maximum and the location of that maximum should be done
in a function called PrintMax.
----------------------------
#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];
void GetData(int scores[][MAXCOLS], int &RowsUsed, int &ColsUsed)
{
cout<<"Please enter the number of rows:"<<endl;
cin>> RowsUsed;
cout<<"Please enter the number of columns:"<<endl;
cin>> ColsUsed;
for(int i = 0; i < RowsUsed; i++)
{
for(int j = 0; j < ColsUsed; j++)
{
cout<<"Please enter the 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 < RowsUsed; 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]++;
}
--------------------------
Warnings:
unused parameter 'ColsUsed' [-Wunused-parameter]
'ColsUsed' is used uninitialized in this function [-Wmaybe-uninitialized]
'RowsUsed' is used uninitialized in this function [-Wmaybe-uninitialized]
'total' may be used uninitialized in this function [-Wmaybe-uninitialized]
'Max' may be used uninitialized in this function [-Wmaybe-uninitialized]
'rownum' may be used uninitialized in this function [-Wmaybe-uninitialized]
------------------
When I run it as is, it will let me input the rows and columns, however I get the output of "a maximum of 0 occurs at row 0"...
In the CalcMax() function the Max and rownum will be unmodified since you are only passing a copy of them.
Unused parameter colused warning appears because you didnt use it but you passed it on calcmax function (i think the inner loop should be j < colsused
The uninitialized warning means you didnt initialized your variables on main function. Always initialize your variables so that they dont placed to random address with garbage values. Thats a good habit
Okay, so I fixed the j < colsused and that got rid of the first warning. However, I have the different variables declared up top as int... do I need to initialize them to a set number for those other warnings to go away? If so, how do I know what number?
I tried initializing them to zero and got the warnings to go away, however, when I run the program, it is still giving "A maximum of 0 occurs at row 0" rather than finding the correct information. Any idea why that might be?
Hey I am sorry- I meant to update that; I took the & out of that section. I am using Putty and compiling using fg++ prog5.cpp -o prog5 (prog5 being my code. I used jpico prog5.cpp to run it). Does that help?
Hey I am sorry- I meant to update that; I took the & out of that section. I am using Putty and compiling using fg++ prog5.cpp -o prog5 (prog5 being my code. I used jpico prog5.cpp to run it). Does that help?
Not really, just wandering if there is a compiler around who would allow it.
The problem was that you passed the vars as value and not as reference, so neither GetDate nor CalcMax could return their results.
This solves it.
1 2
void GetData(int scores[][MAXCOLS], int& RowsUsed, int& ColsUsed);
void CalcMax(int scores[][MAXCOLS], int RowsUsed, int ColsUsed, int &total, int &Max, int &rownum);
1 2 3 4 5 6 7 8 9
void GetData(int scores[][MAXCOLS], int& RowsUsed, int& ColsUsed)
{
// your code
}
void CalcMax(int scores[][MAXCOLS], int RowsUsed, int ColsUsed, int &total, int &Max, int &rownum)
{
// your code
}