-
Help in 2D C++......
Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void call(int array[5][6], const int i){
int colRowSum[6] = {0};
int colSumRow[5] = {0};
//sum to column row
for(int i = 0; i < 5; i++){
for( int j = 0; j < 6; j++){
colRowSum[j] += array[i][j];
colSumRow[i] += array[i][j];
}
}
//print array
for(int i = 0; i != 5; ++i){
for(int j = 0; j != 6; ++j){
cout.width(9);
cout << array[i][j];
}
cout.width(9);
cout << colSumRow[i];
cout << endl;
}
//print result
for(int n = 0; n != 6; ++n){
cout.width(9);
cout << colRowSum[n] <<" ";
}
for (int s = 0; s != 5; ++s){
cout.width(9);
cout <<colSumRow[s];
}
cout << endl;
}
int main(){
int array[ 5 ][ 6 ] = { { 141, 567, 434, 100, 269, 324, },
{ 578, 458, 562, 564, 305, 245, },
{ 381, 427, 561, 591, 595, 542, },
{ 427, 536, 491, 204, 502, 253, },
{ 392, 482, 521, 316, 318, 495, } };
call (array,5);
system("pause");
return 0;
}
here is the correct output...i want the program to be like this....
100 101 102 103 104 105 615
106 107 108 109 110 111 651
112 113 114 115 116 117 687
118 119 120 121 122 123 723
124 125 126 127 128 129 759
560 565 570 575 580 585 3435
i really need your help!>...
-
you really haven't done a very good job of explaining the problem. "help to align the numbers and remove the extra that doesn't belong" puts too much onus on the people you're asking to help to understand your issue.
Code:
//print array
for(int i = 0; i != 5; ++i){
for(int j = 0; j != 6; ++j){
cout.width(9);
cout << array[i][j];
}
cout.width(9);
cout << colSumRow[i];
cout << endl;
}
//print result
for(int n = 0; n != 6; ++n){
cout.width(9);
cout << colRowSum[n] <<" ";
}
for (int s = 0; s != 5; ++s){
cout.width(9);
cout <<colSumRow[s];
}
cout << endl;
istill dont' realyl know what your complaint is, but from what i can piece together, the highlighted lines above seems suspect.
-
Hey I get it now.....i solve the misunderstanding issue.....but there 's a problem....
this is my output but missing of the final sum of R and C....
100 101 102 103 104 105 615
106 107 108 109 110 111 651
112 113 114 115 116 117 687
118 119 120 121 122 123 723
124 125 126 127 128 129 759
560 565 570 575 580 585
100 101 102 103 104 105 615
106 107 108 109 110 111 651
112 113 114 115 116 117 687
118 119 120 121 122 123 723
124 125 126 127 128 129 759
560 565 570 575 580 585 3435 <-- how to get this sum of 3435 from R and C
Code:
#include <iostream>
using namespace std;
void call(int array[][6], const int i){
int colRowSum[6] = {0};
int colSumRow[5] = {0};
//sum to column row
for(int i = 0; i < 5; i++){
for( int j = 0; j < 6; j++){
colRowSum[j] += array[i][j];
colSumRow[i] += array[i][j];
}
}
//print array
for(int i = 0; i != 5; ++i){
for(int j = 0; j != 6; ++j){
cout.width(6);
cout << array[i][j]<<" ";
}
cout.width(6);
cout << colSumRow[i]<<" ";
cout << endl;
}
//print result
for(int n = 0; n != 6; ++n){
cout.width(6);
cout << colRowSum[n]<<" ";
}
for (int s = 0; s != 0; ++s){
cout.width(7);
cout <<colSumRow[s]<<" ";
}
cout << endl;
}
int main(){
int array[ 5 ][ 6 ] = { { 141, 567, 434, 100, 269, 324, },
{ 578, 458, 562, 564, 305, 245, },
{ 381, 427, 561, 591, 595, 542, },
{ 427, 536, 491, 204, 502, 253, },
{ 392, 482, 521, 316, 318, 495, } };
call (array,5);
cout<<endl;
system("pause");
return 0;
}
-
> 560 565 570 575 580 585 3435 <-- how to get this sum of 3435 from R and C
You add up either
- every element of the array (easy, you already have a loop doing this)
- all of colRowSum
- all of colSumRow