1 - Introduction to Computers and C++ Programming
1 - Introduction to Computers and C++ Programming
Programming
• 1.1 Computer Systems
CPU
Secondary
memory
• The computer has five main components
– Input devices
– Output devices
– Central processing unit (CPU)
– Main memory
– Secondary memory
• The CPU and main memory form the heart of a
computer and can be thought of as an integrated unit.
• An input device is any device that allows a person to
communicate information to the computer e.g. Keyboard
and mouse
• An output device is anything that allows the computer to
communicate information to you. E.g. Video display unit
• The computer has two forms of memory called the main
memory and secondary memory.
• The program that is being executed is kept in the main
memory (often referred to as RAM or random access
memory.
• The main memory consists of a long list of numbered
locations called memory locations. The number of
memory locations varies from computer to computer.
• The memory locations in most computers contain eight
bits. An eight-bit portion of memory is called a byte.
• The number that identifies a byte is called its address.
• If the computer needs to deal with a data item (such as a
large number) that is too large to fit in a single byte, it will
use several adjacent bytes to hold the data item. In this
case the entire chunk of memory that holds the data item
is still a memory location.
• Memory locations and bytes
Byte 1
3 byte location with
Byte 2 address 1
Byte 3
2 byte location with
address 4
1 byte location with address 6
Program Data
Computer
Output
• 1.1.3 High-Level Languages
• There are many languages for writing programs.
• C++ is a high-level language
• Other high-level languages include
– C, Pascal, FORTRAN, BASIC, COBOL, Modula-2, Lisp, Scheme
and Ada
• High-level languages resemble human languages in
many ways. They are designed to be easy for human
beings to write programs in and to be easy for human
beings to read.
• A high level language contains instructions that are much
more complicated than the simple instructions a
computer is capable of following.
• The kind of language a computer can understand is
called a low-level language.
• The exact details of a low-level language differ from
computer to computer.
• A typical low-level instruction might be the following:
– ADD X Y Z
• This instruction might mean “add the number in the
memory location X to the number in the memory location
called Y and place the result in the memory location
called Z”.
• The above sample instruction is written in what is called
assembly language. The assembly language must be
translated into string of zeros and ones before the
computer can understand it.
• Programs written in the form of zeros and ones are said
to be written in Machine language.
• 1.1.4 Compilers
• A program that translates a high-level language like C++
to a machine language is called a compiler.
• A compiler its input or data is some other program and
its output is yet another program.
• The input program is called source program or code
• The translated version produced by the compiler is called
the object program or object code.
• The object code for your C++ program must be
combined with the object code for routines (such as input
and output routines) that your program uses. This
process of combining object code is called linking and is
done by a program called a linker. For simple programs
linking may be done for you automatically.
• Preparing a C++ Program for running
C++ program
Compiler
Linker
Complete machine
language code
ready to run
• 1.2 Programming and Problem-Solving
• 1.2.1 Algorithms
• The most difficult part of solving a problem on a
computer is discovering the method of solution. After you
come up with a method of solution, it is routine to
translate your method into the required language.
• A sequence of precise instructions which lead to a
solution is called an algorithm. Some approximate
equivalent words are recipe, method, directions,
procedure and routine
• The instruction may be expressed in a programming
language or human language.
• Our algorithms will be expressed in English
• A computer program is simply an algorithm expressed in
a language that a computer can understand.
• An Algorithm
• 1 Get the list of names
• 2 Get name being checked
• 3 Set a counter to zero
• 4 Do the following for each name on the list:
– Compare the name on the list of the name being checked and if
the names are the same, then add one to the counter
• 5 Announce that the answer is the number indicated by
the counter
• The above algorithm determines the number of times a
specified name occurs on a list of names
• 1.2.2 Program Design
• Designing a program is often a difficult talk with no
complete set of rules, no algorithm to tell you how to
write programs. Program design is a creative process.
• The outline is given in the diagram below. The entire
process can be divided into two phases: the problem-
solving phase and the implementation phase.
• The result of the problem-solving phase is an
algorithm, expressed in English, for solving the problem.
• To produce a program in a programming language, the
algorithm is translated into the programming language .
Producing the final program from the algorithm is called
the implementation phase.
• Program Design Process
Start Implementation phase
Problem definition
#include <iostream.h>
int main()
{
int number_of_pods, peas_per_pod, total_peas;
cout << “press return after entering a number. \n”;
cout << “Enter the number of pods: \n”;
cin >> number_of_pods;
cout << “Enter the number of peas in a pod: \n”;
cin >> peas_per_pod;
return 0;
}
• Sample Dialogue
int main()
{
Variable_Declarations
Statement_1
Statement_2
…..
Statement_Last
return 0;
}
• The first line
#include <iostream.h>