1

I'm trying to sum up each column I have to my 7th row but I can't figure out how to display something like:

   100   101   102   103   104   105   
   106   107   108   109   110   111  
   112   113   114   115   116   117   
   118   119   120   121   122   123  
   124   125   126   127   128   131   
   560   565   570   575   580   587 

The columns of this table need to sum up to the last row of the array. This is what I came up with:

//Libraries
#include<ctime>
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
//Global constants
   const int ROWS=7, COLS=7;
//function prototypes
void add(int [][COLS]);
void prntAry(int [][COLS]);
//execution begins here
int main(int argc, char** argv){
   //Declare Variables

   //fill array
   int array[ROWS][COLS]={{100,101,102,103,104,105,0},                            
                          {106,107,108,109,110,111,0},
                          {112,113,114,115,116,117,0},
                          {118,119,120,121,122,123,0},
                          {124,125,126,127,128,131,0},
                          {0,0,0,0,0,0}};

    add(array);
    system("Pause");
   return 0;
}
void add(int a[][COLS]){
    cout<<endl;
    int i=0;
    for(i;i<ROWS;i++)
        for(int row=0;row<ROWS;row++){
        a[i][7]+=a[i][row];
    }
        prntAry(a);
}
void prntAry(int a[][COLS]){
    cout<<endl;
    for(int row=0;row<ROWS;row++){
        for(int col=0;col<COLS;col++){
            cout<<setw(4)<<a[row][col];
        }
        cout<<endl;
    }
    cout<<endl;
}

1 Answer 1

2

You are displaying until the last column. If you only want the first 6 columns

void prntAry(int a[][COLS]){
    cout<<endl;
    int lastcol = COLS - 1;
    for(int row=0;row<ROWS;row++){
        for(int col=0;col<lastcol;col++){
            cout<<setw(4)<<a[row][col];
        }
        cout<<endl;
    }
    cout<<endl;
}

Also, in the routine add, a is not being indexed correctly

void add(int a[][COLS]){
    cout << endl;
    int lastcol = COL - 1;
    for (int row = 0; row < ROWS; row++) {
       for (int col = 0; col < lastcol; ++col)
          a[row][lastcol] += a[row][col];
    }
    prntAry(a);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.