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;
}

The second parameter to getRowTotal() and getColumnTotal() is the row or column you need to work with. The code will be very similar to the one you did for getTotal(), except you only need one loop instead of two. For example, for getRowTotal() all you need to do is loop through each of the columns at the row number from the parameter.

Thanks for your reply. I have tried doing exactly what you said but I can't get it to work. The part that I am having a hard time with is how exactly to write the second parameter.

int getRowTotal (const int array[][0], int rows)
{
   int total = 0;
   int j;
   for (j = 0 ; j < COLS ; j++)
      total += array[rows][j];
   return total;
}

I think this may be what you are saying but I don't know how to call it in int main() so that it displays. I am not even sure if my function is correct. If it isn't too much trouble could you show me some code as an example?

The first parameter should be identical to that of all the other functions.

array[][COLS]

Otherwise that function looks ok. To call it from main() you will have to prompt for the row number, then call the function with the desired row number. Same for column number.

Thanks for your help. I changed the function and called it like this:

cout << getRowTotal(table1, 2);

My problem before was I was using [] around the row number.

I wanted to say thanks agian because I used what you showed me to make functions to find the highest and lowest numbers in one row as well.

int getHighestInRow (const int table1[][COLS],int rows)
{
    int highest = table1[ROWS1][COLS];
    for (int i = 0; i < ROWS1; i++)
    {
        if (table1[rows][i] > highest)
        highest = table1[rows][i];
    }
    return highest;
}

line 3 is incorrect because it is indexing beyond the bounds of the array. You should initialize highest as the first item in the table

int highest = table1[rows][0];

The second problem with that function is line 4. It should be i < COLS

Great! Thanks again, it's working good now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.