random numbers, and 2 dimensional array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2007
Posts: 9
Reputation: anga08628 is an unknown quantity at this point 
Solved Threads: 0
anga08628 anga08628 is offline Offline
Newbie Poster

random numbers, and 2 dimensional array

 
0
  #1
Nov 9th, 2007
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.

  1. #include <iostream>
  2. #include <ctime> // For time()
  3. #include <cstdlib> // For srand() and rand()
  4.  
  5.  
  6. using namespace std;
  7. int main()
  8. {
  9. const int row=5;
  10. const int column=10;
  11. /*int rnum;*/
  12. double table[row][column];
  13.  
  14. /*srand(time(0)); // Initialize random number generator.
  15.   rnum = (rand() % 50) + 1;
  16. if (rnum>1 && rnum <100)*/
  17. for(int r=0; r<row; r++)
  18. { for(int c=0; c<column;c++)
  19. { cout << table[row][column];
  20. }
  21. }
  22.  
  23.  
  24.  
  25.  
  26. system("pause");
  27. return 0;
  28. }


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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: random numbers, and 2 dimensional array

 
0
  #2
Nov 9th, 2007
  1. 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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: random numbers, and 2 dimensional array

 
0
  #3
Nov 9th, 2007
  1. rnum = (rand() % 50) + 1;
  2. 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
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 9
Reputation: anga08628 is an unknown quantity at this point 
Solved Threads: 0
anga08628 anga08628 is offline Offline
Newbie Poster

Re: random numbers, and 2 dimensional array

 
0
  #4
Nov 13th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: maxmaxwell is an unknown quantity at this point 
Solved Threads: 1
maxmaxwell maxmaxwell is offline Offline
Newbie Poster

Re: random numbers, and 2 dimensional array

 
0
  #5
Nov 13th, 2007
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.
Last edited by maxmaxwell; Nov 13th, 2007 at 11:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 9
Reputation: anga08628 is an unknown quantity at this point 
Solved Threads: 0
anga08628 anga08628 is offline Offline
Newbie Poster

Re: random numbers, and 2 dimensional array

 
0
  #6
Nov 16th, 2007
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


i have no idea how to do this...in a 2d array. please help!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: random numbers, and 2 dimensional array

 
0
  #7
Nov 16th, 2007
Originally Posted by anga08628 View Post
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


i have no idea how to do this...in a 2d array. please help!
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 9
Reputation: anga08628 is an unknown quantity at this point 
Solved Threads: 0
anga08628 anga08628 is offline Offline
Newbie Poster

Re: random numbers, and 2 dimensional array

 
0
  #8
Nov 17th, 2007
yes i did.
it this:





  1. #include <iostream>
  2. #include <ctime> // For time()
  3. #include <cstdlib> // For srand() and rand()
  4. #include <iomanip>
  5. using namespace std;
  6. int table[5][10];
  7. void sortarray (int[], int);
  8. void showarray (int[], int);
  9.  
  10. int main()
  11. {
  12. const int row=5;
  13. const int column=10;
  14. int table[row][column];
  15. int rnum;
  16. int t[row];
  17.  
  18. srand(time(0)); // Initialize random number generator.
  19. rnum = (rand() % 100) + 1;
  20.  
  21.  
  22.  
  23. for(int r=0; r<row; r++)//row
  24. { for(int c=0; c<column; c++)
  25. table [r][c] = (rand()%100) + 1;
  26. }
  27.  
  28. for(int r=0; r<row; r++)//row
  29. { for(int c=0; c<column; c++) //column
  30.  
  31. {
  32. cout << setw(5) << table[r][c] << ' '; //display table
  33.  
  34. }
  35. cout << endl;
  36. }
  37.  
  38.  
  39. sortarray (t, row);
  40.  
  41. cout << "your new sorted table is: ";
  42. showarray ( t, row);
  43.  
  44.  
  45.  
  46.  
  47. system("pause");
  48. return 0;
  49.  
  50. }
  51.  
  52.  
  53.  
  54. void sortarrray(int t[], int elems)
  55. {
  56. int temp;
  57. bool swap;
  58.  
  59. do
  60. {
  61. swap=false;
  62. for (int count=0; count < (elems-1); count++)
  63. {
  64. if (t[count] > t[count +1])
  65. {
  66. temp = t[count];
  67. t[count]=t[count +1];
  68. t[count +1]= temp;
  69. swap=true;
  70. }
  71. }
  72. }while (swap);
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79. void showarray (int t[], int elems)
  80. {
  81. for (int count=0; count < elems; count++)
  82. cout << t[count] << " ";
  83. cout << endl;
  84. }

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: random numbers, and 2 dimensional array

 
0
  #9
Nov 17th, 2007
you need to change the sort function to sort one column of a two-dimensional array
  1. void sortarrray(int t[][10], int NumRows, int ColToSort)
  2. {
  3. // sort code here
  4. }

>>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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: random numbers, and 2 dimensional array

 
0
  #10
Nov 17th, 2007
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.
  1. int i;
  2. for( i = 0; i < 5; i++ )
  3. 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
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC