Uploaded by Muhammad Daud Ali

Screenshot 2021-12-06 at 4.27.18 AM

advertisement
Computer System and Programming
Department of Computer Science,
Capital University of Science and Technology, Islamabad
Outline
• What are computers ?
• Simple program logic
• The steps in the program development cycle
2
What is a Computer?
A computer is a electro-mechanical device that
works semi-automatically to process input data
according to the stored set of instructions and
produces output or resultant data.
Instruction
s
Dat
a
A Computer
System
Result
s
3
Components of a Computer System
Peripheral Devices / Connected
devices
Keyboard
Mouse
Display
CD Rom
Hard Disk
Compute
r
Main Memory
CPU
Arithmetic and Logic Unit
Control Unit
4
Computer Instructions and Programs
Instruction: A computer instruction is a command
or directive given to a computer to perform
specific task.
Examples: Add 2 and 5, Print “Hello World”
Program: A program is sequence of instructions
written in programming language that directs a
computer to solve a problem
Examples: Draw a square, etc.
5
Computer Instructions and Programs
Program “Draw a square”
1 – Draw a vertical line of length n inches
2 – Draw a horizontal line of n inches
3– Draw a vertical line of length n inches
4 – Draw a horizontal line of n inches
6
Computer Software System
Application Programs
(.cpp, .c, .java,)
Compilers / Libraries
(C++, C, Java)
Operating Systems
(Windows, Linux, MAC, Solaris)
Computer Hardware
7
Programming Languages
Classification of programming languages:
1.
Machine language
2.
Low-level languages
3.
High-level languages
8
1. Machine level languages
A computer understands only sequence of bits or 1’s
and 0’s (the smallest piece of information)
A computer program can be written using machine
languages (01001101010010010….)
Very fast execution
Very difficult to write and debug
Machine specific (different codes on different
machines)
9
2. Low level languages
English encrypted words instead of codes (1’s and
0’s)
More understandable (for humans)
Example: Assembly language
Requires: “Translation” from Assembly code to
Machine code
Assembly
Code
compare:
cmpl #oxa,n
cgt
end_of_loop
acddl #0x1,n
end_of_loop:
Machine Code
Assembler
1001010101001101
1110010110010100
0101010111010010
0110100110111011
1101100101010101
10
3. High level languages
Mostly machine independent
Close to natural language (English like language
keywords)
Easy to write and understand programs
Easy to debug and maintain code
Requires compilers to translate to machine code
Slower than low-level languages
11
3. High level languages
Some Popular High-Level languages
COBOL (COmmon Business Oriented Language)
FORTRAN (FORmula TRANslation)
BASIC (Beginner All-purpose Symbolic Instructional
Code)
Pascal (named for Blaise Pascal)
Ada (named for Ada Lovelace)
C (whose developer designed B first)
Visual Basic (Basic-like visual language by Microsoft)
C++ (an object-oriented language, based on C)
Java
12
Programming Paradigms
Programming paradigm is the fundamental
style of computer programming
Imperative
Functional
Logical
Object Oriented
13
Imperative Paradigm
Machine based modes (Sequence of steps
required to compute)
Statements executed sequentially step-wise
Emphasis on How is to compute
How to obtain the required results or output
Example languages: C, Pascal, Ada, etc.
14
Imperative Paradigm Languages
15
Functional/Logical Paradigm
Specify what is to be computed using
programming abstractions.
Responsibility of a programmer is to specify
What is to compute
How to compute is implementation dependent
and is managed by language compiler
Example languages: Lisp, Scheme, Erlang,
Haskell, Scala etc.
16
Object-Oriented Paradigm
• Programming with Abstract Data Types, to model
real world
• Basic Program Unit: Class (a programmer defined
type)
– A entity that contains data and methods which work on
the data
• Basic Run-time Unit: Object
– Instance of a class during execution
17
Problem Solving Steps
Understand the problem
Plan the logic
Code the program
Test the program
Deploy the program into production
18
1. Understanding the Problem
Problems are often described in natural
language like English.
Users may not be able to specify needs
well, and the needs may changing
frequently
Identify the requirements
Inputs or given data-items
Required output(s) or desired results
19
1. Understanding the Problem
Example: Calculate the area of a circle
having the radius of 3 cm
Inputs:
Radius=3
Output:
Area
Indirect Inputs:
Pi=3.14
Area = 3.14 * (3*3) = 28.27
20
2. Plan the Logic
Identify/Outline small steps in sequence, to
achieve the goal (or desired results)
Tools such as flowcharts and pseudocode can
be used:
Flowchart: a pictorial representation of the logic
steps
Pseudocode: English-like representation of the
logic
Walk through the logic before coding
21
3. Code the Program
• Code the program:
– Select the programming language
– Write the program instructions in the selected
programming language
– Use the compiler software to translate the
program into machine understandable code or
executable file
– Syntax errors (Error in program instructions) are
identified by the compiler during compilation
and can be corrected.
22
4. Test the Program
• Testing the program
– Execute it with sample data and check the
results
– Identify logic errors if any (undesired results
or output) and correct them
– Choose test data carefully to exercise all
branches of the logic (Important)
23
5. Deploy the Program
• Putting the program into production
– Do this after testing is complete and all known
errors have been corrected
24
Introduction to Pseudocode
• One of the popular representation based on
natural language
• Widely used
– Easy to read and write
– Allow the programmer to concentrate on the
logic of the problem
• Structured in English language (Syntax/grammar)
25
What is Pseudocode (continued...)
• English like statements
• Each instruction is written on a separate line
• Keywords and indentation are used to signify
particular control structures.
• Written from top to bottom, with only one entry
and one exit
• Groups of statements may be formed into
modules
26
Some Basic Constructs
• Print
Output is to be sent to the Printer
• Write
Output is to be written to a file
• Display
Output is to be written to the screen
• Prompt required before an input instruction Get,
causes the message to be sent to the screen
• Compute /Calculate
computer
Calculation performed by
• Calculation operations: +, -, *, /, ()
27
Some Basic Constructs
• Set
•=
Used to set inital value,
E.g., Set Marks to 0
Place values from right hand-side item to left
hand side
E.g., Marks = 67
• Save
Save the variable in file or disk
E.g., Save Marks
28
Comparison/Selection
• Making decision by comparing values
•
• Keywords used:
•IF, THEN, ELSE
IFstudent_Marks
student_Marksare
areabove
above50
50THEN
THEN
IF
Result==“Pass“
“Pass“
Result
ELSE
ELSE
Result==“Fail“
“Fail“
Result
ENDIF
ENDIF
29
Comparison/Selection
• Case Strtucture
➢ Multiple banching based on value of condition
• Three keywords used:
• CASE of, Case, ENDCASE
CASE of <condition-variable>
Case <valueA>
Do Step1
Case <valueB>
Do Step2
Case <valueC>
30
Comparison/Selection
• Case Strtucture
➢ Example...
The Case structure can be used in place of a series of If ..ElseIf.. statements.
So instead of
Input a Grade
If the Grade >= 100 Then
Report “Perfect Score”
ElseIf the Grade > 89 Then
Report “Grade = A”
ElseIf the Grade > 79 Then
Report “Grade = B”
ElseIf the Grade > 69 Then
Report “Grade = C”
ElseIf the Grade > 59 Then
Report “Grade = D”
Else
Report “Grade = F”
End If
31
Comparison/Selection
You
Input a Grade
Case based on Grade
Case >=100
Report “Perfect Score”
Case > 89
Report “Grade = A”
Case > 79
Report “Grade = B”
Case > 69
Report “Grade = C”
Case > 59
Report “Grade = D”
Default
Report “Grade = F”
End Case
Note: You can replace an If...Then...ElseIF…. structure with a Select Case structure only
if the If statement and each ElseIf statement evaluates the same expression.
32
Repeat a group of Statements
• Repeating set of instruction base on some condition
• Keyword used:
•DOWHILE, ENDDO
DOWHILE student_total < 50
Read student record
Print student name
Add 1 to student_total
ENDDO
33
Example Pseudocode Program
A program is required to read three numbers,
add them together and print their total.
Input
Number1
Number2
Number3
Processing
Output
total
34
Solution Algorithm
Add_three_numbers
Set total to 0
Read number1
Read number2
Read number3
Total = number1 + number2 + number3
Print total
END
35
Repeat Until (Loop)
• Repeat-Until statement (loop)
REPEAT
Do StepA
Do StepB
UNTIL conditionN is True
- What is the Difference between (While &
Repeat) ?
36
Repeat Until (Loop)
- What is the Difference between (While &
Repeat) ?
• Repeat Until :
– Stops looping if the condition evaluates to True.
– Will only test the condition after iterating thru all
commands between Repeat...Until. This means that
even if the condition would evaluate to True right at
the repeat command, all commands will be executed
regardless of this, until the until command is reached,
where it will exit the loop.
– repeat…until does one loop and then evaluates the37
condition. If it doesn’t hold, repeat. If it holds, the
Repeat Until (Loop)
- What is the Difference between (While &
Repeat) ?
• While Do :
– Stops looping if the condition evaluates to False.
– Will test the condition right before entering the loop,
and wont execute any command of the loop if the
condition evaluates to False.
- while loop first evaluates the condition and if it
holds, one loop is done, otherwise the looping
is done. Then repeat.
38
Flowcharts
• “A graphic representation of a sequence of
operations to represent a computer program”
• Flowcharts show the sequence of instructions in a
single program or subroutine.
39
A Flowchart
• A Flowchart
– Shows logic of an algorithm or problem
– Shows individual steps and their interconnections
– E.g., control flow from one action to the next
40
Flowchart Symbols
Name
Oval
Parallelogram
Rectangle
Diamond
Arrow
Symbol
Description
Beginning or End of the Program
Input / Output Operations
Processing for example,
Addition, Multiplication,
Division, etc.
Denotes a Decision (or
branching) for example IF-ThenElse
Denotes the Direction of logic
flow
41
STAR
T
Example 1
Input
M1,M2,M3,M
4
GRADE (M1+M2+M3+M4)/
4
N
IS
GRADE<
50
Print
“PASS”
Y
Step 1: Input M1,M2,M3,M4
Step 2: GRADE =
(M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Print
“FAIL”
STOP
42
Example 2
• Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
43
Example 2
Flowchart
Algorithm
• Step 1: Read Lft
• Step 2: Lcm = Lft x 30
• Step 3: Print Lcm
START
Read Lft
Lcm = Lft x 30
Print LCM
STOP
44
Example 3
• Write an algorithm and draw a flowchart that will
read the two sides of a rectangle and calculate its
area.
45
Example 3
Algorithm
• Step 1: Read W,L
• Step 2: A = L x W
• Step 3: Print A
STAR
T
Read
W, L
A
LxW
Print
A
STOP
46
DECISION STRUCTURES
• The expression A>B is a logical expression
• It describes a condition we want to test
• if A>B is true (if A is greater than B) we take the
action on left
• Print the value of A
• if A>B is false (if A is not greater than B) we take
the action on right
47
• Print the value of B
IF–THEN–ELSE STRUCTURE
• The algorithm for the flowchart is as follows:
If A>B then
print A
Else
Y
N
is
print B
A>
B
endif
Print
A
Print
B
48
CASE STRUCTURE
• Multiple branching based on a single condition
CASE
Condition
Do Step A
Do Step B
Do Step C
Do Step D
49
Relational / Logical Operators
Relational Operators
Operator
Description
>
Greater than
<
Less than
=
Equal to
Greater than or equal to
Less than or equal to
Not equal to
50
Example 4
• Write an algorithm that reads two values, finds largest
value and then prints the largest value.
ALGORITHM
Step 1: Read VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3: Print “The largest value is”, MAX
-- DRAW the Flow Chart for the Program
51
Selection Structure
• A Selection structure can be based on
1.
Dual-Alternative (two code paths)
2.
Single Alternative (one code path)
Dual Alternative Example
Y
Print
A
is
A>
B
N
Print
B
52
Selection Structure
Single Alternative Example
Pseudocode:
IF GPA is greater than 2.0 Then
Print “Promoted ”
End IF
N
GPA
> 2.0
Y
Print
“Promoted”
53
Loop Structure
• Repetition (WHILE structure)
– Repeats a set of actions based on the answer to a
question/condition
pseudocode: DoWHILE <Some-True-Condition>
Do Something
ENDDO
54
Loop Structure
• REPEAT-UNTIL structure
– Repeats a set of actions until a condition remains True
– pseudocode: REPEAT
Do-Something
UNTIL <Some True Condition>
55
Download