Jump to content

add 2D array values

- - - - -

  • Please log in to reply
7 replies to this topic

#1
ggmo

ggmo

    Newbie

  • Members
  • Pip
  • 4 posts
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

Edited by ggmo, 13 November 2010 - 11:54 PM.


#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

Please post the code that you have done so far: then we may help you by pointing the problems.

Munir

#3
ggmo

ggmo

    Newbie

  • Members
  • Pip
  • 4 posts
maybe my algorithm is very poor. please help me and tell me some approach
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--;
    }
}



#4
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
The simplest solution (I know it is ugly):

int main()

{

	int a[4][3] = {

		{1, 2, 3},

		{4, 5, 6},

		{7, 8, 9},

		{10, 11, 12}

	};


	for (int i = 0; i < 3; i++) {

		for (int j = 0; j < 3; j++) {

			for (int k = 0; k < 3; k++) {

				for (int l = 0; l < 3; l++) {

					cout<<a[0][i]<<" + "<<a[1][j]<<" + "<<a[2][k]<<" + "<<a[3][l]<<endl;

				}

			}

		}

	}

}

Output:

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

2 + 4 + 7 + 10

2 + 4 + 7 + 11

2 + 4 + 7 + 12

2 + 4 + 8 + 10

2 + 4 + 8 + 11

2 + 4 + 8 + 12

2 + 4 + 9 + 10

2 + 4 + 9 + 11

2 + 4 + 9 + 12

2 + 5 + 7 + 10

2 + 5 + 7 + 11

2 + 5 + 7 + 12

2 + 5 + 8 + 10

2 + 5 + 8 + 11

2 + 5 + 8 + 12

2 + 5 + 9 + 10

2 + 5 + 9 + 11

2 + 5 + 9 + 12

2 + 6 + 7 + 10

2 + 6 + 7 + 11

2 + 6 + 7 + 12

2 + 6 + 8 + 10

2 + 6 + 8 + 11

2 + 6 + 8 + 12

2 + 6 + 9 + 10

2 + 6 + 9 + 11

2 + 6 + 9 + 12

3 + 4 + 7 + 10

3 + 4 + 7 + 11

3 + 4 + 7 + 12

3 + 4 + 8 + 10

3 + 4 + 8 + 11

3 + 4 + 8 + 12

3 + 4 + 9 + 10

3 + 4 + 9 + 11

3 + 4 + 9 + 12

3 + 5 + 7 + 10

3 + 5 + 7 + 11

3 + 5 + 7 + 12

3 + 5 + 8 + 10

3 + 5 + 8 + 11

3 + 5 + 8 + 12

3 + 5 + 9 + 10

3 + 5 + 9 + 11

3 + 5 + 9 + 12

3 + 6 + 7 + 10

3 + 6 + 7 + 11

3 + 6 + 7 + 12

3 + 6 + 8 + 10

3 + 6 + 8 + 11

3 + 6 + 8 + 12

3 + 6 + 9 + 10

3 + 6 + 9 + 11

3 + 6 + 9 + 12



#5
ggmo

ggmo

    Newbie

  • Members
  • Pip
  • 4 posts
please help me who knows how to write algorithm

#6
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Another variant for arbitrary n x m:

int pos[HEIGHT];


int next(int a[][WIDTH], int i) {

	int ret = a[i][pos[i]];


	int maxWidth = WIDTH - 1;

	int maxHeight = HEIGHT - 1;


	if (i == maxHeight) {

		pos[i] += 1;

	}


	if (pos[i] > maxWidth) {

		int j;

		for (j = i - 1; j >= 0; j--) {

			if (pos[j] < maxWidth) {

				pos[j] += 1;

				break;

			}

		}

		for (j = j + 1; j < HEIGHT; j++) {

			pos[j] = 0;

		}

	}


	return ret;

}



int main()

{

	for (int i = 0; i < HEIGHT; i++) {

		pos[i] = 0;

	}


	int a[HEIGHT][WIDTH] = {

		{1, 2, 3},

		{4, 5, 6},

		{7, 8, 9},

		{10, 11, 12}

	};


	int count = 1;

	for (int i = 0; i < HEIGHT; i++) {

		count *= WIDTH;

	}


	while (count--) {

		for (int i = 0; i < HEIGHT; i++) {

			cout<<next(a, i)<<" ";

		}

		cout<<endl;

	}

}

Output:

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 

2 4 7 10 

2 4 7 11 

2 4 7 12 

2 4 8 10 

2 4 8 11 

2 4 8 12 

2 4 9 10 

2 4 9 11 

2 4 9 12 

2 5 7 10 

2 5 7 11 

2 5 7 12 

2 5 8 10 

2 5 8 11 

2 5 8 12 

2 5 9 10 

2 5 9 11 

2 5 9 12 

2 6 7 10 

2 6 7 11 

2 6 7 12 

2 6 8 10 

2 6 8 11 

2 6 8 12 

2 6 9 10 

2 6 9 11 

2 6 9 12 

3 4 7 10 

3 4 7 11 

3 4 7 12 

3 4 8 10 

3 4 8 11 

3 4 8 12 

3 4 9 10 

3 4 9 11 

3 4 9 12 

3 5 7 10 

3 5 7 11 

3 5 7 12 

3 5 8 10 

3 5 8 11 

3 5 8 12 

3 5 9 10 

3 5 9 11 

3 5 9 12 

3 6 7 10 

3 6 7 11 

3 6 7 12 

3 6 8 10 

3 6 8 11 

3 6 8 12 

3 6 9 10 

3 6 9 11 

3 6 9 12 



#7
ggmo

ggmo

    Newbie

  • Members
  • Pip
  • 4 posts
really thanks.
it works well.
i have a headache for 2 days. but you decided my problem easily.
thanks at all

#8
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
You can use the above given code(which is perfect one) to get the sum. Look at the following code

	while (count--) {

		sum = 0;

		for (int i = 0; i < HEIGHT; i++) {

			int nextitem = next(a, i);

			sum += nextitem;

			cout<<nextitem<<" + ";

		}

		cout<<" = "<<sum<<endl;

	}




I hope this helps!

Munir




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users