Lecture 13 Formatting and Random Numbers - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Lecture 13 Formatting and Random Numbers

Description:

There are built-in C/C functions for creating random numbers in a program (rand and srand) ... numbers the srand and rand create are actually related through ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 15
Provided by: jimand
Category:

less

Transcript and Presenter's Notes

Title: Lecture 13 Formatting and Random Numbers


1
Lecture 13Formatting andRandom Numbers
  • ENGR17 Engineering Programming
  • Section 1
  • Fall 2001

10/28/01
2
Outline
  • Formatting output
  • Generate random numbers using
  • srand
  • rand
  • time

3
Formatting Output
  • C Stream Manipulators
  • setprecision ( )
  • Specify number of decimal places to display in
    the outputs numbers
  • Syntax setprecision(number)
  • setiosflags( )
  • Used to control format of the numeric output
  • Syntax setiosflags(formatflag)
  • setw( )
  • Set width of the output field
  • setfill( )
  • Set fill character
  • Both manipulators are defined in a header file
    named include ltiomanip.hgt
  • See programming manuals for details

4
Formatting Example
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • void main()
  • int number 12
  • float real (float) 12.345
  • cout ltlt "1234567890\n"
  • cout ltlt number ltlt "lt-\n"
  • cout ltlt setw(5) ltlt number ltlt "lt-\n"
  • cout ltlt setw(5) ltlt setfill('') ltlt number ltlt
    "lt-\n"
  • cout ltlt setiosflags(iosshowposiosleft) ltlt
    setw(5) ltlt number ltlt "lt-\n"
  • cout ltlt real ltlt "lt-\n"
  • cout ltlt setprecision(1) ltlt setiosflags(iosfixed
    ) ltlt real
  • ltlt "lt-\n"
  • cout ltlt setiosflags(iosscientific) ltlt real ltlt
    "lt-\n"

5
Formatting Example
Screen Output
1234567890 12lt- 12lt- 12lt- 12lt- 12.345lt-
12.3lt- 1e001lt- Press any key to continue
There wont be a question on formatting on any of
the tests.
6
The displayPrices Function
7
Generating Random Numbers
  • Ideally, a random number generator can produce a
    sequence of completely unrelated numbers
  • Since a computer is by nature deterministic,
    creating truly random numbers is very difficult
    (if not impossible)
  • There are built-in C/C functions for creating
    random numbers in a program (rand and srand)
  • However, the built-in functions are extremely
    poor at creating truly random numbers
  • The numbers the srand and rand create are
    actually related through a fairly simple
    algorithm
  • But, for most everyday applications, its ok
  • Keep in mind that many other random number
    generators exist that are much better than the
    built-in functions

8
rand Function
  • Produces the random numbers
  • Also referred to as the random number generator
  • Generates integers from 0 through RAND_MAX
  • Syntax rand()
  • Defined in ltstdlib.hgt

9
srand Function
  • Initializes the random number generator
  • Syntax srand(seed)
  • time function typically used as the seed
  • Syntax for using the time function as the seed
    for the srand function time(NULL)
  • srand is defined in ltstdlib.hgt
  • time is defined in lttime.hgt

10
getRandomNumbers Function
  • Write a function that generates two random
    numbers, which should be stored in the firstNum
    and secondNum variables
  • Pass in a variable called level that serves the
    following function
  • If level1, then generate random numbers between
    1 and 10
  • Otherwise, generate random numbers between 10 and
    100
  • This type of function would be very useful in a
    game program

11
Generating Random Numbers
12
Summary
  • Formatting output streams
  • Generating random numbers

13
Example
  • Write a function to fill a 1D array with random
    numbers from 0 to 99
  • The array itself and the size of the array are
    passed to the function

14
Code
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • include lttime.hgt
  • void fillArray(int nums, int size)
  • void main()
  • int nums20
  • int size20
  • int i
  • fillArray(nums, size)
  • for(i0 iltsize i)
  • cout ltlt numsi ltlt endl
  • void fillArray(int nums, int size)
  • int i
  • srand(time(NULL))
  • for (i0 iltsize i)
Write a Comment
User Comments (0)
About PowerShow.com