// Problem: given a rectangular real matrix, compute the sums of their rows

#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

// Essential part of the code (this part must be written in exams):

typedef vector<double> realvector;
typedef vector<vector<double> > realmatrix;

realvector SumsOfRows(const realmatrix& a) {
  size_t j, k, m = a.size();
  size_t n = (m > 0) ? a[0].size() : 0;
  realvector result(m);
  for (j = 0; j < n; ++j)
    for (k = 0; k < n; ++k)
      result[j] += a[j][k];
  return result;
}

// Interface and test part (is not to be written in exams)

realmatrix MyRandomRealSquareMatrix(size_t n) {
  realmatrix result(n, realvector(n));
  srand(time(NULL));
  for (size_t j = 0; j < n; ++j)
    for (size_t k = 0; k < n; ++k)
      result[j][k] = (rand() % 10 - 5) + (rand() % 10) / 10.0;
  return result;
}

void OutputRealVector(const realvector& v) {
  size_t n = v.size();
  for (size_t j = 0; j < n; ++j)
      cout << setw(6) << v[j] << (j + 1 == n ? "\n" : " ");
}

void OutputRealSquareMatrix(const realmatrix& a) {
  size_t n = a.size();
  for (size_t j = 0; j < n; ++j)
    for (size_t k = 0; k < n; ++k)
      cout << setw(6) << a[j][k] << (k + 1 == n ? "\n" : " ");
}

int main() {
  realmatrix a = MyRandomRealSquareMatrix(4);
  realvector v = SumsOfRows(a);
  OutputRealSquareMatrix(a);
  cout << "Sums of rows:" << endl;
  OutputRealVector(v);
  return 0;  
}

