0

I am writing a program that accepts an input and i would like to process it in the below fashion:

Question:

input :

1 2 4 5 
3 4 5 6
7 2 4 5 1 2
1 2 4 3 4 5 5

I want to get the sum of the columns and the rows like”

sum of rows:

12
18
21
etc

sum of columns:

12
10
17
etc

And then I want to store them in an array for later use.

How do I achieve this?

My approach:

I have managed to get the sum of rows in the below manner:

istringstream iss(line);
   int i = 0;
   int sum = 0;

    while (iss >> i)
    {

        sum += i;
        numberOfColumns ++ ;

    }

I am stuck on how to get the sum of columns.

Assume: That the number of columns is fixed Note: This is not an academic exercise, this is an extension of this question: Sum all integers in a string C++

Thank you

Community
  • 1
  • 1
Mathew
  • 43
  • 5

2 Answers2

1

You could collect the input in a vector of vectors. Like:

vector<vector<int>> myData;

For each line of input, i.e. row, do:

vector<int> thisRow:
while (iss >> i)
{
    thisRow.push_back(i);
}
myData.push_back(thisRow);

Then you have all data available and you can calculate both row sum and column sum.

EDIT: As the question has changed to fixed number of columns, i.e. 7...

You could collect the input in a vector of arrays. Like:

vector<array<int,7>> myData;

For each line of input, i.e. row, do:

array<int, 7> thisRow {0, 0, 0 , 0, 0, 0 ,0};
int idx = 0;
while (iss >> i)
{
    thisRow[idx] = i;
    idx++;
    if (idx == 7) 
    {
      // Add code to clear the input stream
      // ...

      break;
  }
}
myData.push_back(thisRow);

Then you have all data available and you can calculate both row sum and column sum.

To calculate to row-sum:

for (auto x : myData)
{
    int sum = 0;
    for (int t = 0; t < 7; t++)
    {
        sum += x[t];
    } 
    cout << sum << endl;
}

To calculate to column-sum:

for (int t = 0; t < 7; t++)
{
    int sum = 0;
    for (auto x : myData)
    {
        sum += x[t];
    } 
    cout << sum << endl;
}

However, I would go for the vector of vector solution as it is much more flexible.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • If the size of the number of columns is fixed won't it be better to just use arrays? – Mathew Oct 15 '15 at 20:45
  • Maybe... but your question doesn't have a fixed number ofcolumns! Therefore vector is the way to go. – Support Ukraine Oct 15 '15 at 20:49
  • I have edited the question assume that the maximum number of columns is 7. The idea is more important than the solution so I have made the question simpler – Mathew Oct 15 '15 at 20:50
  • Still your input shows different number of columns. Anyway - I'll make an update... – Support Ukraine Oct 15 '15 at 20:56
  • @Mathew If you know both dimensions (rows and columns) and they will never ever change, you can use an array. If not, you have to use a dynamic array, and if you think getting a vector right is hard, just wait until you try to work with a 2D dynamic array. – user4581301 Oct 15 '15 at 22:41
  • 1
    Its not like i have to use an array, but i am relatively new to c++ so starting off slow. – Mathew Oct 15 '15 at 23:01
0

You can remember all in input values by storing them in a 2D array then using row major and column major iteration.

However, I believe you can do it all at once without remembering the input values. The row sums are easy since each line of input is an entire row. For the columns maintain a std::vector<int> which stores the running sum for each column then while processing each row track which column is being processed and update the std::vector<int> containing the column sums.

Note: This seems too much like an academic exercise to provide any code.

James Adkison
  • 9,412
  • 2
  • 29
  • 43
  • If I know the number of columns or if i have an upper bound on the columns, can the job be done in a simpler fashion? – Mathew Oct 15 '15 at 20:39
  • This is not an academic exercise, this is an extension of this question: http://stackoverflow.com/questions/17350624/sum-all-integers-in-a-string-c – Mathew Oct 15 '15 at 20:41
  • @Mathew what is difficult or unclear about either solution? – James Adkison Oct 15 '15 at 21:04