2017-05-05 141 views
-2

我能够与代码打印阵列的列表在2D见下表萨姆数组表

#include <iostream> 
#include <vector> 
using namespace std; 

void inputRoutine(vector<int> &a) 
{ 
    const int MAXNUM = 17; 
    for (int i = 1; i <= MAXNUM; i++) a.push_back(i); 
} 


void printRoutine(vector<int> a) 
{ 
    const int COLS = 5; 
    int size = a.size();           
    int fullrows = size/COLS;         
    int leftover = size % COLS;         
    int rows = fullrows + (leftover != 0);      

    cout << "TABLE:\n"; 
    for (int i = 0; i < rows; i++)        
    { 
     int indexTop = 0;           
     for (int j = 0; j < COLS; j++) 
     { 
     int index = indexTop + i;        
     if (i < fullrows || j < leftover) cout << a[index]; 
     cout << '\t';           
     if (j < leftover) indexTop += rows;     top-of-column 1-d index for the number in this column 
     else    indexTop += fullrows;    
     } 
     cout << '\n'; 
    } 
} 


int main() 
{ 
    vector<int> a; 
    inputRoutine(a); 
    printRoutine(a); 
} 

和我能够产生低于

TABLE: 1 5 9 12 15 
     2 6 10 13 16 
     3 7 11 14 17 
     4 8 

输出作为现在我想要得到打印表格的总和如下 (第一个数字是列号,第一行的第5列表示单独的行总数42,47,52,12,如表中所给出的,第二行列号1:如果只有1列,那么有17行 - 它应该是亲达斯总数)

C ROW SUMS 

5 42 47 52 12 
************************************ 
1 153 
************************t*********** 
2 11 13 15 17 19 21 23 25 9 
************************************ 

如何做到这一点,可以有人帮助。

+0

42 = 1 + 5 + 9 + 12 + 15 52 = 3 + 7 + 11 + 14 + 17 47 = 2 + 6 + 10 + 13 + 16 12 = 4 + 8 – Eranka

+0

它应该运行通过'COLS'的所有可能的选择或者只有一个有限集合,例如'1 Jonas

+0

当选择第5列时应计算每行的总和,并且当选择第1列时,应计算1到17的总和 – Eranka

回答

0

我会这样做的方式,将有一个缓冲区row_sum这是这种情况下填充(和计算)打印表时。 打印完表格后,row_sum包含计算的行数总和。您可以将COLs设为printRoutine的参数,以便可以在不重新编译的情况下运行不同的COLS值。

Here是您原始代码的简单扩展,运行COLS = 1,...,5。对于每个值COLS它都会打印表格和行数。

#include <iostream> 
#include <vector> 
#include <numeric> 
using namespace std; 

void inputRoutine(vector<int> &a) 
{ 
    const int MAXNUM = 17; 
    for (int i = 1; i <= MAXNUM; i++) a.push_back(i); 
} 

void printRoutine(int COLS, vector<int> a, vector<int>& row_sum) 
{ 
    int size = a.size();           
    int fullrows = size/COLS;         
    int leftover = size % COLS;         
    int rows = fullrows + (leftover != 0);      

    // Set the size of the row_sum buffer 
    row_sum.resize(rows); 

    cout << "TABLE:\n"; 
    for (int i = 0; i < rows; i++)        
    { 
     int indexTop = 0;           
     for (int j = 0; j < COLS; j++) 
     { 
     int index = indexTop + i;        
     if (i < fullrows || j < leftover) 
     { 
      cout << a[index]; 

      // Add the value to the row sum. We do the add here, since 
      // if it should be printed it should also be added 
      row_sum[i] += a[index]; 
     } 
     cout << '\t';           
     if (j < leftover) indexTop += rows; // top-of-column 1-d index for the number in this column 
     else    indexTop += fullrows;    
     } 
     cout << '\n'; 
    } 

    // Special case for COLS == 1 
    if (COLS == 1) 
    { 
     row_sum[0] = std::accumulate(std::begin(row_sum), std::end(row_sum), 0); 
     row_sum.resize(1); 
    } 
} 

int main() 
{ 
    vector<int> a; 
    inputRoutine(a); 

    for (int COLS = 1; COLS < 6; ++COLS) // Here we loop over a few selected value of COLS 
    { 
     vector<int> row_sum; // To store the row sums 
     printRoutine(COLS, a, row_sum); 

     // The rest of the loop is just printing the row sums 
     std::cout << '\n' << COLS << '\t'; 
     for (int i : row_sum) 
     std::cout << i << ' '; 
     std::cout << std::endl << std::endl; 
    } 
} 
+0

乔纳斯根据您的代码,它显示了总和的5个单独表格,但是我的要求是得到5,1,2列在一张表中的总和,不是由单独的表格中的星号分隔 – Eranka

+0

@Eranka如果你只想要'COLS'的那些值,那么就改变for循环:for(int COLS = 1; COLS < 6; ++ COLS)' – Jonas

+0

@Eranka如果你不想打印表格只是评论它。 – Jonas