2017-05-05 48 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