Lec 1
Lec 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
Program is created in
Editor
Phases of C++ Programs
Disk the editor and stored
on 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
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
Implementation Phase
Result is the algorithm translated into a programming
language
11
Problem Solving Phase
12
Implementation Phase
Translate the algorithm into a programming
language
Easier as you gain experience with the language
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.
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
3 #include <iostream.h>
5 int main()
6 {
10 }
16
Escape Character
Indicates that a “special” character is to be output
Escape Sequence Description
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 }
3 #include <iostream.h>
5 int main()
6 {
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