Correction of the Handout - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Correction of the Handout

Description:

Correction of the Handout #include //Preprocessor using namespace std; int main (){ .. return 0;} A namespace is a named group of definitions. – PowerPoint PPT presentation

Number of Views:104
Avg rating:3.0/5.0
Slides: 20
Provided by: Compute77
Category:

less

Transcript and Presenter's Notes

Title: Correction of the Handout


1
Correction of the Handout
  • include ltiostreamgt //Preprocessor
  • using namespace std
  • int main ()
  • ..
  • return 0
  • A namespace is a named group of definitions. When
    objects that are defined within a namespace are
    used outside of that namespace, either their
    names must be prefixed with the name of the
    namespace or they must be in a block that is
    preceded by a using namespace statement.
    Namespaces make it possible for a program to use
    different objects with the same name, just as
    different people can have the same name. The cout
    object is defined within a namespace named std
    (for standard) in the ltiostreamgt header file.
  • Throughout the rest of this class, every program
    is assumed to begin with the two lines
  • include ltiostreamgt
  • using namespace std

2
Week 3
  • In this class, you will learn about
  • Program Structure, Actions and Data Types
  • Data Type Variables
  • Output and Input action function coutltlt cingtgt
  • Assignment
  • Simple calculations
  • Reference Programming with C, Theory and
    Problems of, Second Edition John R Hubbard
    (McGraw Hill, 2000, Schaum's Outline Series)

3
Data Type Variables
  • Variables
  • Name of a variable
  • A variable has a name, which is given by the
    programmer by declaration or definition. When the
    name is given, the memory for it is specified.
  • Rules for creating names of variables
  • Rule 1 a name starts with a letter.
  • Rule 2 a name is unique, which should be
    different from other words in the program.
  • Rule 3 names are sensitive to letters upper case
    or lower case.
  • Doug is not equivalent to doug.
  • Rule 4 names should be meaningful.

4
Data Type Variables
  • For example, int alice
  • name of variable alice
  • type int(eger)
  • tail

Using namespace std
5
Data Types
6
Part of Integral Type
7
Float type example
  • It shows that the 32 bits it uses to store a
    float are partitioned into 3 parts 23 bits for
    the mantissa, 8 bits for the exponent, and 1 bit
    for the sign. The 23-bit mantissa produces a
    floating-point value with 6 significant digits,
    and the 8-bit exponent yields a range in
    magnitude from about 1037 to about 3 1038.
    i.e., 0.0000000000000000000000000000000000001 lt
    x lt 300,000,000,000,000,000,000,000,000,000,000,
    000,000 for any variable x declared to have type
    float.

8
A new variable char (character data)
  • Definition
  • A char variable is defined to hold a single
    character that appears on the keyboard.
  • Difference between a char and a string is that a
    char is a single character while a string is a
    collection of serial characters.
  • Declaration
  • char c
  • Input a char variable from keyboard
  • A space cannot be input from keyboard

9
A new variable char (character data)
10
A new variable char (character data)
11
Output action function coutltlt
  • More actions by using a back slash \
  • The sign \ is not a normal character but a
    control character to instruct the computer to
    take an action corresponding to next character.
    See table below,
  • Escape characterEffect\nNewline\tHorizontal
    tab\rCarriage return\bBackspace\aBell or
    beep\\Backslash\Double quote\Single
    quote\?Question mark

12
Input action input function cingtgtAssignment
  • We have seen that one way of assigning a variable
    with a value is to input a number of digits on
    keyboard. Another important way is to use an
    assignment statement. For example
    (assignment1.cpp),

13
Simple calculations
14
Simple calculations
15
Integer Calculation Example
  • int main()
  • // tests operators ,-,,/,and
  • int m54
  • int n20
  • cout ltlt "m " ltlt m ltlt " and n " ltlt n ltlt endl
  • cout ltlt "mn " ltlt mn ltlt endl // 5420 74
  • cout ltlt "m-n " ltlt m-n ltlt endl // 54-20 34
  • cout ltlt "mn " ltlt mn ltlt endl // 5420 1080
  • cout ltlt "m/n " ltlt m/n ltlt endl // 54/20 2
  • cout ltlt "mn " ltlt mn ltlt endl // 5420 14
  • Return 0

16
Prefix and postfix increment
  • Examplek x//prefix increment
  • is equivalent to
  • x x 1 //increment n first
  • k x//assign xs value to k
  • Examplek x//postfix increment
  • is equivalent to
  • k x//assign xs value to k
  • x x 1//and then increment x

17
Applying the Pre-increment and Post-increment
Operators
  • int main()
  • // shows the difference between m and m
  • int m,n
  • m 44
  • n m // the pre-increment operator is applied
    to m
  • cout ltlt "m " ltlt m ltlt ", n " ltlt n ltlt endl
  • m 44
  • n m // the post-increment operator is
    applied to m
  • cout ltlt "m " ltlt m ltlt ", n " ltlt n ltlt endl

18
COMPOSITE ASSIGNMENT OPERATORS
  • The standard assignment operator in C is the
    equals sign . In addition to this operator, C
    also includes the following composite assignment
    operators , -, , /, and .
  • When applied to a variable on the left, each
    applies the indicated arithmetic operation to it
    using the value of the expression on the right.

19
Applying Composite Arithmetic Assignment Operators
  • int main()
  • // tests arithmetic assignment operators
  • int n22
  • cout ltlt "n " ltlt n ltlt endl
  • n 9 // adds 9 to n
  • cout ltlt "After n 9, n " ltlt n ltlt endl
  • n - 5 // subtracts 5 from n
  • cout ltlt "After n - 5, n " ltlt n ltlt endl
  • n 2 // multiplies n by 3
  • cout ltlt "After n 2, n " ltlt n ltlt endl
  • n / 3 // divides n by 9
  • cout ltlt "After n / 3, n " ltlt n ltlt endl
  • n 7 // reduces n to the remainder from
    dividing by 7
  • cout ltlt "After n 7, n " ltlt n ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com