943,692 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 11167
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 9th, 2007
0

random numbers, and 2 dimensional array

Expand Post »
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.

cpp Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anga08628 is offline Offline
9 posts
since Oct 2007
Nov 9th, 2007
0

Re: random numbers, and 2 dimensional array

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Nov 9th, 2007
0

Re: random numbers, and 2 dimensional array

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 13th, 2007
0

Re: random numbers, and 2 dimensional array

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anga08628 is offline Offline
9 posts
since Oct 2007
Nov 13th, 2007
0

Re: random numbers, and 2 dimensional array

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
maxmaxwell is offline Offline
16 posts
since Nov 2007
Nov 16th, 2007
0

Re: random numbers, and 2 dimensional array

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anga08628 is offline Offline
9 posts
since Oct 2007
Nov 16th, 2007
0

Re: random numbers, and 2 dimensional array

Click to Expand / Collapse  Quote originally posted by anga08628 ...
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...
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Nov 17th, 2007
0

Re: random numbers, and 2 dimensional array

yes i did.
it this:





C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anga08628 is offline Offline
9 posts
since Oct 2007
Nov 17th, 2007
0

Re: random numbers, and 2 dimensional array

you need to change the sort function to sort one column of a two-dimensional array
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 17th, 2007
0

Re: random numbers, and 2 dimensional array

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.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to do a program 4 billing
Next Thread in C++ Forum Timeline: Reecursive reverse order sequence trouble





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC