chapter1_2_3 - Kristen Bhing Salvio
Download
Report
Transcript chapter1_2_3 - Kristen Bhing Salvio
Chapter
1
Overview of a Computer
System
Fundamentals of Programming
Memory
Input
CPU
Arithmetic
/Logic Unit
Output
Programming
is
providing a computer a set of
instructions to perform.
Program
is
a set of instructions for a
computer to perform.
Programmer
is
a person who writes the set of
instructions.
A sample program (written in the
English language)
1)
2)
3)
4)
Accept three numbers from the
keyboard
Store these numbers in the memory
Name the memories A, B and C
correspondingly
Add the three numbers
A sample program (written in the
English language)
5) Store the result of the addition
memory
6) Name the memory where the
is stored as D
7) Print on the computer screen
result stored in D
in
sum
the
Programming Language
is
the language used in writing a
set of instructions for the
computer to follow.
Timelines of Popular Computer
Programming Languages
Assembler
Name: Assembly Language
Created: 1956 – 1963
By: IBM
Language type: low level
Timelines of Popular Computer
Programming Languages
Lisp
Name: Lisp
Created: 1956
By: John McCarthy
Acronym: List Processing
Language type: Functional
Timelines of Popular Computer
Programming Languages
Fortran
Name: Fortran
Created: 1957
By: IBM, John Backus
Acronym: Formula Translation
Language type: block-structured
Timelines of Popular Computer Programming
Languages
Cobol
Name: Cobol
Created: 1959
By: Conference on Data Systems Languages
Acronym: Common Business Oriented
Language
Language type: block-structured
Timelines of Popular Computer
Programming Languages
Basic
Name: Basic
Created: 1964
By: John George Kemeny & Tom Kurtz
Acronym: Beginners All-purpose Symbolic Instruction
Code
Language type: block-structured
Timelines of Popular Computer
Programming Languages
Pascal
Name: Pascal
Created: 1970
By: Professor Niklaus Wirth
Acronym:
Language type: block-structured
Timelines of Popular Computer
Programming Languages
C-language
Name: C
Created: 1972
By: Bell Laboratories
Acronym:
Language type: block-structured
Timelines of Popular Computer
Programming Languages
C++
Name: C++
Created: 1983 - 1985
By: Bjarne Stroustrup at Bell Labs
Acronym:
Language type: Object-oriented
Timelines of Popular Computer
Programming Languages
QBasic
Name: QBasic
Created: 1986
By: Microsoft
Acronym: QuickBasic
Language type: block-structured
Timelines of Popular Computer
Programming Languages
Perl
Name: Perl
Created: 1987
By: Larry Wall
Acronym:
Language type: Command or Scripting
Timelines of Popular Computer
Programming Languages
Visual
Basic
Name:Visual Basic
Created: 1991
By: Microsoft
Acronym:
Language type: block-structured
Timelines of Popular Computer
Programming Languages
Java
Name: Java
Created: 1995
By: James Gosling
Acronym:
Language type: Object-oriented
Timelines of Popular Computer
Programming Languages
JavaScript
Name: JavaScript
Created: 1995
By: Netscape Communications Corp. & Sun
Microsystems
Acronym:
Language type: Command or Scripting
Timelines of Popular Computer
Programming Languages
VBScript
Name:VBScript
Created: 1997
By: Microsoft
Acronym:Visual basic Scripting Edition
Language type: Application/Macro
Timelines of Popular Computer
Programming Languages
VBScript
Name:VBScript
Created: 1997
By: Microsoft
Acronym:Visual basic Scripting Edition
Language type: Application/Macro
Steps in Programming
5. Documenting the
Program
4. Debugging & Testing the
Program
3. Coding the Program
2. Analysis of the Problem
1. Definition of the
Problem
Chapter
2
Defining the Problem
Stating the Problem
Problem 1:
Write a program to determine the
sum of two numbers.
Problem 2:
Write a program that will
compute the area of a circle.
Definitive Statement of the
Problem
Problem 1:
Give the computer two numbers.
It should be able to add the two
numbers and give the result.
Definitive Statement of the
Problem
How will you re-state Problem
No 2?
Analyzing the Problem
NATURE. (What is it about)
SCOPE. (Limitations)
Example 1
Problem 1:
Write a program to determine the sum of
two numbers.
Nature: Mathematical
Scope: Limit the scope to numbers that
are integers or whole numbers.
Example 2
Problem 2:
Write a program that will compute the area
of a circle.
Nature: Geometry.
Scope: Allow fractions and decimals as
acceptable values. No negative values for
the radius should be accepted.
Identifying the Output
Problem 1:
Write a program to determine the
sum of two numbers.
Output:
[sum]
The sum of the two numbers you
gave is: [sum]
Identifying the Input
Problem 1:
Write a program to determine the
sum of two numbers.
Input:
We need two numbers as the
input. (keyed in the keyboard)
Identifying the Input
Problem 2:
Write a program that will
compute the area of a circle.
Input:
Radius (to be entered from the
keyboard)
Defining the Process
Problem 1:
Write a program to determine the sum of two
numbers.
Process:
Enter two numbers using the keyboard. The
computer must be able to reject fractions and
decimal numbers. The computer will then add the
two numbers. The result of the addition will be
displayed on the computer screen.
Defining the Process
Problem 2:
Write a program that will compute the area of a circle.
Process:
The program must be able to accept a number
representing the radius. The computer should make
it sure that no negative number will be entered.
The computer must compute the area of the circle.
Result must be displayed on the screen.
Exercise #1
Analyze the given problem for using the
five-step procedure you learned from this
chapter.
Problem: Write a program to determine
the average of three numbers.
Exercise #2
Analyze the given problem for using the fivestep procedure you learned from this
chapter.
Problem: Write a program to determine
whether a number entered on the keyboard
is odd or even.
Exercise #3
Analyze the given problem for using the
five-step procedure you learned from this
chapter.
Problem: Generate the first ten positive
numbers. For each number, print its square
and cube.
Chapter
3
Planning the Solution
ICT Programming 1
Algorithm
A set of rules for solving a
problem in a finite number of
steps.
Overview of an Algorithm
STEPS IN COOKING SCRAMBLED EGG
1) Prepare all necessary items needed (egg, pan,
oil etc.)
2) Beat the egg
3) Add salt
4) Heat pan
5) Put oil into the pan
Overview of an Algorithm
STEPS IN COOKING SCRAMBLED EGG
6) Pour beaten egg if pan is already hot
enough
7) Cook egg for at least two minutes
8) Serve Egg
Control Structures
Structured programming
is a technique that emphasizes thebreaking of a
program or algorithm into logical sections, or
modules following a universal programming standard
(Breaking
of programs into smaller modules)
Control Structures
is a step, or a series of steps, that determines what
instructions or set of instructions will be done nest.
Three Basic Structures
Sequential
Selection
Iteration
Sequential
is the most straight forward. The program
will just follow the instructions one after the
other in a sequential manner.
Process 1
Process 2
Example: Taking a Bath
TurnontheShower
Getyourselfwet
GettheBathSoap
RinseyourhairandBody
Massageitonhair
Rubitonyourbody
Dryyourselfwithtowel
PutonNewClothes
Selection
Let your program or algorithm make a
decision. Choose one from two sets of
instructions depending on the
condition.
IF (condition) THEN (Process 1)
ELSE (Process 2)
Process 1
IF
Condition
Process 2
IF (it is dark) THEN (turn on the
lights) ELSE (do not turn on the
lights)
Turn on the
Lights
True
Is it dark?
False
Do not turn on
the lights
Iteration
We use when we want our algorithm or
program to repeat a process a specified
number of times.
The structure that repeats an instruction or a
set of instructions until a condition is met is
known as iteration or loop.
Iteration
Is the
condition
met?
Yes
No
Process 1
Whether the scrambled egg is ready to
be served or not
Beat the egg
Add salt
Place the pan in the stove
Put on the stove
Pour cooking oil
Heat the oil
Put egg
Cook: { here is the example of the control structure}
Heat the egg
IF the egg is done
THEN serve the egg
ELSE goo to Cook {End pf control structure}
Iteration control for serving
scrambled egg
Cook
Heat the egg
Is egg
done?
Yes
Serve the egg
No
Flowcharting
is one method of pictorially representing a
procedural (step-by-step) solution to a
problem before you actually start to write the
computer instructions required to produce
the desired results.
Two Types of Flowchart
System (data) flowcharts
Programming flowcharts
System Flowchart
Defines the major phases of the
processing, as well as the various data
media used.
It shows the relationship of
numerous jobs that make up an
entire system.
Example of system flowchart
Pay Records
Process Payroll
Pay Check
Programming Flowchart
Constructed by the programmer to
represent the sequence of operations
the computer is to perform to solve a
specific problem.
It graphically describes what is to take
place in the program.
Standard Symbols
Start or end of a program
Off-page connector
Input or Output Operation
Magnetic tape
Computational steps or processing
function of a program
Alternate Process
Decision-Making and
Branching
Connector
Preparation or
Initialization
Magnetic Disk
Display
Flow line
Examples on Flowcharting
Problem: Make a flowchart to determine the
sum of two numbers.
Definitive Statement: Give the computer
two numbers. It should be able to add the two
numbers and give the result.
Examples on Flowcharting
Output: [sum] or
The sum of the two numbers is:
[sum]
Input: we need two numbers as the
input. The program allows both
positive and negative integers.
Start the process
Enter the 1st no called N1
Enter the 1st no
called N2
N1 and N2 is
added and the
result is called
Sum
Sum is given as the
output
End of the process
Start
Input N1
Input N2
Sum = N1 + N2
Output Sum
End
Example 1
The two blocks can
be combined as
Input
N1, N2
Example 2
Start
Draw a flowchart
to find the sum of
first 50 natural
numbers.
Sum = 0
N=0
N=N+1
Sum = Sum + N
No
IsN=50?
Yes
End
Print Sum
Start
Read A, B, C
Yes
Is B>C?
Print C
No
No
Is A>B?
Yes
Is A>C?
Yes
Print B
Print A
End
Example 3
Draw a flowchart
to find the
largest of three
numbers A, B and
C.
Pseudocode
Uses words and mathematical
expressions to represent the logic, or
the flow of the program.
Informal language for writing
algorithms.
NOTATION
USE
OTHER NOTATIONS
USED
INPUT
Used when reading or
entering data in any of the
input devices.
READ, ENTER
OUTPUT
Used when we wish a value
to be displayed.
WRITE, DISPLAY, PRINT
BEGIN
Marks the start of a
pseudocode.
START
END
Marks the end of a
pseudocode.
STOP, HALT
Storage Operator. Used to
indicate that a value is
stored in a memory location.
Example of Pseudocode
Problem: Write a pseudocode to determine
the sum of two numbers.
Definitive Statement: Give the computer
two numbers. It should be able to add the two
numbers and give the result.
Example of Pseudocode
Output: The output of the computer
should give us a number which is integer
or a whole number.
[sum] or
The sum of the two numbers you gave
is: [sum]
Example of Pseudocode
Pseudocode:
BEGIN
INPUT the first number
INPUT the seocnd number
Add the two numbers
OUTPUT the sum
END
Equivalent Code in C++
{
cin>>N1;
cin>>N2;
Sum=N1 + N2;
cout<<Sum;
}
VALID IDENTIFIERS
INVALID IDENTIFIERS
sum
123
N1
4abc
rs232c
2nd
salary
#ars
X
Y
Problem 2:
Make a pseudocode to enter two
numbers. Display their sum if the first
number is less than the second
number entered, otherwise, display
their average.
Solution:
Pseudocode:
INPUT A
INPUT B
Sum A + B
Ave Sum / 2
IF (A>B) THEN (Output Sum) ELSE
(Output Ave)
END
Problem 3:
Make a pseudocode to determine
whether a number is even or odd.
Solution:
Pseudocode:
BEGIN
INPUT N
Divide N by 2
IF(there is remainder) THEN (output “Number
is Odd”)
ELSE (output “Number is Even”)
END
Flowcharting & Pseudocoding
Exercises
1)
2)
3)
Read three numbers, add them, print the
result and end the procedure.
Read two numbers, multiply them, print
the result and end the procedure.
Read two numbers, divide the second by
the first and print the quotient.
Pseudocode for Exercise #1
Read three numbers, add them,
print the result and end the
procedure.
BEGIN
INPUT A, B, C
SumA + B + C
OUTPUT Sum
END
1)
Flowchart for Exercise #1
1)
Read three numbers, add them,
print the result and end the
procedure.
START
Read A, B, C
Sum = A + B + C
Write Sum
STOP
Pseudocode for Exercise #2
2) Read two numbers, multiply them,
print the result and end the
procedure.
BEGIN
INPUT A, B
ProductA * B
OUTPUT Product
END
Flowchart for Exercise #2
2) Read two numbers, multiply them,
print the result and end the
procedure.
START
Read A, B
Product = A * B
Write Product
STOP
Pseudocode for Exercise #3
3) Read two numbers, divide the second by the first
and print the quotient.
BEGIN
INPUT A, B
QuotientB / A
OUTPUT Quotient
END
2) Read two numbers, multiply
them, print the result and end
the procedure.
Flowchart for Exercise #3
3) Read two numbers, divide the
second by the first and print the
quotient.
START
Read A, B
Quotient = B / A
Write
Quotient
STOP
Seatwork for Today (20 pts)
Given two variables A and B where A=7 and
B=5
Make a Pseudocode or a Flowchart (choose just
one!)that swaps the content of A and B and
display on screen the new values of A and B.
Expected Output: A is 5
B is 7