Sunteți pe pagina 1din 7

Programming a Computer

A computer is a device which processes information according to instructions it has been given. A
program is a set of instructions given to a computer. The activity of writing instructions for a computer is
called programming. Computer programs are written using programming languages such as C, C++, Java,
Pascal, and Visual Basic. Thus, writing a computer program basically involves telling a computer what to
do, using various mechanisms provided in a programming language. In this lecture, we will first describe
how a computer is organized and then explain how a program can be written to instruct the computer
to perform a certain task.

1.1 Computer Organization

All computers have the same basic internal organization, built around an inputprocessingoutput
model. Computer hardware refers to the physical computer equipment, such as the keyboard, the
mouse, the monitor, the hard disk, and the printer. Computer software refers to the programs that
cause the computer to perform different tasks.

Figure 1.1 shows the internal organization of a computer. Each component is briefly described below.

Internal External
Memory Memory

Control Unit

Input Output
Device Arithmetic and Device
Logic Unit (ALU)

e.g., keyboard Central Processing Unit e.g., monitor


(CPU)

Figure 1.1: Internal Organization of a Computer

1
CPU
The CPU is the Central Processing Unit or processor. It consists of the Control Unit and the ALU.
The CPU is contained in a single integrated-circuit chip, which contains millions of components
in an area smaller than a postage stamp.

Control Unit
The Control Unit controls all the activities within the CPU. It accepts input values (from an input
device) and stores them in memory. It also interprets the instructions in a computer program. If
we wanted to perform a calculation, the control unit retrieve s the values needed for the
calculation from memory and sends them to the ALU. The ALU performs the calculation and
returns the result to the Control Unit which then stores it in memory.

ALU
The ALU is the Arithmetic and Logic Unit. It performs all the arithmetic and logic functions in a
computer such as addition, subtraction, comparison, etc.

Input Device
Input devices are used to get information from the user into the computers memory. There are
many kinds of input devices. Some of the most common include keyboard, mouse, and scanner.

Output Device
Output devices are used to communicate the results of processing back to the user. Some of the
more widely used output devices include monitor and printer (e.g., laser printer and ink -jet
printer).

Internal Memory
This includes Random Access Memory (RAM) and Read Only Memory (ROM). Internal memory is
used to store data. RAM is volatile, so as soon as the electrical power is cut off, all the data is
lost.

2
External Memory / Secondary Storage Devices
Secondary storage devices are used to provide an abundant storage space for programs and
data. Examples are hard disk drives (C drive in PCs) and flash drives. Data are generally stored in
these devices as files, which are not volatile. The term save is used to denote the act of
copying data in memory to a non-volatile file in secondary storage. Although files are not
volatile, they can be changed from time to time or be removed forever (this is referred to as
deleting a file).

1.2 How a Computer Executes Instructions

Computers are built to execute instructions. The range of instructions that can be executed by a
computer is referred to as that computer's instruction set. Different computers have different
instruction sets.

An instruction consists of a string of bits (a '1' or a '0') called a word that directs the computer to
perform a certain operation. For example, the following instruction may direct the computer to add two
numbers, where the leftmost part represents "Add", and the two rightmost parts are the numbers to be
added:

1001 101011 111010

It is very difficult to write programs using strings of 1's and 0's. It is also very error-prone. An
improvement over this is to use symbols for the operators and operands, e.g., ADD = 1001, A = 101011,
B = 111010. When this is done, the above instruction can be written as:

ADD A B

However, programming is still tedious and error-prone. Programs written using strings of 1's and 0's or
equivalent symbols are said to be written in low-level programming languages. They cannot be easily
transferred from one type of computer to another, since different computers have different instruction
sets. For example, the code "1001" given to the ADD instruction above might actual ly represent the
MULTIPLY instruction in another kind of computer.

3
1.3 High-Level Programming Languages

A high-level programming language is oriented towards solving problems, rather than towards the
instruction set of a particular computer. There are a number of high-level programming languages such
as C, C++, Java, Visual Basic and Pascal. Each language consists of a grammar (set of rules ) and pre-
defined words for writing instructions (i.e., programming) in the language. A program written in a high -
level language is called a source program and the programming statements in the program are referred
to as source code.

1.4 Our First Program

The following program is written in the C++ programming language:

#include <iostream>

using namespace std;

int main () {
cout << "Hello World of Computer Programming!";

return 0;
}

The source code of the program must first be typed into an editor using characters from the keyboard.
The program should then be saved to a file in external storage so that it will not be lost when power is
turned off. The above program should be saved to a file called HelloWorld.cpp. The .cpp extension is
used for files contain C++ source code.

Earlier, it was mentioned that computers only understand machine level instructions expressed as a
string of bits. The HelloWorld.cpp program written in the C++ programming language consists only of a
set of characters typed at the keyboard. So, how is it possible to get from a C++ source program to an
equivalent set of machine-level instructions, known as the object code? A compiler is used to translate a
source program into object code. The compiler is itself a computer program that accepts as input a
source program and after translation, produces as output the object code.

In this course, we will use the Dev C++ Integrated Development Environment (IDE). The Dev C++ IDE
contains an editor for typing the source code of a program. The Compile menu option can be used to

4
compile the source code to object code when the source code statements have been completely typed.
The Dev C++ IDE uses the GNU C++ compiler to compile the source code.

If compilation is successful, the object program, HelloWorld.exe is produced. This program can then be
executed from the IDE using the Run menu option. If there are errors in writing the statements in the
C++ language (called syntax errors), the compiler will not generate an executable program. Instead, it
will give a list of the lines with errors and an indication of the nature of each error. These errors must be
corrected before an executable file is obtained. Figure 1.2 shows the process of getting from source
code to object code.

Source Translation Object


Program by Program
Compiler

HelloWorld.cpp C++ Compiler HelloWorld.exe

Figure 1.2: From Source Program to Executable Program

When the HelloWorld program is run, the following output is displayed on another window:

Hello World of Computer Programming!

The cout statement in the HelloWorld program causes the program to display the string (the set of
characters in quotation marks) on the window. cout essentially stands for Console Output and
causes the string to be displayed on the console or window.

Suppose we wanted another line of output to be displayed on the window, as follows:

Hello World of Computer Programming!


Here I come!

5
The program can be modified or re-written with another cout statement to display the second string,
Here I come!, as follows:

#include <iostream>

using namespace std;

int main () {
cout << "Hello World of Computer Programming!";
cout << "Here I come!";

return 0;
}

However, after compiling and running the program, we get the following output:

Hello World of Computer Programming!Here I come!

This is not what we wanted. We wanted the second string to be displayed in a new line by itself. This can
be accomplished by telling the cout command to go to a new line, before displaying the second string.
To go to a new line, the endl (end line) keyword can be used:

cout << endl;

The program then becomes:

#include <iostream>

using namespace std;

int main () {
cout << "Hello World of Computer Programming!";
cout << endl;
cout << "Here I come!";

return 0;
}

When compiled and executed, it generates the desired output:

Hello World of Computer Programming!


Here I come!

6
1.5 Exercises

1. Write a program to display your name on one line, your degree programme on a second line, and
your email address on a third line.

2. Write a program to display the translation of the following words from English to Spanish on the
monitor:

English Spanish
dog perro
cat gato
horse caballo
cow vaca
computer computador

3. Write a program to display the following conversion table on the monitor:

Centigrade Fahrenheit
========== ==========
-40 -40
0 32
10 50
20 68
30 86
40 104

4. Write a program to display the following pattern of stars (asterisks) on the monitor:

**********
*********
********
*******
******
*****
****
***
**
*

S-ar putea să vă placă și