Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu - - PowerPoint PPT Presentation

problem solving c m tech cs 1st year 2014
SMART_READER_LITE
LIVE PREVIEW

Problem Solving & C M. Tech. (CS) 1st year, 2014 Arijit Bishnu - - PowerPoint PPT Presentation

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 arijit@isical.ac.in Indian Statistical Institute, India. July


slide-1
SLIDE 1

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 arijit@isical.ac.in

Indian Statistical Institute, India.

July 24, 2014

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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.

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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?

slide-7
SLIDE 7

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 – what are they? (assignment statements, conditionals, loop, procedures)

slide-8
SLIDE 8

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 – what are they? (assignment statements, conditionals, loop, procedures)

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

slide-9
SLIDE 9

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 – what are they? (assignment statements, conditionals, loop, procedures)

Instructions in an imperative language are similar to the native machine instructions of traditional computer hardware. A bit of history – John von Neumann’s stored program computers and Eckert & Mauchly’s contribution. The main idea is that machine can store both data and instructions indistinguishably.

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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; }

slide-12
SLIDE 12

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 short and long data types, wherever possible.

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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 extra variable.

slide-15
SLIDE 15

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 extra variable. 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; }

slide-16
SLIDE 16

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 extra variable. 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?

slide-17
SLIDE 17

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 = x¯ y + ¯

  • xy. Find out what are y ⊕ (x ⊕ y)

and x ⊕ (x ⊕ y)?

slide-18
SLIDE 18

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 = x¯ y + ¯

  • xy. Find out what are y ⊕ (x ⊕ y)

and x ⊕ (x ⊕ y)? Exercise Taking recourse to the above, can you rewrite the program using the bitwise operator XOR ˆ in C?

slide-19
SLIDE 19

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 = x¯ y + ¯

  • xy. Find out what are y ⊕ (x ⊕ y)

and x ⊕ (x ⊕ y)? Exercise Taking recourse to the above, can you rewrite the program using the bitwise operator XOR ˆ in C? Other bitwise operators There are other bitwise operators & (AND), | (OR), << (left shift), >> (right shift) and the unary one’s complement ∼ in C.

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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 integers a >= b is the largest natural number of which both a and b are integral multiples. Write a iterative program to compute gcd(a, b).

slide-22
SLIDE 22

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 integers a >= b is the largest natural number of which both a and b are integral multiples. Write a iterative program to compute gcd(a, b). Fibonacci sequence Given a positive integer n, find F(n), where F(n) denotes the Fibonacci sequence. Do not use recurrence. F(n) = 0 if n = 0 = 1 if n = 1 = F(n − 1) + F(n − 2) if n ≥ 2.

slide-23
SLIDE 23

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, where n is a non-negative integer. First, write a non-recursive program to compute xn. This method requires n − 1 multiplications. Now, we will try to do better. Let m = ⌊n/2⌋, and suppose we know how to compute xm. Then, we have two cases: if n is even, then xn = (xm)2, otherwise, xn = x × (xm)2. Now, write a recursive program to compute xn.

slide-24
SLIDE 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