LECTURE 1
1
What is computer?
Computer
A device capable of performing computations and making logical
decisions
A machine that manipulates data according to a list of instructions.
A programmable device that can store, retrieve, and process data.
Computer programs
Sets of instructions that control a computer’s processing of data
Hardware
Physical part of the computer
Various devices comprising a computer
Examples: keyboard, screen, mouse, disks, memory, CD-ROM, and processing
units
Software
A collection of computer programs, procedures and documentation that
perform some tasks on a computer system
Programs that run a computer
2
Computer organization
There are Six logical units in every computer:
Input unit
Obtains information (data and computer programs) from input
devices (keyboard, mouse)
Output unit
Outputs information to output device (screen, printer) or to control
other devices.
Memory unit
Rapid access, low capacity, stores input information
Arithmetic and logic unit (ALU)
Performs arithmetic calculations and logic decisions
Central processing unit (CPU)
Supervises and coordinates the other sections of the computer
Secondary storage unit
Cheap, long-term, high-capacity storage, stores inactive programs
3
Computer languages
Computer languages are divided into three types.
Machine languages
Machine language is machine dependent.
Strings of numbers giving machine specific instructions
Example:
+1300042774
+1400593419
+1200274027
Assembly languages
English-like abbreviations representing elementary computer
operations (translated via assemblers)
Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
Translator programs called assembler were developed to convert assembly
language programs to machine language programs at computer speed.
4
Computer languages
High-level languages
Similar to everyday English, use mathematical notations
(translated via compilers)
Example:
grossPay = basePay + overTimePay
C, C++ are the most widely used high level languages. Some
other examples are
FORTRAN (formula translator)
Used in scientific and engineering applications
COBOL (common business oriented language)
Used to manipulate large amounts of data
Pascal
Used to teach structured programming
Translator programs called Compilers converts high-level
language programs into machine language 5
Basics of a typical C++ environment
Program is created in
Editor
Phases of C++ Programs
Disk the editor and stored
on disk.
to be executed Preprocessor Disk
Preprocessor program
processes the code.
Edit Compiler creates
Preprocess Compiler Disk object code and stores
it on disk.
Compile Linker Disk
Linker links the object
code with the libraries,
creates a.out and
Link Primary
stores it on disk
Memory
Load Loader
Execute Loader puts program
in memory.
Disk ..
..
..
Primary
Memory
CPU CPU takes each
instruction and
executes it, possibly
storing new data
.. values as the program
.. executes. 6
..
Program organization
Program statement
Definition
Declaration
Action
Executable unit
Named set of program statements
Different languages refer to executable units by different names
Subroutine: Fortran and Basic
Procedure: Pascal
Function : C++
7
C++ programming
C++ program
Collection of definitions, declarations and functions
Collection can span multiple files
Advantages
Structured into small understandable units
Complexity is reduced
Overall program size decreases
8
Programming and Problem Solving
Pseudo code
Artificial, informal language used to develop algorithms
Similar to everyday English
Not executed on computers
Used to think out program before coding
Easy to convert into C++ program
Only executable statements
No need to declare variables
9
Programming and Problem Solving
Algorithm
A sequence of precise instructions which
leads to a solution
Program
An algorithm expressed in a language the computer
can understand
10
Program Design
Programming is a creative process
Program Design Process
Problem Solving Phase
Result is an algorithm that solves the problem
Implementation Phase
Result is the algorithm translated into a programming
language
11
Problem Solving Phase
Be certain the task is completely specified
What is the input?
What information is in the output?
How is the output organized?
Develop the algorithm before implementation
Experience shows this saves time in getting your
program to run.
Test the algorithm for correctness
12
Implementation Phase
Translate the algorithm into a programming
language
Easier as you gain experience with the language
Compile the source code
Locates errors in using the programming language
Run the program on sample data
Verify correctness of results
Results may require modification of
the algorithm and program
13
C++ Programming
Simple program to print a line of text.
Comments
Written between /* and */ or following a //.
1 // A first program in C++ Improve program readability and do not cause the computer to perform
any action.
2 #include <iostream.h> preprocessor directive
Message to the C++ preprocessor.
3
C++ programs containLines
one orbeginning
more with # are preprocessor directives.
functions, one of which must be main
#include <iostream> tells the preprocessor to include the contents of the file
4 int main() which includes input/output operations (such as printing to the screen).
Parenthesis are used to<iostream>,
indicate a
function
The left brace ,{, line 5 must begin the body of function and the
5 { int means that maincorresponding
"returns" an right brace ,}, line 9 must end the body of each function.
integer value.
6 cout << "Welcome to C++!\n";
Prints the string of characters contained between the quotation marks.
7
The entire line, including cout, the << operator, the string "Welcome
to C++!\n" and the semicolon (;), is called a statement.
8 return 0; // indicate that programmust
All statements ended
end with a semicolon.
successfully
9 }
return is a way to exit a function from a function.
return 0, in this case, means that the program
terminated normally. It is one of the several means
14
used to exit a function
C++ Programming
cout
Standard output stream object
“Connected” to the screen
<<
Stream insertion operator
Value to the right of the operator (right operand) inserted into
output stream (which is connected to the screen)
cout << “Welcome to C++!\n”;
15
C++ Programming
1 // an example to observe using statement
2 // program to display greeting
3 #include <iostream.h>
5 int main()
6 {
7 cout << “Hello world\n";
9 return 0; // indicate that program ended successfully
10 }
16
Escape Character
Indicates that a “special” character is to be output
Escape Sequence Description
\n Newline. Position the screen cursor to the
beginning of the next line.
\t Horizontal tab. Move the screen cursor to the
next tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to
the next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote character.
17
C++ Programming
There are multiple ways to print text. Following are some more examples.
1 //observing the use of \n
2 // Printing a line with multiple statements
3 #include <iostream.h>
4
5 int main()
6 {
7 cout << "Welcome ";
8 cout << "to C++!\n";
9
10 return 0; // indicate that program ended successfully
11 }
The output would be as bellow
Welcome to C++!
Unless new line '\n' is specified, the text continues
18
on the same line.
C++ Programming
1 // printing multiple lines with a single statement
2 // Printing multiple lines with a single statement
3 #include <iostream.h>
5 int main()
6 {
7 cout << "Welcome\nto\n\nC++!\n";
9 return 0; // indicate that program ended successfully
10 }
Welcome
To
C++! Multiple lines can be printed with one 19
statement.
Testing and Debugging
Bug
A mistake in a program
Debugging
Eliminating mistakes in programs
20
Program Errors
Syntax errors
Violation of the grammar rules of the language
Discovered by the compiler
Error messages may not always show correct location of
errors
Run-time errors
Error conditions detected by the computer at run-time
Logic errors
Errors in the program’s algorithm
Most difficult to diagnose
Computer does not recognize an error
21