Define a 5 x10 2-dimensional array of integers (5 rows of 10 columns), randomly generate 50 numbers between 1 and 100, assign them to the array elements and display all the numbers on 5 lines of 10 integers each.

``````#include <iostream>
#include <ctime>    // For time()
#include <cstdlib>  // For srand() and rand()

using namespace std;
int main()
{
const int row=5;
const int column=10;
/*int rnum;*/
double table[row][column];

/*srand(time(0));  // Initialize random number generator.
rnum = (rand() % 50) + 1;
if (rnum>1 && rnum <100)*/
for(int r=0; r<row; r++)
{ for(int c=0; c<column;c++)
{ cout << table[row][column];
}
}

system("pause");
return 0;
}``````

how would i apply the random part of this problem to the code?

``cout << table[row][column];``

Are you sure this is what you want to be doing?
In regard to implementing the random integers, you can just assign them as you print that particular cell. So add something before the above code.

On a minor note, why are you creating an array of doubles?

``````rnum = (rand() % 50) + 1;
if (rnum>1 && rnum <100)``````

this is not doing quite what you want. rnum will be in the range 1-50. Your if condition will fail on value 1, which should be legitimate.

You're on the right track to get your 1-100 range, and you don't need the if statement.

You need to place the rand( ) statement inside the innermost loop, assigning that value to an array element.

If you choose to do the assignments and display in the same loops, you will also need to add some spacing between the outputs, and output a newline at the after the inner loop completes.

Val

what do you mean? i need to place the rand( ) statement inside the innermost loop, assigning that value to an array element.

how would i do that?

You know how to populate a two dimensional array with an iteration(for, while etc.)statement right?
Do it the same way, except set the values equal to random numbers instead.

okay so the second part of this assingment is: Using a sort routine of your choice, sort the numbers in each of the rows of the array and display the sorted numbers again on 5 lines of 10 numbers each

okay so the second part of this assingment is: Using a sort routine of your choice, sort the numbers in each of the rows of the array and display the sorted numbers again on 5 lines of 10 numbers each

You can't do this part until you get the first part done. Did you? If so, we can't see the code...

yes i did.
it this:

``````#include <iostream>
#include <ctime> // For time()
#include <cstdlib>  // For srand() and rand()
#include <iomanip>
using namespace std;
int table[5][10];
void sortarray (int[], int);
void showarray (int[], int);

int main()
{
const int row=5;
const int column=10;
int table[row][column];
int rnum;
int t[row];

srand(time(0));  // Initialize random number generator.
rnum = (rand() % 100) + 1;

for(int r=0; r<row; r++)//row
{ for(int c=0; c<column; c++)
table [r][c] = (rand()%100) + 1;
}

for(int r=0; r<row; r++)//row
{ for(int c=0; c<column; c++) //column

{
cout << setw(5) << table[r][c] << ' '; //display table

}
cout << endl;
}

sortarray (t, row);

cout << "your new sorted table is:  ";
showarray ( t, row);

system("pause");
return 0;

}

void sortarrray(int t[], int elems)
{
int temp;
bool swap;

do
{
swap=false;
for (int count=0; count < (elems-1); count++)
{
if (t[count] > t[count +1])
{
temp = t[count];
t[count]=t[count +1];
t[count +1]= temp;
swap=true;
}
}
}while (swap);
}

void showarray (int t[], int elems)
{
for (int count=0; count < elems; count++)
cout << t[count] << " ";
cout << endl;
}``````

i tried doing the second part, but i didnt succeed. it also asks me to do a binary search...would it change if its a 2d array?

you need to change the sort function to sort one column of a two-dimensional array

``````void sortarrray(int t[][10], int NumRows, int ColToSort)
{
// sort code here
}``````

>>it also asks me to do a binary search...would it change if its a 2d array?
Very similar to the way you would sort it. Start out using the normal binary search algorithm for a one-dimensional array then expand it to use a 2-dimensional array that searches only one of the columns.

You have a sort function that will sort a 1D array, it looks to be correct.

In order to sort each row of the 2D array, pass each row to this function.

``````int i;
for( i = 0; i < 5; i++ )
sort_array( table[i], 10 );``````

Using just one index with a 2D array is the essentially accessing just that row, treating it as a 1D array.

Of course, you cannot do this with columns. That's a problem for another day.

Val

>>Of course, you cannot do this with columns. That's a problem for another day
Sure you can -- if what you mean is you can not sort by columns rather than by rows. Its possible to sort all rows by a specific column or all columns by a specific row. Might not make much sense to do it, but it is possible.

Please, you need more consistent formatting. See this for ideas, especially the use of indentation (TABs vs. SPACES for one)

Also, don't use `system("pause")` when there are better solutions...

>>Of course, you cannot do this with columns. That's a problem for another day
Sure you can -- if what you mean is you can not sort by columns rather than by rows. Its possible to sort all rows by a specific column or all columns by a specific row. Might not make much sense to do it, but it is possible.

What I meant was that you cannot simply use an individual column as an argument to the sort function that handles a 1D array.

Val

check this one:

``````// DMA of 2D array in C++
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x = 3, y = 3;

int **ptr = new int *[x];

for(int i = 0; i<y; i++)
{
ptr[i] = new int[y];
}
srand(time(0));

for(int j = 0; j<x; j++)
{
for(int k = 0; k<y; k++)
{
int a = rand()%10;
ptr[j][k] = a;
cout<<ptr[j][k]<<" ";
}
cout<<endl;
}
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.