Hello BlueCOCO1,
My tutor sees nothing wrong and its compiling for them?
|
If your tutor sees nothing wrong with this code then get a new tutor.
To start with there are no "#include"s, so the program will not compile or run.
It is best not to use
using namespace std;
, but for now keep thinking it is really great until it gives you a problem.
The comment on line 7 is misleading.
Line 8 defines "rows" and "columns", but "total" is the only variable initialized.
Line 11 is defined as a "float". It would be better as a "double". Then you initialize it with an "int".
From C++11 on you have the uniform initializer, the {}s. Empty {}s the compiler will initialize the the variable with the proper type of zero based in the variables type. As an example:
double average{};
will set the vriable to "0.0".
ALWAYS initialize all your variables.
Since the VLAs have been discussed you could do something like this:
1 2 3 4 5 6 7 8 9
|
int main()
{
constexpr int MAXROWS{ 3 }, MAXCOLS{ 5 };
// initializing number of rows, columns and the total
int matrix[MAXROWS][MAXCOLS];
int rows{}, columns{}, total{}; // <--- ALWAYS initialize all your variables.
cout << " Enter the number of rows (maximum " << MAXROWS << "): ";
|
Then when you enter a value for "rows" and "columns". That would be the part of the array used.
Line 46 "rowAverage" is a misleading name maybe something like "rowTotal".
In line 60 type casting "rowAverage" to a float will always end in "?.000000". and instead of using (float) use "static_cast<double>(variableName)". The old C style cast works, but you should be using the newer C++ form.
It is good that you are making use of blank lines, but you are using to many.
Have a look at this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
using namespace std;
int main()
{
constexpr int MAXROWS{ 5 }, MAXCOLS{ 6 };
// intializing number of rows, coloumns and the total
int matrix[MAXROWS][MAXCOLS];
int rows{}, coloumns{}, total{}; // <--- ALWAYS initialize all your variables.
// variable for the average
float average = 0;
// asking user for the number of rows and coloumns
cout << " Enter the number of rows (maximum " << MAXROWS << "): ";
cin >> rows;
cout << " Enter the number of coloumns: ";
cin >> coloumns;
// intializing the 2D matrix array ie the table
//int matrix[rows][coloumns];
// looping through i and j for getting each value of the table
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < coloumns; j++)
{
cout << "Enter data for " << i << " " << j << ":";
cin >> matrix[i][j];
}
}
// looping through each element to find the row sum
for (int i = 0; i < rows; i++)
{
// resetting row variable for each row
int rowAverage = 0;
for (int j = 0; j < coloumns; j++)
{
// adding each value to the row and total
rowAverage += matrix[i][j];
total += matrix[i][j];
}
// printing the average of each row
cout << "Row Average is:" << (float) rowAverage / coloumns << "\n";
}
// printing the average of total table
cout << " Average of all data is " << (float) total / (rows * coloumns);
return 0; // <--- Not required, but makes a good break point for testing.
}
|
I did not change everything that is needed or have had a chance to compile and test yet.
Andy