fork(2) download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. int main(int argc, const char* argv[]) {
  5. int total_rows, total_cols;
  6.  
  7. std::cout << "Please enter number of rows: ";
  8. std::cin >> total_rows;
  9.  
  10. std::cout << "Please enter number of columns: ";
  11. std::cin >> total_cols;
  12.  
  13. int* matrix = new int[total_rows*total_cols];
  14.  
  15. std::cout << "Initialize matrix: \n";
  16.  
  17. for (int row = 0; row < total_rows; row++) {
  18. for (int col = 0; col < total_cols; col++) {
  19. std::cin >> matrix[col + row*total_rows];
  20. }
  21. }
  22.  
  23. std::cout << "Matrix:\n";
  24.  
  25. for (int row = 0; row < total_rows; row++) {
  26. for (int col = 0; col < total_cols; col++) {
  27. std::cout << std::setw(3) << matrix[col + row*total_rows];
  28. }
  29. std::cout << "\n";
  30. }
  31.  
  32. int row_number;
  33. std::cout << "\nEnter row number (zero-based) to calculate sum for: ";
  34. std::cin >> row_number;
  35.  
  36. int sum = 0;
  37. for (int col = 0; col < total_cols; col++) {
  38. sum += matrix[col + row_number*total_cols];
  39. }
  40.  
  41. std::cout << "Sum = " << sum << "\n";
  42.  
  43. delete[] matrix;
  44. }
Success #stdin #stdout 0s 3232KB
stdin
4 4

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

2
stdout
Please enter number of rows: Please enter number of columns: Initialize matrix: 
Matrix:
  1  2  3  4
  5  6  7  8
  9 10 11 12
 13 14 15 16

Enter row number (zero-based) to calculate sum for: Sum = 42