problem solving & c m. tech. (cs) 1st year, 2014 · problem solving & c m. tech. (cs) 1st...

24
C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu [email protected] Indian Statistical Institute, India. July 24, 2014

Upload: others

Post on 08-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Problem Solving & CM. Tech. (CS) 1st year, 2014

Arijit [email protected]

Indian Statistical Institute, India.

July 24, 2014

Page 2: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Outline

1 C as an imperative language

2 sizeof Operator in C

3 Use of some other operator

4 Problems

5 Functions and recursion

Page 3: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Outline

1 C as an imperative language

2 sizeof Operator in C

3 Use of some other operator

4 Problems

5 Functions and recursion

Page 4: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

A brief introduction

C is an imperative language.

Imperative programs consist of

a program state – how is it encoded?instructions/commands that change the program state – whatare they? (assignment statements, conditionals, loop,procedures)

Instructions in an imperative language are similar to thenative machine instructions of traditional computer hardware.

A bit of history – John von Neumann’s stored programcomputers and Eckert & Mauchly’s contribution. The mainidea is that machine can store both data and instructionsindistinguishably.

Page 5: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

A brief introduction

C is an imperative language.

Imperative programs consist of

a program state – how is it encoded?instructions/commands that change the program state – whatare they? (assignment statements, conditionals, loop,procedures)

Instructions in an imperative language are similar to thenative machine instructions of traditional computer hardware.

A bit of history – John von Neumann’s stored programcomputers and Eckert & Mauchly’s contribution. The mainidea is that machine can store both data and instructionsindistinguishably.

Page 6: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

A brief introduction

C is an imperative language.

Imperative programs consist of

a program state – how is it encoded?

instructions/commands that change the program state – whatare they? (assignment statements, conditionals, loop,procedures)

Instructions in an imperative language are similar to thenative machine instructions of traditional computer hardware.

A bit of history – John von Neumann’s stored programcomputers and Eckert & Mauchly’s contribution. The mainidea is that machine can store both data and instructionsindistinguishably.

Page 7: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

A brief introduction

C is an imperative language.

Imperative programs consist of

a program state – how is it encoded?instructions/commands that change the program state – whatare they? (assignment statements, conditionals, loop,procedures)

Instructions in an imperative language are similar to thenative machine instructions of traditional computer hardware.

A bit of history – John von Neumann’s stored programcomputers and Eckert & Mauchly’s contribution. The mainidea is that machine can store both data and instructionsindistinguishably.

Page 8: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

A brief introduction

C is an imperative language.

Imperative programs consist of

a program state – how is it encoded?instructions/commands that change the program state – whatare they? (assignment statements, conditionals, loop,procedures)

Instructions in an imperative language are similar to thenative machine instructions of traditional computer hardware.

A bit of history – John von Neumann’s stored programcomputers and Eckert & Mauchly’s contribution. The mainidea is that machine can store both data and instructionsindistinguishably.

Page 9: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

A brief introduction

C is an imperative language.

Imperative programs consist of

a program state – how is it encoded?instructions/commands that change the program state – whatare they? (assignment statements, conditionals, loop,procedures)

Instructions in an imperative language are similar to thenative machine instructions of traditional computer hardware.

A bit of history – John von Neumann’s stored programcomputers and Eckert & Mauchly’s contribution. The mainidea is that machine can store both data and instructionsindistinguishably.

Page 10: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Outline

1 C as an imperative language

2 sizeof Operator in C

3 Use of some other operator

4 Problems

5 Functions and recursion

Page 11: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

A unary operator – sizeof

#include<stdio.h>int main(void){printf("\nSize of char, unsigned char:> %u, %u",sizeof(char),sizeof(unsigned char));printf("\nSize of int, short int, long int:>%u, %u, %u",sizeof(int),sizeof(short int),sizeof(long int));printf("\nSize of float:> %u",sizeof(float));printf("\n Size of double, long double:> %u, %u",sizeof(double),sizeof(long double));printf("\nSize of uint:> %u",sizeof(unsigned int));return 0;}

Page 12: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Exercise

Exercise

Write a program to determine the ranges of char, int, float,double both for unsigned and signed cases; also consider shortand long data types, wherever possible.

Page 13: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Outline

1 C as an imperative language

2 sizeof Operator in C

3 Use of some other operator

4 Problems

5 Functions and recursion

Page 14: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Swapping two variables

Problem

Swap the contents of the two variables without using any extravariable.

The program

#include<stdio.h>int main(void){

int a = 6, b = 9;a = a - b;b = a + b;a = b - a;printf("\n a = %d, b = %d \n",a,b);return 0;

}

Any problem?

Can you detect any problem here?

Page 15: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Swapping two variables

Problem

Swap the contents of the two variables without using any extravariable.

The program

#include<stdio.h>int main(void){int a = 6, b = 9;a = a - b;b = a + b;a = b - a;printf("\n a = %d, b = %d \n",a,b);return 0;

}

Any problem?

Can you detect any problem here?

Page 16: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Swapping two variables

Problem

Swap the contents of the two variables without using any extravariable.

The program

#include<stdio.h>int main(void){int a = 6, b = 9;a = a - b;b = a + b;a = b - a;printf("\n a = %d, b = %d \n",a,b);return 0;

}

Any problem?

Can you detect any problem here?

Page 17: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Another way of swapping

Taking recourse to Boolean algebra

XOR is a boolean operation on two Boolean variables x and y ,denoted as x ⊕ y . x ⊕ y = xy + xy . Find out what are y ⊕ (x ⊕ y)and x ⊕ (x ⊕ y)?

Exercise

Taking recourse to the above, can you rewrite the program usingthe bitwise operator XOR ˆ in C?

Other bitwise operators

There are other bitwise operators & (AND), | (OR), << (leftshift), >> (right shift) and the unary one’s complement ∼ in C.

Page 18: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Another way of swapping

Taking recourse to Boolean algebra

XOR is a boolean operation on two Boolean variables x and y ,denoted as x ⊕ y . x ⊕ y = xy + xy . Find out what are y ⊕ (x ⊕ y)and x ⊕ (x ⊕ y)?

Exercise

Taking recourse to the above, can you rewrite the program usingthe bitwise operator XOR ˆ in C?

Other bitwise operators

There are other bitwise operators & (AND), | (OR), << (leftshift), >> (right shift) and the unary one’s complement ∼ in C.

Page 19: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Another way of swapping

Taking recourse to Boolean algebra

XOR is a boolean operation on two Boolean variables x and y ,denoted as x ⊕ y . x ⊕ y = xy + xy . Find out what are y ⊕ (x ⊕ y)and x ⊕ (x ⊕ y)?

Exercise

Taking recourse to the above, can you rewrite the program usingthe bitwise operator XOR ˆ in C?

Other bitwise operators

There are other bitwise operators & (AND), | (OR), << (leftshift), >> (right shift) and the unary one’s complement ∼ in C.

Page 20: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Outline

1 C as an imperative language

2 sizeof Operator in C

3 Use of some other operator

4 Problems

5 Functions and recursion

Page 21: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Some problems to solve

GCD

The greatest common divisor gcd(a, b) of two positive integersa >= b is the largest natural number of which both a and b areintegral multiples. Write a iterative program to compute gcd(a, b).

Fibonacci sequence

Given a positive integer n, find F (n), where F (n) denotes theFibonacci sequence. Do not use recurrence.

F (n) = 0 if n = 0

= 1 if n = 1

= F (n − 1) + F (n − 2) if n ≥ 2.

Page 22: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Some problems to solve

GCD

The greatest common divisor gcd(a, b) of two positive integersa >= b is the largest natural number of which both a and b areintegral multiples. Write a iterative program to compute gcd(a, b).

Fibonacci sequence

Given a positive integer n, find F (n), where F (n) denotes theFibonacci sequence. Do not use recurrence.

F (n) = 0 if n = 0

= 1 if n = 1

= F (n − 1) + F (n − 2) if n ≥ 2.

Page 23: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Another problem

Integer exponentiation

The problem is to raise a real number x to the n-th power, wheren is a non-negative integer. First, write a non-recursive program tocompute xn. This method requires n − 1 multiplications. Now, wewill try to do better. Let m = bn/2c, and suppose we know how tocompute xm. Then, we have two cases: if n is even, thenxn = (xm)2, otherwise, xn = x × (xm)2. Now, write a recursiveprogram to compute xn.

Page 24: Problem Solving & C M. Tech. (CS) 1st year, 2014 · Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. July 24,

C as an imperative language sizeof Operator in C Use of some other operator Problems Functions and recursion

Outline

1 C as an imperative language

2 sizeof Operator in C

3 Use of some other operator

4 Problems

5 Functions and recursion