PLEASE NEED HELP: Converting 2d array into a 1d array

Pages: 12
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.
Also I have not yet learned the memcpy function for those who have asked to use that before so I'm assuming I cant use that. I would really appreciate it if you can right the answer in code rather than in words because that would help me understand a lot better. Thank you.

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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;

}
Last edited on
This topic is a continuation of http://www.cplusplus.com/forum/beginner/161840/

Your assignment requires you to populate the array with random real numbers, not integers. It is not possible to do this with rand(), so don't use it. Instead, use the random generators in the <random> header:
http://www.cplusplus.com/reference/random/
http://en.cppreference.com/w/cpp/header/random
http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution#Example

You won't need to use memcpy for this assignment. In fact, you won't need to use any C for this assignment - you can do this entirely in C++.
Last edited on
Sorry an update on the post: My professor has told me I can create the this program using integers so disregard that part. I had just written the question like that because I copy and pasted the question from my worksheet which my professor later clarified that I'm able to do this with integers or real numbers it was our choice.
Please edit your post and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.

Right now it looks like your next step is to make the loop to take the diagonal entries and store them in a 1D array.
Yes, that's the part I'm stuck at. My professor said I can simply create a for loop where A[i]=B[i][i] and the diagonal elements are B[0][0],B[1][1],ect. B[0][0] copied to A[0], B[0][1] copied to A[1] ect.

But I don't know how to put this into my program. Any ideas?
I don't know how you're confused - your program already contains for loops, so you clearly know the syntax. What specifically is confusing you?
OK, so I said I didn't know how to put that into my program which meant I really didn't know how to put it. What I have done just takes care of the for loops for the two dimensional arrays. like I've said before I'm very new to this. Can you please put what my professor said into my program in code and show me how its done.
1
2
3
4
5
double diagonal[20];
for(int i = 0; i < 20; ++i)
{
    diagonal[i] = array[i][i];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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<<endl;
double diagonal[20];
for (int i=0;i<20;i++)
{
    diagonal [i]=array [i][i];
    cout<<diagonal[i]<<" ";
}
    cout<<endl;}
Last edited on
Thanks for giving the information in code. Ok This is what I have done but when I cout the diagonal[i] as a test, my output values are somewhat repeating numbers and not all between 1-100. Do you know how I can fix that?
so, is that a thanks LB? And really, still not code tags?
Last edited on
I wasn't quite sure how to put the code tags. Sorry I put it up now.
Please edit your posts and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.

If you fix your code indentation, the problem should become clear. The issue is that you pasted the code I provided inside one of your loops instead of after it.
LB,
Can I post a fix? I know where the problem is. Don't want to step on your feet. You've been on this since the beginning.
The point of a forum is for anyone to get and give help ;) our only preference is to not post full solutions to homework.
Thank you I've fixed the indentation and now I got 20 random numbers between 1-100. So are these 20 numbers supposed to be my diagonal elements?
Look at the curly brace on line 6. Where does it close? It should close before line 12.
//Yes I closed it at line 11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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<<endl;} 
double diagonal[20];
for (int i=0;i<20;i++)
{
    diagonal [i]=array [i][i];
    cout<<diagonal[i]<<" ";
}
    cout<<endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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<<endl;
		double diagonal[20];		//all of this needs to be outside of the brace on 19
		for (int i=0;i<20;i++)
		{
			diagonal [i]=array [i][i];
			cout<<diagonal[i]<<" ";
		}
		cout<<endl;
	}
Last edited on
as4mvp wrote:
Yes I closed it at line 11
http://ideone.com/vwXqcD
It seems to be working, aside from all that excess empty lines. Do you have further questions?
Pages: 12