c++ My output is wrong and don\'t know to fix it the code. #include <iostream> u
ID: 3723768 • Letter: C
Question
c++
My output is wrong and don't know to fix it the code.
#include <iostream>
using namespace std;
const int Row = 50;
const int Col = 50;
int index = 1;
void getArray(int A[Row][Col], int row, int col);
void getTotal(int A[Row][Col], int row, int col, int &total);
void getAverage(int A[Row][Col], int row, int col, int total, int &average);
void getRowtotal(int A[Row][Col], int row, int col, int index, int &total);
void getColtotal(int A[Row][Col], int row, int col, int index, int &total);
void getHighestInRow(int A[Row][Col], int row, int col, int index, int &highestR);
void getHighestInCol(int A[Row][Col], int row, int col, int index, int &highestC);
void getLowestInRow(int A[Row][Col], int row, int col, int index, int &lowestR);
void getLowestInCol(int A[Row][Col], int row, int col, int index, int &lowestC);
void showArray(int A[Row][Col], int row, int col, int total, int average, int highestR, int highestC, int lowestR, int lowestC);
int main() {
int col, row, total, average, highestR, highestC, lowestR, lowestC;
int A[Row][Col];
cout << "Enter the rows: ";
cin >> row;
cout << "Enter the columns: ";
cin >> col;
getArray(A, row, col);
getTotal(A, row, col, total);
getAverage(A, row, col, total, average);
getRowtotal(A, row, col, index, total);
getColtotal(A, row, col, index, total);
getHighestInRow(A, row, col, index, highestR);
getHighestInCol(A, row, col, index, highestC);
getLowestInRow(A, row, col, index, lowestR);
getLowestInCol(A, row, col, index, lowestC);
showArray(A, row, col, total, average, highestR, highestC, lowestR, lowestC);
system("pause");
return 0;
}
void getArray(int A[Row][Col], int row, int col) {
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cout << "A [" << i << "][" << j << "]";
cin >> A[i][j];
}
}
}
void getTotal(int A[Row][Col], int row, int col, int &total) {
int i, j;
total = 0;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
total = total + A[i][j];
}
}
}
void getAverage(int A[Row][Col], int row, int col, int total, int &average) {
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
average = total / (row*col);
}
}
}
void getRowtotal(int A[Row][Col], int row, int col, int index, int &total) {
int i;
total = 0;
for (i = 0; i < col; i++) {
total += A[index][i];
}
}
void getColtotal(int A[Row][Col], int row, int col, int index, int &total) {
int i;
total = 0;
for (i = 0; i < row; i++) {
total += A[index][i];
}
}
void getHighestInRow(int A[Row][Col], int row, int col, int index, int &highestR) {
int i;
highestR = A[row][0];
for (i = 0; i < col; i++) {
if (A[index][i] > highestR) {
highestR = A[index][i];
}
}
}
void getHighestInCol(int A[Row][Col], int row, int col, int index, int &highestC) {
int i;
highestC = A[col][0];
for (i = 1; i < col; i++) {
if (A[index][i] > highestC) {
highestC = A[index][i];
}
}
}
void getLowestInRow(int A[Row][Col], int row, int col, int index, int &lowestR) {
int i;
lowestR = A[row][0];
for (i = 0; i < col; i++) {
if (A[index][i] < lowestR) {
lowestR = A[index][i];
}
}
}
void getLowestInCol(int A[Row][Col], int row, int col, int index, int &lowestC) {
int i;
lowestC = A[col][0];
for (i = 1; i < col; i++) {
if (A[index][i] < lowestC) {
lowestC = A[index][i];
}
}
}
void showArray(int A[Row][Col], int row, int col, int total, int average, int highestR, int highestC, int lowestR, int lowestC) {
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cout << A[i][j] << " ";
}
}cout << endl;
cout << "The total is " << total << endl;
cout << "The average is " << average << endl;
cout << "The highest number is the row is: " << highestR << endl;
cout << "The highest number in the column is: " << highestC << endl;
cout << "The lowest number in the row is: " << lowestR << endl;
cout << "The lowest number in the column is: " << lowestC << endl;
}
/*
Enter the rows: 3
Enter the columns: 2
A [0][0]8
A [0][1]2
A [1][0]5
A [1][1]3
A [2][0]4
A [2][1]6
8 2 5 3 4 6
The total is -858993452
The average is 4
The highest number is the row is: 5
The highest number in the column is: 4
The lowest number in the row is: -858993460
The lowest number in the column is: 3
Press any key to continue . . .
*/
Explanation / Answer
1. You have taken the same variable i.e. total for total, row total and colum total. Changing the variable name of row total to totalrow and colum total to totalcol fixed the issue with total.
2. for highest and lowest "index" row/column, in this case row A[1][0],A[1,1], A[1][2]... and column A[0,1], A[1,1], A[2][1].... you were taking the indices correctly. Please refer the following code for fixes
The code:
#include <iostream>
using namespace std;
const int Row = 50;
const int Col = 50;
int index = 1;
void getArray(int A[Row][Col], int row, int col);
void getTotal(int A[Row][Col], int row, int col, int &total);
void getAverage(int A[Row][Col], int row, int col, int total, int &average);
void getRowtotal(int A[Row][Col], int row, int col, int index, int &total);
void getColtotal(int A[Row][Col], int row, int col, int index, int &total);
void getHighestInRow(int A[Row][Col], int row, int col, int index, int &highestR);
void getHighestInCol(int A[Row][Col], int row, int col, int index, int &highestC);
void getLowestInRow(int A[Row][Col], int row, int col, int index, int &lowestR);
void getLowestInCol(int A[Row][Col], int row, int col, int index, int &lowestC);
void showArray(int A[Row][Col], int row, int col, int total, int average, int highestR, int highestC, int lowestR, int lowestC);
int main() {
int col, row, total,totalrow,totalcol, average, highestR, highestC, lowestR, lowestC;
int A[Row][Col];
cout << "Enter the rows: ";
cin >> row;
cout << "Enter the columns: ";
cin >> col;
getArray(A, row, col);
getTotal(A, row, col, total);
getAverage(A, row, col, total, average);
getRowtotal(A, row, col, index, totalrow);
getColtotal(A, row, col, index, totalcol);
getHighestInRow(A, row, col, index, highestR);
getHighestInCol(A, row, col, index, highestC);
getLowestInRow(A, row, col, index, lowestR);
getLowestInCol(A, row, col, index, lowestC);
showArray(A, row, col, total, average, highestR, highestC, lowestR, lowestC);
//system("pause");
return 0;
}
void getArray(int A[Row][Col], int row, int col) {
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cout << "A [" << i << "][" << j << "]";
cin >> A[i][j];
cout <<A[i][j] <<" ";
}
}
}
void getTotal(int A[Row][Col], int row, int col, int &total) {
int i, j;
total = 0;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
total = total + A[i][j];
}
}
}
void getAverage(int A[Row][Col], int row, int col, int total, int &average) {
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
average = total / (row*col);
}
}
}
void getRowtotal(int A[Row][Col], int row, int col, int index, int &totalrow) {
int i;
totalrow = 0;
for (i = 0; i < col; i++) {
totalrow += A[index][i];
}
}
void getColtotal(int A[Row][Col], int row, int col, int index, int &totalcol) {
int i;
totalcol = 0;
for (i = 0; i < row; i++) {
totalcol += A[index][i];
}
}
void getHighestInRow(int A[Row][Col], int row, int col, int index, int &highestR) {
int i;
highestR = A[index][0];
for (i = 1; i < col; i++) {
if (A[index][i] > highestR) {
highestR = A[index][i];
}
}
}
void getHighestInCol(int A[Row][Col], int row, int col, int index, int &highestC) {
int i;
highestC = A[0][index];
for (i = 0; i < row; i++) {
if (A[i][index] > highestC) {
highestC = A[i][index];
}
}
}
void getLowestInRow(int A[Row][Col], int row, int col, int index, int &lowestR) {
int i;
lowestR = A[index][0];
for (i = 0; i < col; i++) {
if (A[index][i] < lowestR) {
lowestR = A[index][i];
}
}
}
void getLowestInCol(int A[Row][Col], int row, int col, int index, int &lowestC) {
int i;
lowestC = A[0][index];
for (i = 0; i < row; i++) {
if (A[i][index] < lowestC) {
lowestC = A[i][index];
}
}
}
void showArray(int A[Row][Col], int row, int col, int total, int average, int highestR, int highestC, int lowestR, int lowestC) {
int i, j;
/* for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cout << A[i][j] << " ";
}
}cout << endl;
*/
cout << "The total is " << total << endl;
cout << "The average is " << average << endl;
cout << "The highest number in the row# "<< index << " is: " << highestR << endl;
cout << "The highest number in the column# "<< index << " is: " << highestC << endl;
cout << "The lowest number in the row# "<< index << " is: " << lowestR << endl;
cout << "The lowest number in the column# "<< index << " is: " << lowestC << endl;
}
/*
Enter the rows: 3
Enter the columns: 2
A [0][0]8
A [0][1]2
A [1][0]5
A [1][1]3
A [2][0]4
A [2][1]6
8 2 5 3 4 6
The total is -858993452
The average is 4
The highest number is the row is: 5
The highest number in the column is: 4
The lowest number in the row is: -858993460
The lowest number in the column is: 3
Press any key to continue . . .
*/
------------------------------
OUTPUT:
Enter the rows: Enter the columns: A [0][0]8
A [0][1]2
A [1][0]5
A [1][1]3
A [2][0]4
A [2][1]6
The total is 28
The average is 4
The highest number in the row# 1 is: 5
The highest number in the column# 1 is: 6
The lowest number in the row# 1 is: 3
The lowest number in the column# 1 is: 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.