-
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;
}
-
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
-
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
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
-
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];
}
Similar Threads
-
By peljo in forum VB Classic
Replies: 1
Last Post: 03-23-2006, 10:09 AM
-
Replies: 3
Last Post: 10-12-2005, 01:35 AM
-
By Suze in forum VB Classic
Replies: 2
Last Post: 05-10-2005, 03:41 PM
-
Replies: 3
Last Post: 05-14-2002, 08:04 PM
-
By Jeff Condon in forum Security
Replies: 0
Last Post: 08-25-2000, 07:54 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks