COMP102 LAB 2 - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

COMP102 LAB 2

Description:

Fruits to be Charged. Collect number of fruits to be purchased from user ... for different fruits. You need to compute the actual number of fruits to be charged ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 7
Provided by: raym168
Category:
Tags: lab | comp102 | fruits

less

Transcript and Presenter's Notes

Title: COMP102 LAB 2


1
COMP102 LAB 2
2
Division (/ and )
  • Review
  • 7 / 2 3.5 or 31
  • 3 is the quotient and 1 is the remainder
  • In C, quotient and remainder could be obtained
    from / an operators
  • int quotient 7 / 2 // quotient 3
  • int remainder 7 2 // remainder 1
  • REMEMBER / operator performs differently on
    integers and non-integers
  • int/int gt int
  • double/double , int/double , double/int gt double

3
Random Functions
  • Initialization
  • Computers will always give the same output for
    the same inputs
  • Different output requires different inputs!
  • Time will usually be changed at different
    executions
  • It is used as an input parameter to initialize
    the random number generator
  • srand(time(0)) // need ltstdlib.hgt and
  • // lttime.hgt

4
Random Functions (Cont)
  • Generation
  • rand()
  • Generate a number between 0-RAND_MAX (32767 in
    VC)
  • How to generate a different range, say 50-100?
  • operator will return a number smaller than the
    divisor (i.e. x y gt between 0 and y -1)
  • operator will give an offset to the generated
    number
  • General solution (rand() num_possible_values)
    offset
  • How to choose num_possible_values and offset?

5
Truncation and Rounding
  • double-to-int conversion is usually done by
    truncation, i.e. extract the integral part from a
    real number
  • In this lab, the total amount is rounded so that
  • Total is rounded up if its fractional part gt0.5
  • Total is rounded down if its fractional part lt
    0.5
  • Can be done without if-statements
  • Hints How truncation is done? (rounding up if
    its fractional part gt???, rounding down if its
    fractional part lt ???)

6
Fruits to be Charged
  • Collect number of fruits to be purchased from
    user
  • There are buy x get 1 free for different fruits
  • You need to compute the actual number of fruits
    to be charged
  • Hints , -, , /, operators are good enough!
Write a Comment
User Comments (0)
About PowerShow.com