fork download
  1. #include <iostream>
  2.  
  3. void calculateSums(int*, unsigned, unsigned);
  4.  
  5. int main()
  6. {
  7. int t[12] = { 1, 4, 2, 6,
  8. 2, 5, 3, 3,
  9. 8, 7, 4, 6 };
  10. calculateSums(t, 3, 4);
  11.  
  12. return 0;
  13. }
  14.  
  15. void calculateSums(int* t, unsigned rowCount, unsigned columnCount)
  16. {
  17. unsigned rowSum = 0, counter = 1;
  18. unsigned rowDivisor = rowCount;
  19. for(unsigned i = 0 ; i < rowCount * columnCount ; ++i) {
  20. rowSum += t[i];
  21. if(!rowDivisor--) {
  22. std::cout << "suma " << counter << " wiersza: " << rowSum << std::endl;
  23. rowSum = 0;
  24. ++counter;
  25. rowDivisor = rowCount;
  26. }
  27. }
  28.  
  29. unsigned columnSum = 0;
  30. counter = 1;
  31. for(unsigned k = 0 ; k < columnCount ; ++k) {
  32. for(unsigned j = 0 ; j < rowCount * columnCount ; j += columnCount)
  33. columnSum += t[k + j];
  34.  
  35. std::cout << "suma " << counter << " kolumny: " << columnSum << std::endl;
  36. columnSum = 0;
  37. ++counter;
  38. }
  39. }
  40.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
suma 1 wiersza: 13
suma 2 wiersza: 13
suma 3 wiersza: 25
suma 1 kolumny: 11
suma 2 kolumny: 16
suma 3 kolumny: 9
suma 4 kolumny: 15