Presentation is loading. Please wait.

Presentation is loading. Please wait.

Revision. Generating a pseudo-random number Necessary libraries: and Seeding: srand(time(NULL))  We set the seed based on the current time  NB! Time-dependent.

Similar presentations


Presentation on theme: "Revision. Generating a pseudo-random number Necessary libraries: and Seeding: srand(time(NULL))  We set the seed based on the current time  NB! Time-dependent."— Presentation transcript:

1 Revision

2 Generating a pseudo-random number Necessary libraries: and Seeding: srand(time(NULL))  We set the seed based on the current time  NB! Time-dependent seed changes once a second. Don’t use it in a loop! Generating a random number: rand() function  Function returns a random number  The returned value is a positive integer  The number is between 0 … RAND_MAX (the #define comes from stdlib.h library) 20152

3 Sample code to get a pseudo-random number #include int main(void) { srand(time(NULL)); // set the seed, we do this ONLY ONCE! int randNum; randNum = rand(); // call the random number function, store the return printf("max is %d\n", RAND_MAX); // largest possible random number (can’t be changed) printf(“Got: %d\n", randNum); // print out the number that rand() function returned printf(“Got: %d\n", rand()); // print out another number that rand() function returns return 0; } 20153

4 Lab task #1: lottery Lotto numbers should be in between 1 … 30 The user is asked for 6 numbers (can also be given from a file) Program generates 10 numbers at random Display the winning numbers Check and output how many of the numbers matched If all 6 matched, congratulate the user on winning the jackpot Give your condolence when none of the numbers matched 20154

5 Lab task #2: theatre hall This task is targeted at the representation of data The seating plan of the theatre hall and the venue is stored in a file. Choose a notation for your seats. It will represent the status of the seat and can only be 1 symbol per seat (dashes and line changes are allowed). You must identify 3 different statuses The seat does not exist (seat placement narrows down closer to the screen) Open seat Booked / sold out seat The number of seats in a row decreases the closer it is to the screen. The seats however must still start from 1 20155

6 Lab task #2: example 20156

7 Advanced Lab task #1 Verify that the numbers user enters are unique If the user repeats a number, ask again. Only ask for the current number, don’t ask again for suitable ones. Verify, that the numbers generated randomly wouldn’t repeat Lab task #2 Choose an extension for your program It can be to display multiple halls, different times or movies etc. Make changes to the seating arrangement or booking statuses to show difference. Do a seat check – can you book it? NB! When the input stream is redirected to file, you can’t enter anything from the keyboard. The query must also be written in the file until we cover managing files directly. 20157


Download ppt "Revision. Generating a pseudo-random number Necessary libraries: and Seeding: srand(time(NULL))  We set the seed based on the current time  NB! Time-dependent."

Similar presentations


Ads by Google