Computer Programming CS F111

18
BITS Pilani Dubai Campus Computer Programming CS F111 NAND KUMAR

Transcript of Computer Programming CS F111

Page 1: Computer Programming CS F111

BITS Pilani Dubai Campus

Computer Programming CS F111

NAND KUMAR

Page 2: Computer Programming CS F111

BITS Pilani Dubai Campus

Basics of C Programming

Page 3: Computer Programming CS F111

BITS Pilani, Dubai Campus

Write a program that …

1. Asks 5 marks from the user, find the average

of the marks and print it with 2 digit precision.

2. Asks 2 Numbers and precision value, display

the sum with the specified precision

3. Asks the user to enter the radius of a circle

and calculates the Area of the circle as

Area=PI * radius * radius

display the Area with 4 digit precision

Page 4: Computer Programming CS F111

BITS Pilani, Dubai Campus

Function : getchar();

getc(stdin);

Syntax

variable_name = getchar();

variable_name = getc(stdin);

Example:

char ans;

ans = getchar();

ans = getc(stdin);

It accepts any character keyed in.

Reading a character

Page 5: Computer Programming CS F111

BITS Pilani, Dubai Campus

Function : putchar(int c);

putc(int c, stdout);

Syntax:

putchar(var_name);

putc(var_name, stdout);

Example:

char ans;

ans = ‘Y’;

putchar(ans);

Q. What is the effect of putchar(‘\n’);

Writing a character

Page 6: Computer Programming CS F111

BITS Pilani, Dubai Campus

Arithmetic Operators

Operator Meaning

+ Addition or unary plus

- Subtraction or unary minus

* Multiplication

/ Division

% Modulo Division

Page 7: Computer Programming CS F111

BITS Pilani, Dubai Campus

Let int x = 15; int y = 6;

Example:

x-y is 9

x+y is 21

x*y is 90

x/y is 2 (decimal part truncated)

x%y is 3 (remainder of the division)

Q. If x and y are declared as float then?

Q. If x is integer and y is float then?

Arithmetic Expression

Page 8: Computer Programming CS F111

BITS Pilani, Dubai Campus

Operator / and %

7.0 / 2.0 = 3.5

7.0 / 2 = 3.5

7 / 2.0 = 3.5

7 / 2 = 3

0 / 2 = 0

2 / 0 is Undefined

2 / -3 Varies

7.0 % 2.0 = error

error: invalid operands to binary %

7.0 % 2 = error

error: invalid operands to binary %

7 % 2.0 = error

error: invalid operands to binary %

7 % 2 = 1

For % operation, when the operands are negative, the output depends the compiler

GCC behaves the following way

-7 % -2 = -1

-7 % 2 = -1

7 % -2 = 1

7 % 2 = 1

7 % 0 is Undefined

Page 9: Computer Programming CS F111

BITS Pilani, Dubai Campus

What is Precedence and Associativity?

Operators have rules that are used to

determine how expressions are evaluated.

Precedence and associativity deal with the

evaluation order within expressions

Precedence rules specify the order in which

operators of different precedence level are

evaluated

Associativity rules decides the order in which

multiple occurrences of the same level

operator are applied.

Page 10: Computer Programming CS F111

BITS Pilani, Dubai Campus

1. First, parenthesized sub expression from left to right are evaluated.

2. If parentheses are nested, the evaluation begins with the innermost sub expression.

3. The precedence rule is applied in determining the order of application of operators in evaluating sub-expressions.

4. The associativity rule is applied when two or more operators of the same precedence level appear in a sub expression.

Rules of Evaluation of Expression

Page 11: Computer Programming CS F111

BITS Pilani, Dubai Campus

Two priority levels of operators

High priority

*, /, %

Low priority

+, -

Consider the expression

1+2*3

* has higher priority than +

So the value of the expression is 7

Precedence of Arithmetic Operators

Page 12: Computer Programming CS F111

BITS Pilani, Dubai Campus

Priority can be overruled by parenthesis (1+2)*3

Expression inside parenthesis are evaluated first

so (1+2)*3 gives 9

Now consider the expression 1+2-3+4-5

Note: + and – have the same precedence

Associativity rule is used to determine how to evaluate the above expression.

Associativity for Arithmetic Operators is L to R

Try: x = 10 - 16/4 + 4 * 3 - 4

Precedence of Arithmetic Operators

Page 13: Computer Programming CS F111

BITS Pilani, Dubai Campus

Rules for evaluating expressions

– Parenthesis Rule

• Evaluated separately. In case of nested parenthesis, evaluation order is from innermost to outermost

– Operator Precedence Rule

• Unary +,- : first

• *, /, % : next

• Binary +,- : last

– Associativity Rule

• Unary operators at same precedence : right associativity

• Binary operators at same precedence: left associativity

Expression with multiple operators

Find the order of evaluation: x * y * z + a / b - c * d

Page 14: Computer Programming CS F111

BITS Pilani, Dubai Campus

z = 2*3/4+4/4+8-2+5/7 z = ??

y = 4/5*6+2/7+4 y = ??

Let int a,b;

float c,d;

a=5;b=7;

c=4.0;d=3.0;

x = a/b*(c+d)/a-b+c/d x = ??

Examples: Evaluate the following expressions

Page 15: Computer Programming CS F111

BITS Pilani, Dubai Campus

What if the expression has multiple operators?

Consider area = PI * radius * radius ;

Page 16: Computer Programming CS F111

BITS Pilani, Dubai Campus

What if the expression has multiple operators?

Formula for velocity is v = (p2-p1) / (t2-t1)

Page 17: Computer Programming CS F111

BITS Pilani, Dubai Campus

What if the expression has multiple operators?

Consider an expression a=z- (a+b/2) + w * -y

Page 18: Computer Programming CS F111

BITS Pilani, Dubai Campus

Write the following formulas in C

1) b2-4ac

2) a+b

c+d

3) 1

1+x2

• Specify multiplication explicitly

using *

• Use parenthesis to control the

order of operator evaluation