esc101-lec9

Upload: mukesh-kumar-dewra

Post on 05-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 esc101-lec9

    1/7

    1

    ESc101: Fundamentals of Com utin

    2011-12-Monsoon Semester

    Lecture #9, August 11, 2011

    Please switch off your mobile phones.

    Announcements

    th

    Saturday, 20th August.

    Monday lab scheduled on 22nd August will instead be held onSaturday, 27th August.

    Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    1

    Wednesday lab scheduled on 31st August will instead be held

    on Saturday, 3rd September.

  • 7/31/2019 esc101-lec9

    2/7

    2

    Recap

    while statement

    for statement

    Logical operators

    Example programs

    Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    2

    Getting out of loop

    One can get out of loop without checking condition to be false,

    by using a breakstatement

    for ( x = 101; x

  • 7/31/2019 esc101-lec9

    3/7

    3

    Skipping part of an iteration of the loop

    Compute average of non-negative numbers

    ,

    count = 0; sum = 0; // count and sum of non-negative numbers

    for (i = 1; i

  • 7/31/2019 esc101-lec9

    4/7

    4

    How is character stored in memory

    Normally between 0 and 127

    The mapping used by our PCs is called ASCII

    American Standard Code for Information Interchange

    A few examples:

    a is stored as 97 (and successive small-case letters by

    consecutive integers, z is 122)

    A is stored as 65 (Z is 90)

    0 is stored as 48 (9 as 57)

    Note that 0 is different from 0.

    Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    6

    Character Representation

    program

    But you can assume that

    All small case letters will be stored as consecutive numbers

    The difference in z and a will always be 25

    All capital case letters will be stored as consecutive numbers

    All digits will be stored as consecutive numbers

    The difference between 6 and 3 will always be 3

    Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    7

  • 7/31/2019 esc101-lec9

    5/7

    5

    Printing a character

    .

    char gender;

    gender = M;

    printf(The gender is: %c\n, gender);

    The above code will print the following:

    The gender is: M

    Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    8

    Reading a character

    read.

    scanf(%c, &c)

    What if there are multiple characters to be read?

    Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    9

  • 7/31/2019 esc101-lec9

    6/7

    6

    Inputting Multiple Characters: Version 1

    char a, b, c;

    rintf T e first character\n

    scanf(%c, &a);

    printf(Type second character\n);

    scanf(%c, &b);

    printf(Type third character\n);

    scanf(%c, &c);

    printf(%c\n%c\n%c\n, a, b, c);

    Does not Work!

    Enter is read as a

    character.

    Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    10

    Inputting Multiple Characters: Version 2

    char a, b, c;

    printf(Type three characters\n);

    scanf(%c, &a);

    scanf(%c, &b);scanf(%c, &c);

    printf(%c\n%c\n%c\n, a, b, c);

    You have to type all three characters together.

    Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    11

  • 7/31/2019 esc101-lec9

    7/7

    7

    Inputting Multiple Characters: Version 3

    char a, b, c;

    rintf T e first character\n

    scanf(%c, &a);

    printf(Type second character\n);

    scanf( %c, &b);

    // Added a space before %c

    printf(Type third character\n);

    scanf( %c, &c);

    // Added a space before %c

    printf(%c\n%c\n%c\n, a, b, c);

    Works!

    We could fix the

    newline problem.

    Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    12

    Wishing you all a very

    happy Rakhi and

    Independence Day

    Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    13