Vous êtes sur la page 1sur 11

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #6, August 04, 2011

Please switch off your mobile phones.

Announcements
Tutorial on algorithm design by Prof Somenath Biswas on Prof. Saturday, 6th August at 10:30 AM in CS building. Tutorial in Hindi by Arpan Maheshwari on Sunday, 7th August at 5:00 PM in CS building. Monday lab scheduled on 15th August will instead be held on Saturday, 20th August. M d l b scheduled on 22nd A Monday lab h d l d August will instead be held on t ill i t d b h ld th August. Saturday, 27 Wednesday lab scheduled on 31st August will instead be held on Saturday, 3rd September.
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 1

Recap
Variables names, types, sizes names types Comments printf, scanf Arithmetic operators Arithmetic expressions Precedence, Associativity y

Lec-06

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

Recap: Arithmetic Operators


Operator + * / % Meaning Addition Subtraction Multiplication Division Modulus int Yes Yes Yes Yes (integer) Yes float Yes Yes Yes Yes No

71 / 6 = 11 71 % 6 = 5 71.0 / 6.0 = 11.83333 71 / 6.0 = 11.83333 71.0 / 6 = 11.83333


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

Lec-06

Recap: Arithmetic Expressions


An Arithmetic expression is any legal combination of variables, constants, and arithmetic operators: 12 + 6 / 3 = 14 / evaluated earlier than + due to higher precedence 24 / 6 *2 = 8 / is of same precedence as *, so evaluate left to right 24 / 6 / 2 = 2 / is evaluated from left to right 24 6 2 = 16 - is evaluated form left to right Brackets are needed to enforce particular order (BODMAS) (12 + 6) / 3 = 6
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 4

Recap: Precedence and Associativity


Precedence: among multiple operators, precedence rules guide the order in which they will be evaluated Operators having higher precedence are evaluated earlier 12 + 6 / 3 is really 12 + (6 / 3) Associativity: among multiple instances of same operator or operators of same precedence, associativity rules guide the order of their evaluation As all arithmetic operators are left-to-right associative, the leftmost instance of the operator is evaluated first, and so on 12 6 3 is really (12 6) 3 Good programming habit: avoid long expressions without brackets. Should know the rules, but not think about them while programming

Lec-06

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

Functions
We are eventually going to organize our large programs into functions. A function is part of the program that takes some p p p g parameters, , does some processing, and returns some result. For now, we will define only one function in our programs, main() All C programs start their execution from the main function. We will use functions written by someone else and provided by the system printf, scanf, etc. Information about them is in header files, which were mentioned after #include Compiler somehow includes them in your program
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6

More functions
Several mathematical functions supported by system. system In Thursday and Friday labs, you have to use sqrt()
takes a floating point number a parameter, and returns a floating point number which is the square-root of the parameter. the header file corresponding to this function has to be added
#include <math.h> <math h>

The compiler also needs to be given an option:


gcc -lm

Lec-06

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

Assignment Statement
A statement is some instruction followed by a semicolon (;) Assignment statement looks like: mass = 20 + 5; There is a variable name on the left hand side The operator = is in the middle An expression is on the right hand side Execution: The expression on the right hand side is evaluated The value is stored in the memory area whose name is given by the variable
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8

More Assignments
Variables can appear on the right hand side as well, e.g., well e g
j = 5; i = j * 2;

This will place number 5 in the memory area represented by j and number 10 in the memory area represented by i. k = 7 * i + 36 / j * 2 ;

Lec-06

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

Variable Initialization
Variables can be initialized at the time of declaration. declaration The initial value is stored in the memory corresponding to that variable. For example: int year = 2011; float average = 8.5; long huge_number = 5678901234;

Lec-06

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

10

Assignments: Type Mismatch


int i; i = 10.2; (i becomes 10) ( ) float f; f = 5; (f becomes 5.0) int j = 23; float x = 10.45; j = x; x = j; (j becomes 10, x becomes 10.0)
Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 11

float y; y = 20 /3 (y becomes 6.0) ) float x = 12.3, y = 26.7; int n; n = y / x; (n becomes 2)

Lec-06

if statement
Making decisions The general form of the statement is:
if ( expression ) <program statement>

Example: Find the minimum of two integers Algorithm:


Read two numbers, x and y if (x < y) then min = x otherwise, min = y
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 12

C program to find minimum of two numbers


#include <stdio.h> main () { int x, y; // two numbers to be read int min; // to store minimum of the two scanf (%d %d, &x, &y); // read two numbers if (x <= y) min = x; // if x is smaller, minimum is x if (x > y) min = y; // if y is smaller minimum is y smaller, printf (Minimum of the two numbers is %d\n, min); // print the minimum }
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 13

if - else
If your algorithm requires that you take some action if the condition is true, and some other action if the condition is false, then a more general statement is: if ( condition ) {
statements1 } else { statements2 }
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 14

C program to find minimum of two numbers


#include <stdio.h> main () { int i x, y; // two numbers to be read b b d int min; // to store minimum of the two scanf (%d %d, &x, &y); // read two numbers if (x < y) min = x // if x is smaller, minimum is x else min = y; // otherwise minimum is y otherwise, printf (Minimum of the two numbers is %d\n, min); // print the minimum }
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 15

Relational Operators

Operator O t == < <= > >= !=

Meaning M i Equal to Less than Less than or Equal to Greater than Greater than or Equal to q Not equal to

Example E l count == 10 a<b low <= high age > 18 j >= 0 marks != 0

Lec-06

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

16

Understanding if
Condition must evaluate to a boolean (true or false) value When condition is true, the if part is executed Otherwise (i.e., when condition is false), the else part is executed It is not necessary to have else part Multiple statements can be used in either if part or else part or both.

Lec-06

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

17

Block of statements in if else


if ( condition ) { statement 1 statement 2 } else { statement 3 statement 4 }
Lec-06 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 18

Checking multiple conditions


Problem: Read an integer, and print whether it is postive, integer postive negative, or zero. Read a number, N. if ( N < 0) print (negative) else if (N > 0) print (positive) else print (zero)

Lec-06

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

19

10

Any Questions?

Lec-06

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

20

11

Vous aimerez peut-être aussi