DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Random numbers in ascending order help pls!!

1. Registered User
Join Date
Jun 2006
Location
Michigan
Posts
3

## Random numbers in ascending order help pls!!

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

#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;
void bubbleSort (int Rand[]);
main()
{
cout<<"Data Searching and Sorting!!"<<endl<<endl;
srand(time(0));

int Rand_Num;
int Input;
char ans;
int cnt, inside, outside, Swapped, tmp;
int high, low;
do
{
cout << "How many numbers would you like to be generated (2 - 25): ";
cin >> Input;
if(Input >= 2 && Input <= 25)
{
cout<<"The "<<Input<<" numbers genterated are: "<<endl;
for(int i = 0; i < Input; i++)
{

Rand_Num = (rand() % 47) + 1;
cout <<Rand_Num<<endl;
}

}
else
cout<<"Sorry, that is invalid try again"<<endl;
//______________________________________________________________________________

cout<<"The Highest Number is: "<<high<<endl<<endl<<endl;
cout<<"The Lowest number is: "<<low<<endl<<endl<<endl;
//______________________________________________________________________________

void bubbleSort(int Rand, 25);
cout<< "After sorting, the list elements are: "<<endl;
int i;
for (i = 0; i<25; i++)
cout<<Rand_Num<<" ";
cout<<endl;

cout<<"Want to pick again? ";
cin>>ans;
cout<<endl<<endl;
}
while((ans=='y') || (ans=='Y'));

cin.get();
cin.get();
return 0;
}

2. Registered User
Join Date
Apr 2006
Location
Zagreb, Croatia
Posts
152
there are few errors in your code. First , bubbleSort is declared with only one argument , however the definition of bubbleSort has 2 arguments. Moreover the bubbleSort method doesn't do any sorting. One easy way to solve this would be to store all random values into an array (you used an integer which can store only the last random value) and then simply apply the bubbleSort on the array

3. Registered User
Join Date
Jan 2005
Location
UK
Posts
604
Hi,
is it an assignment to implement bubble-sort? If not and you need your some other purpose, you could use the STL sort() algorithm to sort your array of random numbers.
...cout<<"The Highest Number is: "<<high<<endl<<endl<<endl;
cout<<"The Lowest number is: "<<low<<endl<<endl<<endl;
//______________________________________________________________________________

...
using namespace std;
void bubbleSort (int Rand[]);
main()
...
void bubbleSort(int Rand, 25);

cout<< "After sorting, the list elements are: "<<endl;
int i;
for (i = 0; i<25; i++)...
In your code you declare bubbleSort twice with different signatures I must add.
Are you trying to call an external function the second time round instead? Then the void is wrong at this place. Make sure you call it with all neccessary parameters.
Once your array is sorted the rest is trivial: the lowest number is at the beginning of the array and the biggest at its end.
If you need help help implementing bubblesort come back, but there should be plenty on the Web too.

Cheers,

Dieter

4. Senior Member
Join Date
Dec 2003
Posts
3,366
Or dont even sort it, just iterate and find the low/high.

low = high = list[0];
for(i = 1; i < size; i++)
{
if(list[i] > high)
high = list[i];

if(list[i] < low)
low = list[i];
}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

 FAQ Latest Articles Java .NET XML Database Enterprise