17
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 CSE 20232 Lecture 17 – Search & Sort Lecture 17 – Search & Sort

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

Embed Size (px)

Citation preview

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

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 20232CSE 20232Lecture 17 – Search & SortLecture 17 – Search & Sort

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

2

Reading

Random Numbers Section: 6.7

Overloading & Templates Sections: 6.17 – 6.18 14.1 – 14.3

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

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));

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

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 <iostream>#include <cstdlib> // 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;}

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

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 <cstdlib>// for rand() & srand()#include <iostream>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;}

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

6

Examples of using rand()// output a sequence of 100 values in range 0..MY_MAX// but have the user seed the sequence#include <cstdlib> // for rand() & srand()#include <iostream>#define MY_MAX 100using 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;}

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

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 <iostream>#include <cstdlib> // for rand() & srand()#include <ctime> // for time() function#define MY_MAX 100using 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;}

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

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

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

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 <iostream>#include <cstdlib>#include <string>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 <filename>” return 0;}

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

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

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

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);

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

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 <class T> void swap(T &x, T &y);

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

13

Template functions

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

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

14

Template functions

// searches A for min value in range [lo, hi]// works for any data type as long as // less than (<) operator is definedtemplate <class T>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; }

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

15

Template functions

// sorts arrays of any type of datatemplate <class T>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]); }}

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

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

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

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