Copying form one array to the other

Write your question here.
I need assistance in writing a statement(s) for the following instances

1.declare an array of 50 elements of type int called alpha
2.declare another 2 dimensional array aplha2D with 10 columns and 5 rows
3.Write a nested for loop to copy the elements of the array aplha into the rows of alpha2D i.e the first 10 elements go to the first row,second 10 elements go to the second row and so forth,until all the elements have been copied over.

My answers are as follows:
1.alpha[50];
2.alpha2D[5][10];

3.I am stuck with this question as I am not sure how to copy the elements of alpha to aplha2D

please assist
Think about how integer division and the modulo operator works.
Hi fg109
Do you have nested loop for me to look at cz I am nt sure how to use modulus in this instance.
Oops, didn't read carefully enough and thought you were supposed to do it in a single loop.

Well, I was thinking about something like this:

1
2
for (int i = 0; i < 50; ++i)
    alpha2D[i/10][i%10] = alpha[i];

You should be able to turn that into a nested loop easily enough.
@ fg109
i am failing to make a nested loop fro the loop above can you please assist
1
2
3
4
5
6
7
8
for (int i = 0; i < 50; ++i)
    alpha2D[i / 10][i % 10] = alpha[i];
//          0 - 4    0 - 9

for (int j = 0; j < 5; ++j) // 0 - 4
    for (int k = 0; k < 10; ++k) // 0 - 9
        alpha2D[j][k] = alpha[?];
// ? = some equation using j and k that gives values 0 - 49 

Any more and I would be solving it for you, so you need to figure this out on your own.
Last edited on
1
2
3
4
for(int col = 0; col < 10; col++)
  for(int row = 0; row < 5; row++)
    // first call [0][0], then [0],[1] ... [9],[4] (remember indexing is 1-off from number)
    // since alpha is contiguous, alpha[col * 5 + row] will increment [0] -> [49] 

Topic archived. No new replies allowed.