### How will I be able to create an array of 10,000 random integers?

We were given the starting point for the timing function to record the elapsed time for three sorting algorithms (Bubble, Insertion, Selection) and don't know how to proceed from here.

In this exercise use the program below (ClockTime.cpp) as a starting point for the timing function. Create a program (ch3SortTest.cpp) that will create an array of 10,000 random integers.
A. Sort the array with each of the simple sorting algorithms and record the elapsed time for each sorting algorithm and display the results.
B. Re-load the array with sequential numbers in ascending order and repeat step A.
C. Re-load the array with sequential numbers in descending order and repeat step A.
Your output should look like the screenshot below. Your timing values will vary based on the type of system you use for this test.

https://imgur.com/Vcq1UJM

 ``123456789101112131415161718192021222324252627282930`` `````` #include #include #include using namespace std; int main () { float x,y; clock_t time_req; // Using pow function time_req = clock(); //Get the Starting Time in Microseconds for(int i=0; i<100000; i++) { y = log(pow(i,5)); } time_req = clock() - time_req; // Get the Ending Time in Microseconds cout << "Using pow function, it took " << (float)time_req/CLOCKS_PER_SEC << " seconds" << endl; // Without pow function time_req = clock(); for(int i=0; i<100000; i++) { y = log(i*i*i*i*i); } time_req = clock()- time_req; cout << "Without using pow function, it took " << (float)time_req/CLOCKS_PER_SEC << " seconds" << endl; return 0; ``````
Last edited on
Modern c++ random:

https://www.cplusplus.com/reference/random/uniform_int_distribution/

<random> is a little complicated because it has a lot of options, but you can use the example there ... it has everything you need for this program

its likely you get overflow?
100000 to the 5th is 10 to the 25th
this is way larger than unsigned 64 bit int (2 to the 19th roughly)
so you probably have an issue in this code... because i is using integer math, you may want to cast it as a double eg
log( (double)(i)*i*i*i*i) ;

Last edited on
something like this

 ``12345678`` ``````#include srand(time(0)); int r[10000]; for (auto x = begin(r); x < end(r);++x) { *x=rand()%1000000; }``````
 `` `` ``y = log(pow(i,5));``

You do know that this is the same as:

 `` `` ``y = 5 * log(i);``

 ``123456789101112131415161718`` ``````#include #include #include #include using namespace std; int main () { const int N = 10000; srand( time( 0 ) ); int A[N]; for ( int &i : A ) i = rand(); auto start = clock(); sort( A, A + N ); auto finish = clock(); cout << "Time taken: " << 1000.0 * ( finish - start ) / CLOCKS_PER_SEC << " milliseconds\n"; }``````
I thought it was a 'test how long pow takes' question, is why not use 5* log.
pow does extra work that isnt needed for integers and is a little slow.
Last edited on