Hey guys, I'm completely lost on what to do next, I'm writing a program that takes numbers, and sorts them into columns, then calculate the sum of the 4 separate columns. (Bottom part)
Now that part, I have figured out, what I'm having trouble with is that I also want to take those numbers, again, and calculate the sum of the rows (top part).
This is what I'm having trouble with I just can't seem to find a solution that works for me. (Dashes are cosmetically for the forum to align right, not in actual program)
25 -------- 14 -------- 18 --------- 12 - 69
15 -------- 19 -------- 48 --------- 58 - 140
22 -------- 36 -------- 15 --------- 88 - 161
22 -------- 25 -------- 18 --------- 08 - 73
24 -------- 13 -------- 17 --------- 15
15 -------- 19 -------- 48 --------- 58
22 -------- 36 -------- 15 --------- 88
22 -------- 25 -------- 18 --------- 08
------------------------------------------------
83----------93----------98----------169
I've got the array set up right I believe I just can't figure out how to sum all the numbers in the separate rows without getting a goofy answer.
Thanks for any help you have!
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
|
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int const row = 4, col = 4;
void print_array(double[][col]);
void total_cols(double[][col]);
void total_rows(double[][col]);
int main()
{
double num_arr[row][col]={{4.2,5.1,6.8},{5.5,4.7,8.2},{2.7,3.9,6.5}};
print_array(num_arr);
cout << endl;
total_cols(num_arr);
print_array(num_arr);
}
void print_array(double n[][col])
{
for(int i=0;i<row-1;i++)
{
for(int j=0;j<col;j++)
cout << setw(8) << n[i][j];
cout << endl;
}
for(int k=0;k<col;k++)
cout << setw(8) << "-------";
cout << endl;
for(int j=0;j<col;j++)
cout << setw(8) << n[row-1][j];
cout << endl;
}
void total_cols(double n[][col])
{
for(int j=0;j<col;j++)
for(int i=0;i<row-1;i++)
n[row-1][j]+=n[i][j];
}
void total_rows(double n[][col])
{
// Having Problems here
}
|