Programmin Fundamentals Weak 1 and 2
Programmin Fundamentals Weak 1 and 2
Text book:
Object-Oriented Programming in C++ by Robert
Lafore
Reference book:
C++, How to Program by Deitel & Deitel
Introduction
3
3
Hardware
4
Input devices
Obtains data from outside of the computer.
Usually a keyboard, mouse, disk or scanner
Output devices
Makes info available outside the computer.
Screens, paper printouts, speakers
Major Hardware Components
6
Processor
Central Processing Unit or CPU
The brain of a computer system.
Secondary storage
Stores programs or data not currently being used by
other units on secondary storage devices (like hard disk
and floppy disks)
Long-term, high-capacity “warehouse”
Memory
Input Output
Devices Devices
CPU
13
Central Processing Unit
Brains of the computer Executes program
instructions!
Arithmetic/Logic Unit performs arithmetic
operations, and makes logical comparisons
Control Unit decodes and executes your instructions
14
Language of a Computer
The only language a computer directly understands is
sequences of 0s and 1s
The language made up of 0s and 1s is called machine
language
The digits 0 or 1 are called binary digits (bits) with eight
bits equalling a byte
A sequence of 0s and 1s is also referred to as binary
code
15
16
Computer Programming
17
Computer Programming
18
Computer Programming
19
Some Well-Known Programming
20
Languages
C++
BASIC Ruby
FORTRAN
Java
Visual Basic
COBOL C#
JavaScript
C Python
Types of Programming Languages
21
Types of languages:
▫ Machine languages
machine dependent
Close to machine
▫ Assembly languages
machine dependent
Close to machine
▫ High-level languages
most are portable
Close to human
Specific languages include C, C++, Visual Basic and
Java
The Evolution of Programming
Languages
Early computers were programmed in machine
language
To calculate wages = rates * hours in
machine language:
100100 010001 //Load
100110 010010 //Multiply
100010 010011 //Store
23
23
Assembly Language
Assembly language instructions are mnemonic
Assembler: translates a program written in assembly
language into machine language
24
24
Assembly Language (continued)
LOAD rate
MULT hour
STOR wages
25
25
High-Level Languages
26
26
Compilers and Interpreters
Source code
the original program in a high level language
Object code
the translated version in machine language
28
Compiler and Interpreter
COMPILER INTERPRETER
Input It takes an entire program at a time. It takes a single line of code
or instruction at a time.
Errors Display all errors after compilation, all at Displays error of each line one
the same time. by one.
Pertaining C, C++, C#, Scala, typescript uses Java, PHP, Perl, Python, Ruby
Programming compiler. uses an interpreter.
languages
29
Data and Information
30
Data Information
Meaning Data is raw, unorganized facts that When data is processed, organized,
need to be processed. Data can be structured or presented in a given context so
something simple and seemingly random as to make it useful, it is called information.
and useless until it is organized.
Example Each student's test score is one piece of The average score of a class or of the entire
data. school is information that can be derived
from the given data.
Algorithm
Algorithm is a process that a computer could carry out to
complete a well defined task within finite time and
resources.
31
Al-Khwarizimi Principle
32
Divide and Conquer
33
Divide and Conquer
Hard Problem
34
Algorithms
Algorithm: A step-by-step sequence of instructions
that must terminate
Pseudocode
Use of English-like phrases to describe an algorithm
Flowchart
Useof diagrams that employ symbols to describe an
algorithm
35
Problem Solving Techniques
36
Algorithm
Make a Jam and Butter Sandwich
Output
Jam and Butter Sandwiches
Inputs
Jam, Butter, Bread, Knife, Plate
Process(Pseudocode)
1. Put two slices of bread on the plate
2. Using the knife, spread butter on one side
3. Using the knife, spread jam on the other side
4. Put the two slices together, clean side out
5. Repeat from step 1 to 4 to prepare more sandwiches
37
Conversion from Fahrenheit to Celsius
Output
Temperature in Celsius (C)
Inputs
Temperature in Fahrenheit (F)
Process
5
C (F 32)
9
38
Algorithm
ATM for withdrawal
Inputs
User Identification (ATM card), password, amount
Output
Money, error messages
39
ATM for withdrawal - Process
1. Get the ATM card for identification and ask for
password
2. Check password
3. If password is not valid, generate an error message and
go to step number 8.
4. Get the amount from the user
5. Check the current balance
6. If amount is greater than current balance, generate an
error message and go to step number 8.
7. Subtract the amount from the balance and give out the
cash.
8. Return the ATM card
9. Stop
40
Flowchart
41
Input a,b
S=a+b
Output s
Problem : Compute the area of the circle. Where
area = 3.14 x R2
43
Input r
A = 3.14 * R *R
Output A
Problem : Read any number from the user, then print
positive if it is positive.
44
Input Num
True
Num>0
Output
False “Positive”
Problem : Read any number from the user, then print
positive if it is positive and print negative otherwise.
45
Input Num
False True
Num>0
Output Output
“Negative” “Positive”
Problem : Read Two numbers from the user, then print
the greatest one.
46
Input x, y
False True
X>y
Output Output
y x
What Can a Program Do?
A program can only instruct a computer to:
Read Input
Calculate
Store data
Write Output
Work in a sequential progression (Sequence)
Compare and branch (Selection)
Iterate or Loop (Repetition)
47
Programming Methodologies
Object-oriented
48
48
Structured Programming
Structured design:
Dividing a problem into smaller subproblems
Structured programming:
Implementing a structured design
The structured design approach is also called:
Top-down (or bottom-up) design
Stepwise refinement
Modular programming
49
49
Program
50
51
Problem : Compute and print the average of three
numbers.
52
Input n1,n2,n3
S = n1+n2+n3
Average = s / 3
Output average
Compute and print the average of
53
three numbers.
#include <iostream.h>
#include <conio.h>
int main()
{
int a, b ,c,sum, average;
cout << "Please enter three integers:\n";
cin >> a >> b >> c;
sum = a+b+c;
average = sum/3;
cout << "The average of 3 numbers is: " << average;
Basics of a Typical
Program is created in
C++ Environment Editor Disk the editor and stored
on disk.
54
Preprocessor Disk
Preprocessor program
Phases of C++ processes the code.
Compiler creates
Programs: Compiler Disk object code and stores
it on disk.
4. Link ..
Primary
Memory
5. Load CPU
CPU takes each
instruction and
executes it, possibly
6. Execute ..
storing new data
values as the program
..
.. executes.
55
Integrated Development Environments
56
(IDEs)
An integrated development environment, or IDE,
combine all the tools needed to write, compile, and
debug a program into a single software
application.
Examples are Microsoft Visual C++, Turbo C++
Explorer, CodeWarrior, Borland C++ Builder etc.
Trubo c++
57
Integrated Development Environments
58
(IDEs)
Processing a Program
59
59
Processing a Program (continued)
60
60
Linkers
Some programs we use are already compiled
Theirobject code is available for us to use
For example: Input and output routines
A Linker combines
The object code for the programs we write
and
The object code for the pre-compiled routines
into
The machine language program the CPU can run
61
Programming with the Problem
Analysis–Coding–Execution Cycle
Programming is a process of problem solving
One problem-solving technique:
Analyze the problem
Outline the problem requirements (inputs/outputs?)
Algorithm:
Step-by-step problem-solving process
Solution achieved in finite amount of time
62
62
Programming with the Problem
Analysis–Coding–Execution Cycle
Step 1 - Analyze the problem
Outline the problem and its requirements
Design steps (algorithm) to solve the problem
Step 3 - Maintenance
Use and modify the program if the problem domain
changes
63
63
Analyze the Problem
64
64
Design an Algorithm
65
65
Write the Code
66
66
Compiling and Linking
67
67
The Loader and Executing
68
68
69
1.1 Algorithms and programs ...
compiler
CPU binary
solution!
program
70 ¨
C++
C++ is an extension of C.
C++ was first developed in the early 1980s
(back in the last century!).
Focus was on Object Oriented Programming
viewcomputer programs as collection of objects.
Objects have attributes and actions.
71
History of C and C++
History of C
Evolved from two other programming languages
BCPL and B: “Typeless” languages
73
Parts of a C++ Program
74
comment
// sample C++ program
preprocessor
#include <iostream> directive
using namespace std; which namespace
to use
int main() beginning of
function named main
{ beginning of
block for main
cout << "Hello World"; output
statement
return 0;
string
} end of block literal
for main send 0 to
operating system
75
Programming is
fun!
The \n Escape Sequence
82
Programming is
fun!
cin
84
intVariable
#include <conio.h>
85
Quiz 1
86