-
-
Save NV-Valentine/d03e48e18ac7f782b540df062f5b1725 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| void calculateSums(int matrix[3][4], int rowCount, int colCount, int rowSums[], int colSums[], int& totalSum) | |
| { | |
| totalSum = 0; | |
| for (int i = 0; i < rowCount; ++i) | |
| { | |
| for (int j = 0; j < colCount; ++j) | |
| { | |
| rowSums[i] += matrix[i][j]; | |
| colSums[j] += matrix[i][j]; | |
| totalSum += matrix[i][j]; | |
| } | |
| } | |
| } | |
| void printRowSums(int matrix[3][4], int rowCount, int colCount, int rowSums[]) | |
| { | |
| for (int i = 0; i < rowCount; ++i) | |
| { | |
| for (int j = 0; j < colCount; ++j) | |
| { | |
| std::cout << matrix[i][j] << " "; | |
| } | |
| std::cout << "| " << rowSums[i] << std::endl; | |
| } | |
| } | |
| void printSeparatorLine(int colCount) | |
| { | |
| for (int j = 0; j < colCount; ++j) | |
| { | |
| std::cout << "---"; | |
| } | |
| std::cout << "---" << std::endl; | |
| } | |
| void printColumnSums(int colCount, int colSums[], int totalSum) | |
| { | |
| for (int j = 0; j < colCount; ++j) | |
| { | |
| std::cout << colSums[j] << " "; | |
| } | |
| std::cout << "| " << totalSum << std::endl; | |
| } | |
| int main() | |
| { | |
| int matrix[3][4] = | |
| { | |
| {3, 5, 6, 7}, | |
| {12, 1, 1, 1}, | |
| {0, 7, 12, 1} | |
| }; | |
| int rowCount = 3; | |
| int colCount = 4; | |
| int rowSums[3] = { 0 }; | |
| int colSums[4] = { 0 }; | |
| int totalSum = 0; | |
| calculateSums(matrix, rowCount, colCount, rowSums, colSums, totalSum); | |
| printRowSums(matrix, rowCount, colCount, rowSums); | |
| printSeparatorLine(colCount); | |
| printColumnSums(colCount, colSums, totalSum); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment