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.
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?
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.
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?
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.
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;
}