Question:
c++ multidimensional array?
Muhemmed
2013-08-29 02:18:35 UTC
Hi.I want use two dimens. array for print screen this: 1 6 7
2 5 8
3 4 9.
I tried this:
a[3][3]={1,6,7,2,5,8,3,4,9}

for (i=0;i<3;i++){
for (j=0;j<3;j++){ cout< cout< }
Its give me true but i think its wrong way because:
if i have a[3][3]={1,2,3,4,5,6,7,8,9} how i can print this: 1 6 7
2 5 8
3 4 9.
please write code,i searched google but i cant find.Thanks! Sorry for my bad english.
Five answers:
AnalProgrammer
2013-08-29 02:45:18 UTC
The correct initialisation should be

a[3][3] = {{1,6,7}, {2,5,8}, {3,4,9}};



Try

int b[3][3];

int k = 0;

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

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

if (i % 2 == 0) {

b[i][k] = a[i][j];

k++;

} else {

b[i][k] = a[i][j];

k--;

} //End if

}

if (i % 2 == 0) {

k = 2;

} else {

k = 0;

} //End if

}



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

for (j=0;j<3;j++){ cout<
cout<
}



Have fun.
2013-08-29 09:59:08 UTC
Hi,

You can do one of two things:-

Your input is a[3][3]={1,2,3,4,5,6,7,8,9}

You need output {1,6,7,2,5,8,3,4,9}

1.Assuming your input is always a 2-d array a[3][3]

You can use a temporary variable say temp and rearrange the sequence

Something like

for(n=0 to 8)

{

a[0][0]=a[0][0];//Keeping 1 in the first position

temp=a[0][1];//Storing value in second position say 2

a[0][1]=a[1][2]//Now we are storing the value in position a[1][2] that is 6 in the second position

a[1][2]=temp//At this stage your array will look like this a[3][3]={1,6,3,4,5,2,7,8,9}

//Similarly you assign the value for next position

//Say you want 7 to appear in the 3rd position

/*so add a few steps and exchange array element 7 with array element 3 so you get the array a[3][3]={1,6,7,4,5,2,3,8,9} */

//proceed this way until you get the required arrangement

}



2.The other way is a more complicated code but it is shorter,I suggest you dont attempt it since it looks like you are at a beginners level
cja
2013-08-31 11:50:27 UTC
As you can see from previous answers, there are a few different ways to approach this. If you find that your code has special-case logic for this particular array or its contents, it's a hint that you're not getting the best solution. As I've done it below, the only restriction is that the matrix is square.



#include



using namespace std;



const size_t rows = 3;

const size_t cols = rows;



int A[rows][cols] = {

    {1, 2, 3},

    {4, 5, 6},

    {7, 8, 9}

};



int main(int argc, char *argv[]) {

    //

    // Display transpose of square matrix A, but with

    // odd-numbered columns reversed, top to bottom.

    //

    for (size_t i = 0; i < rows; i++) {

        for (size_t j = 0; j < cols; j++) {

            cout << A[ j ][ (j & 1) ? (cols - i - 1) : i ] << ' ';

        }

        cout << endl;

    }

    return 0;

}



#if 0



Program output:



1 6 7

2 5 8

3 4 9



#endif
2013-09-06 05:52:31 UTC
one ofn the important things to consider when you contemplate leaving your husband is what your income will be while the divorce is pending. If you don't work outside the home or are retired, how will you pay your bills during a divorce?
?
2013-08-29 09:43:59 UTC
include

using namespace std;



int main() {

  int a[3][3] = {1,2,3,4,5,6,7,8,9};

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

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

      if (a[i][j] == 1 || a[i][j] == 6 || a[i][j] == 7)

        cout << a[i][j] << " ";

    }

  }

  return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...