Skip to content

Instantly share code, notes, and snippets.

@Vinnik67
Created February 12, 2025 17:57
Show Gist options
  • Select an option

  • Save Vinnik67/fc8b7cbb640443ef670edda7ad3bedb4 to your computer and use it in GitHub Desktop.

Select an option

Save Vinnik67/fc8b7cbb640443ef670edda7ad3bedb4 to your computer and use it in GitHub Desktop.
#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