数组表的总和

时间:2017-05-05 08:57:25

标签: c++

我能够在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
************************************

如何做这部分,可以帮助一些人。

1 个答案:

答案 0 :(得分: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;
   }
}