Have to print the array on the screen Find the total sum of

Have to print the array on the screen. Find the total, sum of all the rows and sum of all the columns and print. c++ program.

SO FAR I HAVE:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
   const int NUM_ROWS = 4;
   const int NUM_COLS = 5;
   int total;
   //int num_array[NUM_ROWS][NUM_COLS];
   const int num_array[4] [5] = {
       {2, 4, 6, 8, 10},
       {12, 14, 16, 18, 20},
       {22, 24, 26, 28, 30},
       {32, 34, 38, 38, 40},
   };
   for (int index = 0; index < [4][5]; index++) {
       cout << num_array[index] << endl;
   }
   for (int row = 0; row < NUM_ROWS; row++) {
       total = 0;
      
       //Sum a row
       for (int col = 0; col < NUM_COLS; col++)
           total += num_array[row] [col];

       cout << (row + 1) << total << endl;

       }
   for (int col = 0; col < NUM_COLS; col++) {
       total = 0;

       //Sum a column
       for (int row = 0; row < NUM_ROWS; row++)
           total += num_array[row] [col];
       cout << (col + 1) << total << endl;
   }
}

Solution

// C++ total of 2d array and sum of each row and column

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    const int NUM_ROWS = 4;
    const int NUM_COLS = 5;
    int total = 0;


    //int num_array[NUM_ROWS][NUM_COLS];
    const int num_array[4] [5] =
    {
        {2, 4, 6, 8, 10},
        {12, 14, 16, 18, 20},
        {22, 24, 26, 28, 30},
        {32, 34, 38, 38, 40},
    };

    cout << \"2D Array:\ \";
    for (int i = 0; i < NUM_ROWS; i++)
    {
      for (int j = 0; j < NUM_COLS; ++j)
      {
        cout << num_array[i][j] << \" \";

      }
      cout << endl;
    }

    cout << endl << endl;

    for (int row = 0; row < NUM_ROWS; row++)
    {
      int row_sum = 0;
      
        for (int col = 0; col < NUM_COLS; col++)
        {
            total = total + num_array[row][col];
            row_sum = row_sum + num_array[row][col];
        }
        cout << \"Sum row \" << (row + 1) << \": \"<< row_sum << endl;
    }

    cout << endl << endl;

    for (int col = 0; col < NUM_COLS; col++)
    {
        int column_sum = 0;

        for (int row = 0; row < NUM_ROWS; row++)
        {
            column_sum = column_sum + num_array[row][col];
        }
        cout << \"Sum column \" << (col + 1) << \": \"<< column_sum << endl;
    }

    cout << endl << endl;

    cout << \"Total: \" << total << endl;
    return 0;
}


/*
output:

2D Array:
2 4 6 8 10
12 14 16 18 20
22 24 26 28 30
32 34 38 38 40


Sum row 1: 30
Sum row 2: 80
Sum row 3: 130
Sum row 4: 182


Sum column 1: 68
Sum column 2: 76
Sum column 3: 86
Sum column 4: 92
Sum column 5: 100


Total: 422
*/

Have to print the array on the screen. Find the total, sum of all the rows and sum of all the columns and print. c++ program. SO FAR I HAVE: #include <iostre
Have to print the array on the screen. Find the total, sum of all the rows and sum of all the columns and print. c++ program. SO FAR I HAVE: #include <iostre

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site