| | |
random numbers, and 2 dimensional array
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 9
Reputation:
Solved Threads: 0
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.
how would i apply the random part of this problem to the code?
cpp Syntax (Toggle Plain Text)
#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?
Last edited by WaltP; Nov 9th, 2007 at 3:14 am. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post... And removed COLOR tags
C++ Syntax (Toggle Plain Text)
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?
C++ Syntax (Toggle Plain Text)
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
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
You can't do this part until you get the first part done. Did you? If so, we can't see the code...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2007
Posts: 9
Reputation:
Solved Threads: 0
yes i did.
it this:
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?
it this:
C++ Syntax (Toggle Plain Text)
#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?
Last edited by cscgal; Nov 17th, 2007 at 3:31 pm. Reason: Added [code] tags.
you need to change the sort function to sort one column of a two-dimensional array
>>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.
C++ Syntax (Toggle Plain Text)
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.
Last edited by Ancient Dragon; Nov 17th, 2007 at 6:19 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
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
In order to sort each row of the 2D array, pass each row to this function.
C++ Syntax (Toggle Plain Text)
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
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Help needed filling array with unique random numbers (C++)
- C++ Random Numbers (C++)
- Random Numbers to create sentences (C)
- generating random numbers into an array.. (Java)
- not getting non repeating random numbers many times (C)
- C++ Reorder random numbers (C++)
- I need help with this particular c++ problem (C++)
- C language problem (C)
Other Threads in the C++ Forum
- Previous Thread: how to do a program 4 billing
- Next Thread: Reecursive reverse order sequence trouble
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






