1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <iostream>
#include <iomanip>
using namespace std;
const int NUM_ROWS = 3;
const int NUM_COLS = 5;
int sales[NUM_ROWS][NUM_COLS] =
{
{ 88, 97, 79, 86, 94 },
{ 86, 91, 78, 99, 82 },
{ 84, 78, 81, 77, 93 } };
int rowsum[NUM_ROWS];
int colsum[NUM_COLS];
int Rowsum(int arr[][NUM_COLS], int[]);
int Colsum(int arr[][NUM_COLS], int[]);
void transpose(int arr[][NUM_COLS], int);
int main()
{
cout << "The transpose of the matrix is" << endl;
transpose(sales, NUM_COLS);
cout << endl;
rowsum[NUM_ROWS] = Rowsum(sales, rowsum);
for (int i = 0; i < NUM_ROWS; i++)
{
for (int j = 0; j < NUM_COLS; j++)
cout << setw(5) << sales[i][j];
cout << "|" << setw(5) << Rowsum(sales, rowsum) << endl;
}
for (int j = 0; j < NUM_COLS; j++)
cout << "---" << setw(5) << Colsum(sales, colsum) << endl;
return 0;
}
int Rowsum(int arr[][NUM_COLS], int rsum[])
{
for (int row = 0; row < NUM_ROWS; row++)
{
int total = 0;
for (int col = 0; col < NUM_COLS; col++)
total += arr[row][col];
return total; // You are returning before the loop has ended
}
// Do you want to return here instead?
}
int Colsum(int arr[][NUM_COLS], int csum[])
{
for (int col = 0; col < NUM_COLS; col++)
{
int total = 0;
for (int row = 0; row < NUM_ROWS; row++)
total += arr[row][col];
return total; // You are returning before the loop has ended
}
// Do you want to return here instead?
}
void transpose(int arr[][NUM_COLS], int size)
{
for (int row = 0; row < size; row++)
{
for (int cols = 0; cols < NUM_ROWS; cols++)
{
cout << setw(5) << arr[cols][row] << " ";
}
cout << endl;
}
}
|