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.

Slides:



Advertisements
Similar presentations
UNIT 12 UNIX I/O Redirection.
Advertisements

A file reminder Files are used to store data and information They are manipulated through a pointer to FILE (FILE *) This pointer is returned when opening.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
1 Session-23 CSIT 121 Spring 2006 Revision Ch 7: Reference Parameters Revision Ch 7: Reference Parameters Chapter 8: Value Returning Functions Chapter.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises.
1 Lab Session-9 CSIT-121 Fall 2003 w Random Number Generation w Designing a Game.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
1 Lab Session-7 CSIT-121 Fall Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises.
CHAPTER 1: INTORDUCTION TO C LANGUAGE
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
1 Lecture 3 Part 1 Functions with math and randomness.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be.
Week 7 - Wednesday.  What did we talk about last time?  scanf()  Memory allocation  malloc()  free()
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
End of unit assessment Challenge 1 & 2. Course summary So far in this course you have learnt about and used: Syntax Output to screen (PRINT) Variables.
WEEK 6 Class Activities Lecturer’s slides.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Chapter 7 Problem Solving with Loops
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:
String operations. About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here:
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
UNIT 11 Random Numbers.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
Function Call Stack and Activation Frame Stack Just like a pile of dishes Support Two operations push() pop() LIFO (Last-In, First-Out) data structure.
GCSE Computing: Programming GCSE Programming Remembering Python.
Engineering Problem#1 Engr201 Computer Programming for Engineers Faculty of Engineering Chiang Mai University 1.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 Generating Random Numbers Textbook ch.6, pg
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Lesson #5 Repetition and Loops.
Chapter 9: Value-Returning Functions
Lesson #5 Repetition and Loops.
Number guessing game Pick a random number between 1 and 10
CS1010 Programming Methodology
Lab Session-9 CSIT-121 Spring 2005
Arrays An Array is an ordered collection of variables
Lesson #5 Repetition and Loops.
A First Book of ANSI C Fourth Edition
Conditionals.
Week 6 CPS125.
Files.
Functions.
String manipulation string.h library
Functions continued.
Loops.
Arrays.
Lesson #5 Repetition and Loops.
Review Lab assignments Homework #3
Learning Intention I will learn about the standard algorithm for input validation.
Revision.
GCSE Computing Mini Assignment.
Presentation transcript:

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

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

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

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

Lab task #2: example 20156

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