2014-02-16 4 views
1

내가 내 7 번째 행에있는 각 열을 요약하기 위해 노력하고있어하지만 난 같은 것을 표시하는 방법을 알아낼 수 없습니다 :이 테이블 필요추가 열

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 

열을 배열의 마지막 행까지 합계합니다. 이것은 내가 생각해 낸 것입니다.

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

답변

2

마지막 열까지 표시하고 있습니다. 당신은 단지

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

또한 첫 6 열을 원하는 경우, 루틴 추가에, a는

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); 
} 
제대로 인덱싱되지 않는