introduction to compilation process march 24. the following slides will show you step by step...

18
Introduction to compilation process March 24

Upload: delphia-fleming

Post on 21-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

Introduction to compilation process

March 24

Page 2: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

2

The following slides will show you step by step instruction how to get ready and build C language programs in Linux Ubuntu.

1. Enter terminal by pressing <ctrl> <alt> t

2. To create a folder workspace/programming in you home folder type

mkdir -p ~/workspace/programming/3. Enter newly created folder cd ~/workspace/programming/

4. Enter text editor by typingnano first.c

Page 3: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

3

nano editor

See the control option at the bottom of the screen.For example to delete line of text press <ctrl> k

Page 4: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

4

Type your programImportant!!!

Add comments

Add indents

Page 5: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

5

Building the program To build(create a binary file) your program typegcc <source_file.c> -o <binary_file>

buildexecute

Page 6: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

6

Errors

Remove ;

Remove &

Lets make an error and a warning on purpose

Page 7: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

7

Building a program with errors

Notifies us about the syntactical error

Notifies us about the warning

Even thought program compiles and ready to run, it will not function correctly, thus pay attention to warnings carefully.

Page 8: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

8

Largest of three numbers

start

read X, Y, Z

stop

X > Y ?

stop

yes no

max > Z ?

Output max Output z

yes no

Max = X Max = Y

Transform the flowchart below to a C language program.

See next slide

Page 9: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

9

Largest of three numbers

Page 10: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

10

Find whether a number is odd or even Draw a flow chart for the algorithm that checks whether the

number is odd or even. Convert the algorithm into C language program

start

read x

stop

x % 2 == 0 ?

stop

yes no

Output “even”

Output “odd”

Page 11: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

11

Find whether a number is odd or even Type the program in nano editor and compile it with

gcc evenodd.c -o evenodd

Page 12: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

12

Example of a while loop The program prints out a conversion table from Fahrenheit to

Celsius. Draw a corresponding flowchartProgram Output

Page 13: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

13

start

read N

stop

Count > N ?

output sum

no yes

sum = sum + count * count

sum = 0count = 1

count = count + 1

Example of a for loopThe program calculates the following sum: sum = 12 + 22 + 32 + … + N2

Page 14: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

14

The following slides are your homework assignments

Page 15: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

15

start

read N

stop

Count > N ?

output sum

no yes

sum = sum + count * (count + 1)

sum = 0count = 1

count = count + 1

Problem 1

Convert to C program

Use do{}while() loop construction

sum = 1*2 + 2*3 + 3*4 + … + N*(N +1)

The following algorithm calculates the sum

Page 16: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

16

start

read N

stop

count > N ?

output sum

no yes

prod = prod * count

prod = 1count = 1

count = count + 1

Problem 2

Convert to C program

Use for() loop construction

The following algorithm calculates factorial

Page 17: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

17

start

Read X, N

stop

count > N ?

output sum

noyes

sum = prod * countterm = term * X / count

term = 1prod = 1count = 1

count = count + 1

Problem 3

Use Taylor expansion to represent ex up to N terms.

Convert to C program

Hint: Use for() loop construction

The following algorithm computes ex series up to N terms

Page 18: Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs

18

start

Read X, N

stop

term < .0001 ?

output sum

noyes

sum = prod * countterm = term * X / count

term = 1prod = 1count = 1

count = count + 1

Problem 4

Use Taylor expansion to represent ex up to 4 decimal places.

Convert to C program

Hint: Use do{}while() loop construction

The following algorithm computes ex series up to 4 decimal places