Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions.

Similar presentations


Presentation on theme: "1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions."— Presentation transcript:

1 1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions Using template functions CSE 20232 Lecture 17 – Search & Sort

2 2 Reading Random Numbers Section: 6.7 Overloading & Templates Sections: 6.17 – 6.18 14.1 – 14.3

3 3 Pseudo random Numbers A sequence of “randomly” distributed numbers can be generated by using unsigned rand(); Returns and unsigned int in the range 0..RAND_MAX Defined in the header cstdlib The initial seed value of the sequence can be changed by using void srand(unsigned seed); The seed can be user determined, constant, or determined from current system time cin >> value; srand(value); srand(1); srand(time(0));

4 4 Examples of using rand() // output a sequence of 100 values in range 0..MAX_RAND // but have the same sequence every time the program runs #include #include // for rand() & srand() using namespace std; int main() { srand(1); // set seed value for rand() for (int i=0; i<100; i++) cout << rand() << ‘ ‘; // next “random” value cout << endl; return 0; }

5 5 Examples of using rand() // output a sequence of 100 values in range 0..n // but have the same sequence every time the program runs #include // for rand() & srand() #include using namespace std; int main() { int n; cout << “enter max desired value: “; cin >> n; srand(1); // set seed value for rand() for (int i=0; i<100; i++) cout << (rand() % n) << ‘ ‘; // next “random” value cout << endl; return 0; }

6 6 Examples of using rand() // output a sequence of 100 values in range 0..MY_MAX // but have the user seed the sequence #include // for rand() & srand() #include #define MY_MAX 100 using namespace std; int main() { unsigned seed; cout << “enter seed for randome # generator: “; cin >> seed; srand(seed); // set seed value for rand() for (int i=0; i<100; i++) cout << (rand() % MY_MAX) << ‘ ‘; // next “random” value cout << endl; return 0; }

7 7 Examples of using rand() // output a sequence of 100 values in range 0..MY_MAX // but have seed differ based on time() program was run #include #include // for rand() & srand() #include // for time() function #define MY_MAX 100 using namespace std; int main() { unsigned seed; srand(time(0)); // set seed value for rand() based on time for (int i=0; i<100; i++) cout << (rand() % MY_MAX) << ‘ ‘; // next “random” value cout << endl; return 0; }

8 8 C/C++ system() function Your program can call for the operating system command interpreter to execute a command Systems: Linux, Unix, DOS, … Command Interpreters: bash, sh, csh, tcsh, command.exe, … The function used is int system(const char str[ ]); It returns the status of the shell after the commanded action is performed

9 9 Examples of system() // program lists all C++ files in current directory, and uses // the Linux “more” command to allow user to see the selected one #include using namespace std; int main () { string filename, command = “more “; system(“clear”); // clear the console window cout << “C/C++ files in current folder:\n”; system(“ls *.h *.cpp”); // list the files cout << “Enter name of file to view:”; cin >> filename; command += filename; system(command.c_str()); // execute “more ” return 0; }

10 10 Searching & Sorting Demo Main program provides these options Create a random list of numbers Sort the list Search the list Sequentially Using Binary Search Displays process using NCurses I/O

11 11 Making code more generic To search or sort various types of data sequences, we can write separate versions of each function for each data type The compiler tells them apart by their signatures (function name and the number, order and types of parameters) Example: void swap(double &x, double &y); void swap(int &x, int&y); void swap(string &x, string &y);

12 12 Making code more generic Or we create a template for the function and let the compiler actually do the work of building each version we actually need The compiler chooses the version based on the call (the number, order and types of actual parameters) There cannot be ambiguity in the call, or the compiler cannot choose the types of formal parameters to use Example: template void swap(T &x, T &y);

13 13 Template functions // swaps values of two parameters // the type T may be anything (int, string,...) template void swap (T &x, T &y) { T temp = x; x = y; y = temp; }

14 14 Template functions // searches A for min value in range [lo, hi] // works for any data type as long as // less than (<) operator is defined template int minPos(T A[], int lo, int hi) { int smallest = lo; for (int i=lo+1; i<=hi; i++) if (A[i] < A[smallest]) smallest = i; return smallest; }

15 15 Template functions // sorts arrays of any type of data template void selectionSort(T A[], int n) { for (int k=0; k<n-1; k++) { // scan unsorted sublist to find smallest value int smallIndex = minPos(A, k, n-1); // swap smallest value with leftmost if (smallIndex != k) swap(A[k],A[smallIndex]); }

16 16 One restriction The entire declaration and implementation of a template function must go in a header file Since the template is not actually a function as written, it Cannot be in a *.cpp file Cannot be separately compiled The template may not even be used if a call to the function does not appear somewhere in the code The call “instantiates” the function and establishes the types of data it manipulates

17 17 Template Example See files sortDemo2.cpp selectionSort() is called with different array types sortLib2.h Note: writeElements() is not written as a template It written as two separate versions (one for integers and one for strings) because the proper printw() format string (“%d” or “%s”) requires that the data type be known


Download ppt "1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions."

Similar presentations


Ads by Google