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;
}``````

## All 4 Replies

You'll have to actually write your sorting function and call it correctly, using an array.

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 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.

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

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

using namespace std;
main()
{
cout<<"Data Searching and Sorting!!"<<endl<<endl;
srand(time(0));

int Rand_Num;
int Input;
char ans;
int high, low;
int i, o, c, swapped, cnt;
int value;
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;
//______________________________________________________________________________
//highest number in random pick

cout<<"The Highest Number is: "<<high<<endl<<endl<<endl;

//______________________________________________________________________________
//lowest number in random pick

cout<<"The Lowest number is: "<<low<<endl<<endl<<endl;
//______________________________________________________________________________
//ascending order

for (o=0; o<25; o++)
{
swapped=0;
for(i=o; i<25; i++)
{if (value[i]<value[o])
{cnt = value[i];
value[i] = value[o];
value[o] = cnt;
swapped=1;
}
}
if (swapped==0) {break;}
cout<<"The "<<Input<<" numbers in Ascending Order: "<<endl;
for (c=0; c<25; c++)
{cout<<value[c]<<" ";}

}
//______________________________________________________________________________
//descending order

for (o=0; o<25; o++)
{
swapped=0;
for(i=o; i<25; i++)
{if (value[i]>value[o])
{cnt = value[i];
value[i] = value[o];
value[o] = cnt;
swapped=1;
}
}
if (swapped==0) {break;}
cout<<"The "<<Input<<" numbers in Descending Order: "<<endl;
for (c=0; c<25; c++)
{cout<<value[c]<<" ";}

}

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

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

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>

using namespace 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;
int input;
char ans;
int high, low;
int i, o, c, swapped, cnt;
int value;

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;
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;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.