You are on page 1of 7

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #9, August 11, 2011

Please switch off your mobile phones.

Announcements
Monday lab scheduled on 15th August will instead be held on Saturday, 20th August. Monday lab scheduled on 22nd August will instead be held on Saturday, 27th August. Wednesday lab scheduled on 31st August will instead be held on Saturday, 3rd September.

Lec-09

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Loops
while statement for statement

Logical operators Example programs

Lec-09

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Getting out of loop


One can get out of loop without checking condition to be false, by using a break statement Find the first number between 101 and 200 that is divisible by 7 for ( x = 101; x <= 200; x = x +1) { if ((x % 7) == 0) { printf ( d\ x); i f (%d\n, ) break; } }
Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 3

Skipping part of an iteration of the loop


Compute average of non-negative numbers
scanf (%d, &n); ( %d // Total number of inputs count = 0; sum = 0; // count and sum of non-negative numbers for (i = 1; i <= n; i = i + 1) // Iterate n times { printf (next number = ?); scanf (%d, &x); // Read the next number if (x < 0) continue; ( ) ; // if negative, no p g , processing g sum = sum + x; // if non-negative, add to sum count = count + 1; // if non-negative, increment count } printf (Average of non-negative numbers is %d\n, sum/count);
Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 4

Character
char data type One byte of memory Stores a single character
char c; c = a; c = 1;

Lec-09

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

How is character stored in memory


Each character is represented by a small number
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) g , ) 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

Character Representation
Cannot assume a specific character representation in your 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


The difference between D and B will always be 2 D B

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

Printing a character
%c is used to indicate to printf that a character is to be printed. printed
char gender; gender = M; printf (The gender is: %c\n, gender);

Th above code will print the following: The b d ill i h f ll i


The gender is: M
Lec-09 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8

Reading a character
%c is used to indicate scanf that a single character is to be 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

Inputting Multiple Characters: Version 1


char a, b, c; printf (Type first character\n); ( Type 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.


10

Lec-09

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Inputting Multiple Characters: Version 2


char a, b, c; printf (Type three characters\n); scanf (%c, &a); scanf (%c, &b); scanf (%c, &c); p 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

Inputting Multiple Characters: Version 3


char a, b, c; printf (Type first character\n); ( Type 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.


12

Lec-09

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Wishing you all a very happy Rakhi and Independence Day

Lec-09

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

13

You might also like