I need help with finding the sum of only 1 row and 1 column in a 2D array. I have loops working inside int main() that show the sum of all the rows and all the columns but what I need is a function to sum only one row and one column and I haven't got a clue how to do it. I assume that I may be able to use the same for loops in the functions but I don't know how to make the functions to do that. I am new to C++ and programming so please keep that in mind. Thanks. Here is my code:
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
const int COLS = 5;
const int ROWS1 = 5;
void showArray(const int[][COLS], int);
int getTotal(const int [][COLS], int);
double getAverage (const int [][COLS], int);
int getRowTotal (const int [][COLS], int);
int getColumnTotal (const int[][COLS], int);
int main(int argc, char *argv[])
{
const int table1[ROWS1][COLS] = {{1, 20, 38, 41, 50},
{59, 64, 76, 80, 99},
{91, 103, 118, 125, 55},
{56, 21, 109, 53, 101},
{78, 50, 178, 155, 132}};
int total;
int total2;
cout << "the contents of table1 are:\n";
cout << endl;
showArray(table1, ROWS1);
cout << endl;
cout << "The sum of all the numbers in the table is:\n";
cout << endl;
cout << "SUM = " << getTotal(table1,ROWS1);
cout << endl;
cout << endl;
cout << "The average of all the number in the table is:\n";
cout << endl;
cout << fixed << showpoint << setprecision(2);
cout << "AVERAGE = " << getAverage(table1, ROWS1);
cout << endl;
for (int row = 0; row < COLS; row++)
{
int total = 0;
for (int col = 0; col < ROWS1; col++)
total += table1[row][col];
cout << "The sum of row " << (row + 1) << " is " << total << endl;
}
cout << endl;
for (int cols2 = 0; cols2 < ROWS1; cols2++)
{
total2 = 0;
for (int row2 = 0; row2 < COLS; row2++)
total2 += table1[row2][cols2];
cout << "The sum of column " << (cols2 + 1) << " is " << total2 << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
void showArray(const int array[][COLS], int rows)
{
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < rows; y++)
{
cout << setw(7) << array[x][y] << " ";
}
cout << endl;
}
}
int getTotal(const int array[][COLS], int rows)
{
int total = 0;
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < COLS; y++)
total += array[x][y];
}
return total;
}
double getAverage (const int array[][COLS], int rows)
{
double total = 0;
double average = 0;
int arrayNum = 25;
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < COLS; y++)
total += array[x][y];
average = total / arrayNum;
}
return average;
}