-
add 2D array values
hi everyone!
i have a problem with 2D array and tried this for 2 days.
array is m*n and want to add values. explained in following example.
1 2 3
4 5 6
7 8 9
10 11 12
outputs:
1 + 4 + 7 + 10
1 + 4 + 7 + 11
1 + 4 + 7 + 12
1 + 4 + 8 + 10
1 + 4 + 8 + 11
1 + 4 + 8 + 12
1 + 4 + 9 + 10
1 + 4 + 9 + 11
1 + 4 + 9 + 12
1 + 5 + 7 + 10
1 + 5 + 7 + 11
1 + 5 + 7 + 12
1 + 5 + 8 + 10
1 + 5 + 8 + 11
1 + 5 + 8 + 12
1 + 5 + 9 + 10
1 + 5 + 9 + 11
1 + 5 + 9 + 12
1 + 6 + 7 + 10
1 + 6 + 7 + 11
1 + 6 + 7 + 12
1 + 6 + 8 + 10
1 + 6 + 8 + 11
1 + 6 + 8 + 12
1 + 6 + 9 + 10
1 + 6 + 9 + 11
1 + 6 + 9 + 12
and in case of 2 and 3, add sequence are same as 1
thanks
-
So post your last best attempt at the solution.
-
i stopped the program it always resulted wrong
it is making me crzay
Code:
int a[4][4]={{1,2,3,4}, //i mor
{5,6,7,8}, //j bagana
{9,10,11,12},
{13,14,15,16}};
int i1;
int j1;
int i=0,j=0,b[5][5],r,chose,sanakh=4,q,too=0,ber=0, save=0,horg=3,qwe=0,ewq=0;
int sum[1000];
for(int l = 0; l < 1000; l++)
sum[l] = 0;
// while(1==1){
// for(j=0;j<=3;j++)
// {
for(r=j;r<=3;r++)
{ for(q=0;q<=16;q++)
{ save=0; top: for(i=0;i<=3;i++)
{
if(i<horg ) {save=save+a[i][r]; }
if(horg==i && qwe==ewq) {
for(int z=0;z<=3;z++)
{
sum[ber]=save+a[i][z];ber++;too++;
qwe++;
}
save=0;
horg--;
}
else if (horg==i) { sanakh=save;
for (int t=r+1;t<=3;t++)
{
sanakh=save+a[i][t];
for(int d=0;d<=3;d++)
{
cout<<sanakh<<" ";
sum[ber]=a[i+1][d]+sanakh; ber++;too++;
}
}
horg--;
if(i<4 ) {goto top; }
}
}
sanakh--;
}
}
int h=1;
for(q=0;q<=30;q++) { cout<<sum[q]<<" ";
if (h==4){ cout<<endl;h=0;}h++;}
-
Well the first thing is to learn the value of indentation.
SourceForge.net: Indentation - cpwiki
Here is your code, indented.
Code:
int a[4][4] = { {1, 2, 3, 4}, //i mor
{5, 6, 7, 8}, //j bagana
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int i1;
int j1;
int i = 0, j = 0, b[5][5], r, chose, sanakh = 4, q, too = 0, ber =
0, save = 0, horg = 3, qwe = 0, ewq = 0;
int sum[1000];
for (int l = 0; l < 1000; l++)
sum[l] = 0;
// while(1==1){
// for(j=0;j<=3;j++)
// {
for (r = j; r <= 3; r++) {
for (q = 0; q <= 16; q++) {
save = 0;
top:
for (i = 0; i <= 3; i++) {
if (i < horg) {
save = save + a[i][r];
}
if (horg == i && qwe == ewq) {
for (int z = 0; z <= 3; z++) {
sum[ber] = save + a[i][z];
ber++;
too++;
qwe++;
}
save = 0;
horg--;
} else if (horg == i) {
sanakh = save;
for (int t = r + 1; t <= 3; t++) {
sanakh = save + a[i][t];
for (int d = 0; d <= 3; d++) {
cout << sanakh << " ";
sum[ber] = a[i + 1][d] + sanakh;
ber++;
too++;
}
}
horg--;
if (i < 4) {
goto top;
}
}
}
sanakh--;
}
}
int h = 1;
for (q = 0; q <= 30; q++) {
cout << sum[q] << " ";
if (h == 4) {
cout << endl;
h = 0;
}
h++;
}
next_permutation
Create an array called
Code:
row_index[4] = { 0, 1, 2, 3 };
Create another called
Code:
col_index[4] = { 0, 1, 2, 3 };
For each permutation of row_index, you permute all of col_index
For all of that, you do
Code:
for ( i = 0 ; i < 4 ; i++ )
sum += a[row_index[i]][col_index[i]];
-
Unfortunately, note that Salem's suggestion of next_permutation is from the C++ standard library; in C you have to implement the algorithm yourself.
-
And...another cross-poster.
-
Mmm, this is indeed the C forum, yet the code has cout calls.
Moved.
-
This is probably spoon-feeding, but
Code:
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
int main ( ) {
int a[4][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int row_index[] = { 0, 1, 2, 3 };
int col_index[] = { 0, 1, 2, 3 };
const int N = sizeof(row_index) / sizeof(row_index[0]);
do {
do {
for ( int i = 0 ; i < N ; i++ ) {
cout << "a[" << row_index[i] << "]["<< col_index[i] << "]";
if ( i == N-1 ) cout << endl; else cout << "+";
}
} while ( next_permutation(col_index, col_index+N) );
} while ( next_permutation(row_index, row_index+N) );
return 0;
}