+ All Categories
Home > Documents > Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and...

Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and...

Date post: 06-Oct-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
51
Introduction to Programming Lecture Three N.R.Aravind I.I.T. Hyderabad 26 Sep 2017 1 / 38
Transcript
Page 1: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Introduction to ProgrammingLecture Three

N.R.Aravind

I.I.T. Hyderabad

26 Sep 2017

1 / 38

Page 2: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Topics in this lecture

Review

Binary arithmetic

Arrays

while Loop examples

2 / 38

Page 3: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Review

3 / 38

Page 4: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Compiling and running a C program

$ gcc helloWorld.c -o hello

gcc: Gnu C Compiler

Translates the C program into machinecode named “hello”

-o: specifies the outpiut file name

$ ./hello

Run (execute) the program named ”hello”

To run a file named ”xyz”, type ./xyz.

4 / 38

Page 5: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Lab guidelines

1 Write programs on paper before the lab.2 Save programs (pen-drive, email, g-drive)3 Meaningful program names:

addition.c, circle.c, helloWorld.c, quiz.c,maximum.c

4 Avoid: abcd.c, ramakrishna.c,cs15btech1001.c, program1.c, progam2.c

5 Meaningful variable names:radius, area, count, username, country, length

6 Take breaks!

5 / 38

Page 6: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Lab reschedule

Next Mon lab ⇒ This Wed, 12:00 and 4:00 p.m.

6 / 38

Page 7: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

if ( ) {...} else {...}int a=5,b=15;if (!(a > 7)){

printf(” Hello”);}else{

printf(” Welcome”);}if (!(b==15)){

printf(” Bye”);}

7 / 38

Page 8: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Exercise 3: printSquares.c

int count, N, square; // Accept the value of N.count=1;while (square<=N){

square=count*count;printf(”\n %d”,square);count=count+1;}

// Output statements

8 / 38

Page 9: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Exercise 3: printSquares.c

int count, N; // Accept the value of N.count=1;while (count*count<=N){

printf(”\n %d”, count*count);count=count+1;}

// Output statements// Two multiplications

9 / 38

Page 10: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Exercise 3: printSquares.c

int count, N, square; // Accept the value of N.count=1;square=0;while (square<=N){

square=square+2*count-1;printf(”\n %d”,square);count=count+1;}

// Output statements

10 / 38

Page 11: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Useful basic block

i=1;while(i<20){// Some statementsi=i+1;}

11 / 38

Page 12: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Useful basic block

sum=0;i=1;while(i<20){sum=sum+i;i=i+1;}

12 / 38

Page 13: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Useful basic block

product=1;i=1;while(i<20){product=product*i;i=i+1;}

13 / 38

Page 14: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Useful block for strings

i=0;while(text[i]!=’\0’){printf(”\n %d”,text[i]”);i=i+1;}

14 / 38

Page 15: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Binary Arithmetic

15 / 38

Page 16: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Decimal

4716 = 4× 103 + 7× 102 + 1× 101 + 6× 100.

4716=6+10+700+4000

583=3+80+500

Decimal: Multiply by 1, 10, 100, 1000 etc.(right-to-left)

In binary, we multiply by 1, 2, 4, 8 etc.(right-to-left)

16 / 38

Page 17: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Binary to Decimal

Multiply by 1, 2, 4, 8 etc. (right-to-left)

(1101)2 = 1 + 4 + 8 = 13.

(1101)2 = 1× 23 + 1× 22 + 0× 21 + 1× 20.

In binary, (100)2 = 4 and (111)2 = 7.

17 / 38

Page 18: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Binary to decimal

1012 =

5.

10002 = 8.

11102= 14.

101012=21.

18 / 38

Page 19: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Binary to decimal

1012 = 5.

10002 = 8.

11102= 14.

101012=21.

18 / 38

Page 20: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Binary to decimal

1012 = 5.

10002 =

8.

11102= 14.

101012=21.

18 / 38

Page 21: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Binary to decimal

1012 = 5.

10002 = 8.

11102= 14.

101012=21.

18 / 38

Page 22: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Binary to decimal

1012 = 5.

10002 = 8.

11102=

14.

101012=21.

18 / 38

Page 23: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Binary to decimal

1012 = 5.

10002 = 8.

11102= 14.

101012=21.

18 / 38

Page 24: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Binary to decimal

1012 = 5.

10002 = 8.

11102= 14.

101012=

21.

18 / 38

Page 25: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Binary to decimal

1012 = 5.

10002 = 8.

11102= 14.

101012=21.

18 / 38

Page 26: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Digits: right-to-left

4716 = 10× 471 + 6

471 = 10× 47 + 1

47 = 10× 4 + 7

4 = 10× 0 + 4.

14 = 2× 7 + 0

7 = 2× 3 + 1

3 = 2× 1 + 1

1 = 2× 0 + 1.

19 / 38

Page 27: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Digits: right-to-left

4716 = 10× 471 + 6

471 = 10× 47 + 1

47 = 10× 4 + 7

4 = 10× 0 + 4.

14 = 2× 7 + 0

7 = 2× 3 + 1

3 = 2× 1 + 1

1 = 2× 0 + 1.

19 / 38

Page 28: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Printing the digits from right-to-left

int num;int dividend, remainder;dividend=num;while (???){

remainder=dividend % 10;// Update dividend.

}

20 / 38

Page 29: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

char variable in memory

char ch=’A’;

Address Value68400 ’A’

68400 0 1 0 0 0 0 0 1

// ASCII value of ’A’ is 65 = (100001)2.

21 / 38

Page 30: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

int variable in memory

int num=21;

Address Value68400 21

0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 1 0 1 0 1

22 / 38

Page 31: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

int variable in memory

int num=-21;

Address Value68400 -21

1 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 1 0 1 0 1

23 / 38

Page 32: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Arrays

24 / 38

Page 33: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Arrays

int num[20];

Variable Address Valuenum[0] 68400num[1] 68404num[2] 68408num[3] 68412

num[18] 68472num[19] 68476

num[0]=287;num[3]=-50;x=2;

num[x]=841;

25 / 38

Page 34: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Arrays

int num[20];

Variable Address Valuenum[0] 68400num[1] 68404num[2] 68408num[3] 68412

num[18] 68472num[19] 68476

num[0]=287;num[3]=-50;x=2;

num[x]=841;

25 / 38

Page 35: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Arrays

int num[20];

Variable Address Valuenum[0] 68400num[1] 68404num[2] 68408num[3] 68412

num[18] 68472num[19] 68476

num[0]=287;num[3]=-50;

x=2;

num[x]=841;

25 / 38

Page 36: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Arrays

int num[20];

Variable Address Valuenum[0] 68400num[1] 68404num[2] 68408num[3] 68412

num[18] 68472num[19] 68476

num[0]=287;num[3]=-50;x=2;

num[x]=841;

25 / 38

Page 37: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Arrays

int num[20];

Variable Address Valuenum[0] 68400num[1] 68404num[2] 68408num[3] 68412

num[18] 68472num[19] 68476

num[0]=287;num[3]=-50;x=2;

num[x]=841;

25 / 38

Page 38: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Arrays

int num[100];

Variable Address Valuenum[0] 68400 287num[1] 68401num[2] 68402 841num[3] 68403 -50

num[98] 68498num[99] 68499

num[0]=287;num[3]=-50;x=2;

num[x]=841;

26 / 38

Page 39: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Integer arrays

int num[10], i=0;while(i<10){printf(”Enter number %d”,i);scanf(”%d”,&num[i]);i=i+1;}

27 / 38

Page 40: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Using arrays

int days[12]={0,31,59,80,110,141,171,202,233,263,294,324};

// Add days[m] to calculate day-of-week

int matrix[10][10];

char days[7][3]={”Sun”,”Mon”,. . .,”Sat”};

28 / 38

Page 41: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Using arrays

int days[12]={0,31,59,80,110,141,171,202,233,263,294,324};

// Add days[m] to calculate day-of-week

int matrix[10][10];

char days[7][3]={”Sun”,”Mon”,. . .,”Sat”};

28 / 38

Page 42: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

Examples

29 / 38

Page 43: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

P1: Finding the maximum of a sequenceof numbers

37

30 / 38

Page 44: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

P1: Finding the maximum of a sequenceof numbers

24

31 / 38

Page 45: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

P1: Finding the maximum of a sequenceof numbers

80

32 / 38

Page 46: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

P1: Finding the maximum of a sequenceof numbers

64

33 / 38

Page 47: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

P1: Finding the maximum of a sequenceof numbers

32

34 / 38

Page 48: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

P1: Finding the maximum of a sequenceof numbers

56

35 / 38

Page 49: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

P2: Count the vowels in a string

Input: ALPHANUMERIC

Output: There are 5 vowels.

36 / 38

Page 50: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

P3: Print logarithms

37 / 38

Page 51: Introduction to Programming Lecture Threearavind/Files-ID1303/ProgC-Lec3.pdf · Compiling and running a C program $ gcc helloWorld.c -o hello gcc: Gnu C Compiler Translates the C

P4: Trigonometric ratios

38 / 38


Recommended