C++ 在二维数组中添加列
我试图将每一列汇总到第7行,但我不知道如何显示以下内容:C++ 在二维数组中添加列,c++,arrays,multidimensional-array,dynamic-columns,C++,Arrays,Multidimensional Array,Dynamic Columns,我试图将每一列汇总到第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 57
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;
}
//库
#包括
#包括
#包括
#包括
使用名称空间std;
//全局常数
const int ROWS=7,COLS=7;
//功能原型
无效添加(int[][COLS]);
无效数据(int[][COLS]);
//行刑从这里开始
int main(int argc,字符**argv){
//声明变量
//填充数组
int数组[ROWS][COLS]={{100101102103104105,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}};
添加(数组);
系统(“暂停”);
返回0;
}
无效添加(int a[][COLS]){
cout您将一直显示到最后一列。如果您只需要前6列
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;
}
void prntAry(int a[][COLS]){
库特
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);
}