Convert one dimensional array (array[8]) to two dimensional array (array[2][4]) . | Sololearn: Learn to code for FREE!

0

Convert one dimensional array (array[8]) to two dimensional array (array[2][4]) .

please help

c++

9/20/2021 5:08:55 PM

Younis Ahmad

11 Answers

New Answer

+2

Here are two examples of displaying a single dimensioned array as a 2D array. This example uses a fixed sized array... https://code.sololearn.com/cTrqsdbKkg82 This example uses a dynamically allocated array (pointer)... https://code.sololearn.com/crI2AkB1y3O4

+6

Just use a pointer to an int. An array of 8 integers is effectively an 8x1, or 1x8, array in terms of pointer arithmetic. There is no difference in the storage requirements between an 8x1, 1x8, 2x4, or 4x2 array. It's simply a matter of how you calculate the pointer address.

+3

Martin Taylor thank you

+2

int main() { int k[8]={15,3,21,8,3,12,5,3}; cout<<k[8]; int a[2][4]={{15,3,21,8},{3,12,5,3}} for(int i=0;i<2;i++) { for(int j=0;j<4;j++) { cout<<a[i][j]<<" "; } } return 0; }

+2

mesarthim thank you

0

Younis Ahmad Did you solve your problem in the comment or just add your code lines? It looks still error. Could you specify?

0

i don´t know

0

I checked and a ";" remaining. I added. Also if you print the elements of k, you should use a loop to write all elements. #include <iostream> using namespace std; int main(){ int k[8]={15,3,21,8,3,12,5,3}; for(int s = 0; s < 8; s++){ cout<<k[s] << " "; } int a[2][4]={{15,3,21,8},{3,12,5,3}}; for(int i=0;i<2;i++){ for(int j=0;j<4;j++){ cout<<a[i][j]<<" "; } } return 0; } Also, I suppose the main problem is how to convert it, am I right?

0

yes exactly

0

You should check. I hope, it helps. Happy coding! https://code.sololearn.com/cNGPkZa7MYTq/?ref=app

0

how i can do it