Copying 2d array into 1d array

Create a 2D array with 20 rows and 20 columns. Populate the array row-by-row with random real numbers (not integers) that range in value from 0 to 100. Copy the diagonal elements of the 2D array into a 1D array.

My professor also gave this information: In your main function, you simply create a for loop where A[i] = B[i][i]. The diagonal elements are B[0][0], B[1][1], B[2][2], etc. B[0][0] gets copied to A[0], B[1][1] to A[1], and so on.
But I don't know how to incorporate this into my program, I'm quite new to this.

My program thus far:
//Ive just been able to setup the the 20x20 multidimensional array with random //real numbers 0-100. Someone please help me incorporate the information above.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int main()
{
double array[20][20];
srand(time(0));
for (int i=0;i<20;i++)
{
for (int j=0;j<20;j++)
{
array[i][j]=(rand()%100);
cout<<array[i][j]<<" ";
}
cout<<endl;

}



}
Can you just use memcpy()? Allocating array as you have the memory is contiguous.
B[0][0] gets copied to A[0], B[1][1] to A[1],


I dont think this is correct, you probably meant B[0][1] to A[1].

In that case, just create an array that is of size 400, which is 20x20. Then just put everything in there.
In future posts, please use the [code][/code] tags to surround your code.

I finally figured out how to post the tags without actually using them.
@fg109 Could you please tell me, cuz Ive honestly tried everything .D

Edit: @Texan40 No he cant
Last edited on
@TarikNeaj

I'm pretty sure he can.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstring>

int main()
{
    int A[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int B[3][3];
    
    std::cout << "A\n";
    for (int i = 0; i < 9; ++i)
        std::cout << A[i] << ' ';

    std::cout << "\n\nB before copy\n";
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
            std::cout << B[i][j] << ' ';
        std::cout << '\n';
    }
    
    memcpy(B, A, sizeof(A));

    std::cout << "\n\nB after copy\n";
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
            std::cout << B[i][j] << ' ';
        std::cout << '\n';
    }
    return 0;
}
A
1 2 3 4 5 6 7 8 9 

B before copy
1 0 2 
0 6295552 0 
4197037 0 1 


B after copy
1 2 3 
4 5 6 
7 8 9 


And you can post the tags like this: [co[tt][/tt]de][/co[tt][/tt]de]
@fg109. He got instructions on how to do it, so no he probably cant.

My professor also gave this information: In your main function, you simply create a for loop where A[i] = B[i][i].


This doesnt look like instructions to using memcpy.

And thanks :)

[code] Testing Testing [/code]
Sorry for the late reply. yes as Tarik said I have not yet learned memcpy function in class so I'm assuming I cannot use that.Only using the information I have posted above am I suppose to write this code. I have re-posted the question on the forums to further input other peoples answers to this problem.
Topic continued here:
http://www.cplusplus.com/forum/beginner/162155/

Please do not post more than once.
Topic archived. No new replies allowed.