CS 107 - 7/1/15 Random Numbers Typically use a fixed sequence of numbers that range from 0 to The Maximum INteger value on the machine. When we call the rand() library function, it returns the next value in the sequence. There is a function in the library called srand(), which stand for "seed random". srand() start the access of the random number sequence from a position other than the first position. srand (X) would cause the Xth value in the random number sequence to be returned by the next call to rand(). The most common solution to get "truly random numbers", is to use srand() with the time() library function. This is done using the code: srand (time(NULL)); time() is located in the time library, so we need the #includes of the following for a C program. #include /* srand, rand */ #include /* time */ of the following for a C++ program: #include // srand, rand #include // time Comments on Project 2: See recorded lecture screen Computation with sets of data - arrays are the first apporach to letting us deal with more than one set of data. From lab, we had algorithms to deal with: - calculate average - find largest/smallest - find the location of a value Algorithm for sorting values in an array. Selection Sort: 1. Set the current position to the first position 2. Find the smallest in the part of the array from the current position to the last position 3. Exchange this smallest value with the value in the current position 4. Set the current position to the next position 5. if not at the end of the array go to step 1. Example 5 8 3 7 4 smallest: ^ becomes: 3 8 5 7 4 smallest: ^ becomes: 3 4 5 7 8 smallest ^ becomes: 3 4 5 7 8 smallest ^ becomes: 3 4 5 7 8 int array[SIZE]; // code to store values into the array // code for selection sort using WHILE LOOPS int currPos = 0; // loop for all locations in the array while ( currPos < (SIZE-1) ) { // find the position of the smallest value in this part of the array int smallPos = currPos; // loop to find the position of the smallest value int i = currPos; while ( i < SIZE ) { if ( array[i] < array[smallPos] ) { smallPos = i; } i++; } // exchange the value at the smallest position with the // value at the current Position int temp; temp = array[currPos]; array[currPos] = array[smallPos]; array[smallPos] = temp; // update/increment the current Position currPos++; } Same code but using for loops instead of while loops // code for selection sort using FOR LOOPS // loop for all locations in the array for ( int currPos = 0 ; currPos < (SIZE-1) ; currPos++ ) { // find the position of the smallest value in this part of the array int smallPos = currPos; // loop to find the position of the smallest value for ( int i = currPos ; i < SIZE ; i++ ) { if ( array[i] < array[smallPos] ) { smallPos = i; } } // exchange the value at the smallest position with the // value at the current Position int temp; temp = array[currPos]; array[currPos] = array[smallPos]; array[smallPos] = temp; }