944,089 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4179
  • C++ RSS
Jul 10th, 2006
0

Random number with ascending order help

Expand Post »
Hello,

I'm having some problems with trying to get my random numbers to go in ascending order and descending order as well and trying to figure out how to get when I put in a number between 2 - 25 how to get the highest number out of the random pick as well as the lowest number out of the random pick. Can someone please help!?!?!?!?! I greatly appreciate it if someone could help me out on this!

Below is my code I have so far...

Thanks,
ethompson


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6. void bubbleSort (int Rand[]);
  7. main()
  8. {
  9. cout<<"Data Searching and Sorting!!"<<endl<<endl;
  10. srand(time(0));
  11.  
  12. int Rand_Num;
  13. int Input;
  14. char ans;
  15. int cnt, inside, outside, Swapped, tmp;
  16. int high, low;
  17. do
  18. {
  19. cout << "How many numbers would you like to be generated (2 - 25): ";
  20. cin >> Input;
  21. if(Input >= 2 && Input <= 25)
  22. {
  23. cout<<"The "<<Input<<" numbers genterated are: "<<endl;
  24. for(int i = 0; i < Input; i++)
  25. {
  26.  
  27. Rand_Num = (rand() % 47) + 1;
  28. cout <<Rand_Num<<endl;
  29. }
  30.  
  31. }
  32. else
  33. cout<<"Sorry, that is invalid try again"<<endl;
  34. //______________________________________________________________________________
  35.  
  36. cout<<"The Highest Number is: "<<high<<endl<<endl<<endl;
  37. cout<<"The Lowest number is: "<<low<<endl<<endl<<endl;
  38. //______________________________________________________________________________
  39.  
  40. void bubbleSort(int Rand, 25);
  41. cout<< "After sorting, the list elements are: "<<endl;
  42. int i;
  43. for (i = 0; i<25; i++)
  44. cout<<Rand_Num<<" ";
  45. cout<<endl;
  46.  
  47.  
  48.  
  49. cout<<"Want to pick again? ";
  50. cin>>ans;
  51. cout<<endl<<endl;
  52. }
  53. while((ans=='y') || (ans=='Y'));
  54.  
  55. cin.get();
  56. cin.get();
  57. return 0;
  58. }
Last edited by Dave Sinkula; Jul 10th, 2006 at 5:54 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ethompson is offline Offline
4 posts
since Jun 2006
Jul 10th, 2006
0

Re: Random number with ascending order help

You'll have to actually write your sorting function and call it correctly, using an array.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 10th, 2006
0

Re: Random number with ascending order help

Quote originally posted by ethompson ...
Hello,

I'm having some problems with trying to get my random numbers to go in ascending order and descending order as well and trying to figure out how to get when I put in a number between 2 - 25 how to get the highest number out of the random pick as well as the lowest number out of the random pick. Can someone please help!?!?!?!?! I greatly appreciate it if someone could help me out on this!

Below is my code I have so far...

Thanks,
ethompson


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6. void bubbleSort (int Rand[]);
  7. main()
  8. {
  9. cout<<"Data Searching and Sorting!!"<<endl<<endl;
  10. srand(time(0));
  11.  
  12. int Rand_Num;
  13. int Input;
  14. char ans;
  15. int cnt, inside, outside, Swapped, tmp;
  16. int high, low;
  17. do
  18. {
  19. cout << "How many numbers would you like to be generated (2 - 25): ";
  20. cin >> Input;
  21. if(Input >= 2 && Input <= 25)
  22. {
  23. cout<<"The "<<Input<<" numbers genterated are: "<<endl;
  24. for(int i = 0; i < Input; i++)
  25. {
  26.  
  27. Rand_Num = (rand() % 47) + 1;
  28. cout <<Rand_Num<<endl;
  29. }
  30.  
  31. }
  32. else
  33. cout<<"Sorry, that is invalid try again"<<endl;
  34. //______________________________________________________________________________
  35.  
  36. cout<<"The Highest Number is: "<<high<<endl<<endl<<endl;
  37. cout<<"The Lowest number is: "<<low<<endl<<endl<<endl;
  38. //______________________________________________________________________________
  39.  
  40. void bubbleSort(int Rand, 25);
  41. cout<< "After sorting, the list elements are: "<<endl;
  42. int i;
  43. for (i = 0; i<25; i++)
  44. cout<<Rand_Num<<" ";
  45. cout<<endl;
  46.  
  47.  
  48.  
  49. cout<<"Want to pick again? ";
  50. cin>>ans;
  51. cout<<endl<<endl;
  52. }
  53. while((ans=='y') || (ans=='Y'));
  54.  
  55. cin.get();
  56. cin.get();
  57. return 0;
  58. }
There are a lot of problems in your code. To begin with :

>> you actually intend to store values that were randomly generated in what you call a list but i dont see any array declaration anywhere.

>> void bubbleSort(int Rand, 25);
This is not a proper way of calling functions. It should be like

bubbleSort (Rand, 25); // here Rand is the array u stored the values in

>> You have declared the variables "high" and "low" but i see no logic where you try to find the highest and lowest numbers.

Looks like you are new to C++ progg. so better read some good tutorials and then give this thing a new start.

Post again to ask doubts.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Jul 10th, 2006
0

Re: Random number with ascending order help

Ok, this is what I have come up with now...my problem is that is runs consistantly during the ascending and descending part as well as can someone help me figure out how to get the highest and lowest number for a random number? i am so lost on that part, I don't even know where to look. Please someone help me!! I greatly appreciate anything I can get!

Thanks,
ethompson

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6. main()
  7. {
  8. cout<<"Data Searching and Sorting!!"<<endl<<endl;
  9. srand(time(0));
  10.  
  11. int Rand_Num;
  12. int Input;
  13. char ans;
  14. int high, low;
  15. int i, o, c, swapped, cnt;
  16. int value[26];
  17. do
  18. {
  19. cout << "How many numbers would you like to be generated (2 - 25): ";
  20. cin >> Input;
  21. if(Input >= 2 && Input <= 25)
  22. {
  23. cout<<"The "<<Input<<" numbers genterated are: "<<endl;
  24. for(int i = 0; i < Input; i++)
  25. {
  26.  
  27. Rand_Num = (rand() % 47) + 1;
  28. cout <<Rand_Num<<endl;
  29. }
  30.  
  31. }
  32. else
  33. cout<<"Sorry, that is invalid try again"<<endl;
  34. //______________________________________________________________________________
  35. //highest number in random pick
  36.  
  37.  
  38. cout<<"The Highest Number is: "<<high<<endl<<endl<<endl;
  39.  
  40. //______________________________________________________________________________
  41. //lowest number in random pick
  42.  
  43. cout<<"The Lowest number is: "<<low<<endl<<endl<<endl;
  44. //______________________________________________________________________________
  45. //ascending order
  46.  
  47. for (o=0; o<25; o++)
  48. {
  49. swapped=0;
  50. for(i=o; i<25; i++)
  51. {if (value[i]<value[o])
  52. {cnt = value[i];
  53. value[i] = value[o];
  54. value[o] = cnt;
  55. swapped=1;
  56. }
  57. }
  58. if (swapped==0) {break;}
  59. cout<<"The "<<Input<<" numbers in Ascending Order: "<<endl;
  60. for (c=0; c<25; c++)
  61. {cout<<value[c]<<" ";}
  62.  
  63. }
  64. //______________________________________________________________________________
  65. //descending order
  66.  
  67. for (o=0; o<25; o++)
  68. {
  69. swapped=0;
  70. for(i=o; i<25; i++)
  71. {if (value[i]>value[o])
  72. {cnt = value[i];
  73. value[i] = value[o];
  74. value[o] = cnt;
  75. swapped=1;
  76. }
  77. }
  78. if (swapped==0) {break;}
  79. cout<<"The "<<Input<<" numbers in Descending Order: "<<endl;
  80. for (c=0; c<25; c++)
  81. {cout<<value[c]<<" ";}
  82.  
  83. }
  84.  
  85. //______________________________________________________________________________
  86. cout<<"Want to pick again? ";
  87. cin>>ans;
  88. cout<<endl<<endl;
  89. }
  90. while((ans=='y') || (ans=='Y'));
  91.  
  92. cin.get();
  93. cin.get();
  94. return 0;
  95. }
Last edited by Dave Sinkula; Jul 10th, 2006 at 11:24 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ethompson is offline Offline
4 posts
since Jun 2006
Jul 11th, 2006
0

Re: Random number with ascending order help

Quote originally posted by ethompson ...
Ok, this is what I have come up with now...my problem is that is runs consistantly during the ascending and descending part as well as can someone help me figure out how to get the highest and lowest number for a random number? i am so lost on that part, I don't even know where to look. Please someone help me!! I greatly appreciate anything I can get!

Thanks,
ethompson
I think you are not getting the point.

>> You were supposed to store the input in an array which u still havent done in your new code.

>> The logic or the function for findin the max and min element is not yet defined.

Anyways here is the working code.
Try to compare this code with the one you have written.
See the way the arrray has been declared to store input and the functions for sorting.

Post if any doubts in the code given and

POST YOUR CODE USING THE CODE TAGS PROVIDED PLEASE. MR. DAVE HAS GOT TIRED OF EDITING YOUR POST EACH TIME SO PLEASE UNDERSTAND AND FOLLOW THE FORUM RULES.


#include <iostream> 
#include <ctime> 
#include <cstdlib> 

usingnamespace std;
void bubbleSort (int randNumbers[], int count)
{
int outer, inner;
for (outer = count - 1; outer > 0; --outer)
{
for (inner = 1; inner <= outer; ++inner)
{
if (randNumbers [inner] < randNumbers[inner - 1])
{
int tmp = randNumbers [inner];
randNumbers[inner] = randNumbers[inner - 1];
randNumbers[inner - 1] = tmp;
}
}
}
}
void displayRandNumbers (int randNumbers[], int count)
{
cout << endl;
for (int i = 0; i < count; ++i)
cout << randNumbers[i] << " ";
}
int main (void)
{
srand(time(0)); 
int randNumbers[26];
int input; 
char ans;
int high, low;
int i, o, c, swapped, cnt;
int value[26];

do 
{
cout << "How many numbers would you like to be generated (2 - 25): "; 
cin >> input; 
if(input >= 2 && input <= 25) 
{
for(int i = 0; i < input; i++) 
{
randNumbers[i] = ((rand() * 100) + 1) % 99; 
}
cout << "\nBefore sorting... \n";
displayRandNumbers (randNumbers, input);
bubbleSort (randNumbers, input);
cout << "\n\nHIGHEST = " << randNumbers[input-1];
cout << "\nLOWEST = " << randNumbers[0];
cout << "\n\nAfter sorting...\n";
displayRandNumbers (randNumbers, input);
}
else
cout<<"Sorry, that is invalid try again"<<endl;
cout<<"\n\nWant to pick again? ";
cin>>ans;
cout<<endl<<endl;
}
while((ans=='y') || (ans=='Y')); 

cin.get();
return 0;
}
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

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: Create buttons dynamically and adding them to a panel
Next Thread in C++ Forum Timeline: graphics primitives without OS calls





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


Follow us on Twitter


© 2011 DaniWeb® LLC