Week 4 Programming Language
Week 4 Programming Language
COMPUTER
PROGRAMMING
AND
APPLICATION
PROGRAMMING LANGUAGE
Programming Language
Case Study
PROGRAMMING LANGUAGES
• Assembly language
• Operations must be specified in detail
• Use mnemonics instruction and symbolic names
• More efficient in memory space and run faster
• Editor
• To enter program at terminal
• Use editor or word processor
• Create source file (.cpp file)
• Compiler/ Assembler
• Translate program into machine language
• Detects syntax error
• Create object file
WRITING, EDITING, COMPILING
AND LINKING PROGRAMS
• Linker
• Link other object file or library routines
• Create load file
• Loader
• Put load file in main memory
• For execution
• Create executable program .exe
• Debugger
• Detect, diagnose and correct errors in programs
• Run one instruction at a time
• Bugs = errors
The process:
• Linker links the object code with the libraries, creates a .out file
and stores it on disk
• CPU takes each instruction and executes it, possibly storing new
data values as the program executes
A SIMPLE PROGRAM
header files
int main ()
statements
return 0;
}
A SIMPLE PROGRAM
#include <iostream>
int main ()
return 0;
}
A SIMPLE PROGRAM
#include <iostream>
int main ()
return 0;
Problems
• Converting miles to kilometers
Analysis
• Data requirements
• Input : miles
• Output : kilometers
• Formula : 1 mile = 1.609 kilometers
Algorithms
• Computing problems
• All can be solved by executing a series of actions in a specific order
• Algorithm is a procedure in terms of:
• Actions to be executed
• The order in which these actions are to be executed
SOFTWARE DEVELOPMENT
ENVIRONMENT
Implementation
• Writing a program
• Convert each algorithm step into a statement in a programming
language
Testing
• Run the program several times using different set of data to make
sure it works correctly
PSEUDOCODE
Pseudocode is
• Artificial, informal language that helps us develop algorithms
• Similar to everyday English
• Not actually executed on computers
• Helps us “think out” a program before actually writing it
Example of pseudocode
Initialize n to fifty
Initialize sum to zero
Initialize f1 and f2 to zero
repeat n times
add f1 and f2, store this value in sum
assign f1's current value to f2
assign sum`s current value to f1
end loop
FLOWCHART
What is a flowchart
• Graphical representation of an algorithm
• Drawn using certain special-purpose symbols connected by arrows
called flow lines
• A graphical way to describe the solution of a problem
Data flow
Start
Input
a,b
S=a+b
Output
S
End
Exercise: Read any number from the user, then print positive if it is
positive
Start
Input
num
Num True
>0
Output
“+ve”
False
End
CASE STUDY
Problem
• Take the radius of a circle and compute and print its area and
circumference
Analysis
• Data requirements
• Problem constant
• Pi 3.14159
• Problem input
• radius
• Problem output
• area, circumference
• Relevant formulas
• Area of a circle = pi x r2
• Circumference of a circle = 2 x pi x r
CASE STUDY
Design
• Initial algorithm
• get circle radius
• calculate area
• calculate circumference
• display area and circumference
Pseudocode
• Get circle radius from user
• Calculate area
• Assign product of Pi and radius to area
• Find circumference
• Assign product of two times Pi and radius to circumference
• Display the area and circumference
CASE STUDY
Flowchart
Start
Get
radius
Calculate
area
Calculate
circum.
Print
result
End
// Calculate and display the area and circumference of a circle
#include <iostream>
#define Pi 3.14159
using namespace std;
int main ()
{
double radius, area, circum;
cout << “Enter radius>”; //Get the circle radius
cin >> radius
return 0;
}
CASE STUDY
Implementation
Output
Enter radius> 5
The area is 78.539750
The circumference is 31.415900
Testing
• Calculate by using other means to verify the result