introduction to compilation process march 24. the following slides will show you step by step...
Post on 21-Jan-2016
217 Views
Preview:
TRANSCRIPT
Introduction to compilation process
March 24
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
3
nano editor
See the control option at the bottom of the screen.For example to delete line of text press <ctrl> k
4
Type your programImportant!!!
Add comments
Add indents
5
Building the program To build(create a binary file) your program typegcc <source_file.c> -o <binary_file>
buildexecute
6
Errors
Remove ;
Remove &
Lets make an error and a warning on purpose
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.
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
9
Largest of three numbers
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”
11
Find whether a number is odd or even Type the program in nano editor and compile it with
gcc evenodd.c -o evenodd
12
Example of a while loop The program prints out a conversion table from Fahrenheit to
Celsius. Draw a corresponding flowchartProgram Output
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
14
The following slides are your homework assignments
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
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
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
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
top related