I am working on a code that generates 35 random numbers between 0 and 199. Here is what I have so far. However, I would like to now have it sort by low to high values. Can someone provide guidance?
you can print, then sort, then print, is one way.
you can copy and sort and have 2 copies, is a wasteful way, but fine for homework.
quicksort ... if you want it, you have to write it or get one of the bajillion student homework versions online. C++ does not provide an algorithm choice for sorting in the libraries. C's sort MAY still be quicksort or may be if you use an older compiler. Odds are they have also updated it to intro for up to date tools.
Create a boolean array of size 200, all initialised to false.
Choose 35 distinct elements to turn true.
Take the true elements from indices 0 to 199 to "sort" ascending.
Take the true elements from indices 199 to 0, step -1, to "sort" descending.
Alternatively, generate random numbers and stick them in a set<int> until the size of that set is 35.
Then just iterate through the set; they'll be in ascending order.
If you are prepared to allow duplicates (and my previous post assumed you didn't):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
usingnamespace std;
int main()
{
constint N = 35;
int a[N];
srand( time( 0 ) );
generate( a, a + N, [](){ return rand() % 200; } );
sort( a, a + N );
for ( auto e : a ) cout << e << ' ';
}
If you don't allow duplicates, then:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <set>
usingnamespace std;
int main()
{
constint N = 35;
set<int> a;
srand( time( 0 ) );
while ( a.size() < N ) a.insert( rand() % 200 );
for ( auto e : a ) cout << e << ' ';
}
The right way of using an uniform integer distribution is to use std::uniform_int_distribution with a standard C++ random generator like Mersenne twister
Returning to your original code, you seem to have a bug: it allows duplicates:
1 2
if (i != i-1) // always true
array[j]=i;
If you fixed this to find duplicate entries, it would still have a bug:
1 2 3 4 5
else
{
i = rand() % 200; // what if this is also a duplicate?
array[j] = i;
}
Your basic algorithm needs to be:
1 2 3 4 5 6
do 35 times {
do {
pick a random number
} while (the number is already in the array)
insert the number in the array
}
Alternatively you could use lastchance's method (let std::set() take care of duplicates) or JL Borges's (create an array of 200 number and then pick 35 random values from the array).
Hey. Thank you for the help and guidance everyone. I don't mind duplicates. I'm just trying to figure out how to use quicksort to sort my random numbers separately from the generated numbers.
If you are looking specifically for how to implement quicksort yourself, just search for it or watch a youtube video that explains it. There are probably hundreds of sites that will show an implementation of quicksort. (Note that the C library's standard function is actually called qsort, which is also in C++, but if you're using C++ you should be using std::sort.)