Handouts For Lab 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING


COMPUTER ENGINEERING DEPARTMENT

Computer Programming Lab


Handouts for Lab 1
An Overview of Computers and Programming Languages
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

Elements of a Computer System:


A computer is an electronic device capable of performing commands. The basic commands that
a computer performs are input (get data), output (display result), storage, and performance of
arithmetic and logical operations. There are two main components of a computer system:
hardware and software. We will give a brief overview of these components. Let’s look at
hardware first.

Hardware:
Major hardware components include the central processing unit (CPU); main memory (MM),
also called random access memory (RAM); input/output devices; and secondary storage. Some
examples of input devices are the keyboard, mouse, and secondary storage.

Examples of output devices are the screen, printer, and secondary storage. Let’s look at each of
these components in greater detail.

Central Processing Unit and Main Memory:


The central processing unit is the ‘‘brain’’ of the computer and the single most expensive piece
of hardware in a computer. The more powerful the CPU, the faster the computer. Arithmetic and
logical operations are carried out inside the CPU. Figure 1 (a) shows some hardware
components.

Figure 1: Hardware components of computer and main memory.

Main memory, or random-access memory, is connected directly to the CPU. All programs must
be loaded into main memory before they can be executed. Similarly, all data must be brought
into main memory before a program can manipulate it. When the computer is turned off,
everything in main memory is lost.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

Main memory is an ordered sequence of cells, called memory cells. Each cell has a unique
location in main memory, called the address of the cell. These addresses help you access the
information stored in the cell. Figure 1-1(b) shows main memory with some data.

Today’s computers come with main memory consisting of millions to billions of cells. Although
Figure 1 (b) shows data stored in cells, the content of a cell can be either a programming
instruction or data. Moreover, this figure shows the data as numbers and letters. However, as
explained later, main memory stores everything as sequences of 0s and 1s. The memory
addresses are also expressed as sequences of 0s and 1s.

Secondary Storage:
Because programs and data must be stored in main memory before processing and because
everything in main memory is lost when the computer is turned off, information stored in main
memory must be transferred to some other device for permanent storage. The device that stores
information permanently (unless the device becomes unusable, or you change the information by
rewriting it) is called secondary storage. To be able to transfer information from main memory to
secondary storage, these components must be directly connected to each other. Examples of
secondary storage are hard disks, flash drives, floppy disks, ZIP disks, CD-ROMs, and tapes.

Figure 2: Secondary storage devices.

Input /Output Devices:


For a computer to perform a useful task, it must be able to take in data and programs and display
the results of calculations. The devices that feed data and programs into computers are called
input devices. The keyboard, mouse, and secondary storage are examples of input devices. The
devices that the computer uses to display results are called output devices. A monitor, printer,
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

and secondary storage are examples of output devices. Figure 3 shows some input and output
devices.

Figure 3: Input and output devices.

Software:
Software are programs written to perform specific tasks. For example, word processors are
programs that you use to write letters, papers, and even books. All software is written in
programming languages.

There are two types of programs:

• System programs/software.
• Application programs/software.

System programs
control the computer. The system program that loads first when you turn on your PC is called the
operating system. Without an operating system, the computer is useless. The operating system
monitors the overall activity of the computer and provides services. Some of these services
include memory management, input/output activities, and storage management. The operating
system has a special program that organizes secondary storage so that you can conveniently
access information.

Application programs
perform a specific task. Word processors, spreadsheets, and games are examples of application
programs. The operating system is the program that runs application programs.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

The Language of a Computer:


When you press A on your keyboard, the computer displays A on the screen. But what is stored
inside the computer’s main memory? What is the language of the computer? How does it store
whatever you type on the keyboard? Remember that a computer is an electronic device.
Electrical signals are used inside the computer to process information. There are two types of
electrical signals: analog and digital. Analog signals are continuous wave forms used to represent
such things as sound. Audio tapes, for example, store data in analog signals. Digital signals
represent information with a sequence of 0s and 1s. A 0 represents a low voltage, and a 1
represents a high voltage. Digital signals are more reliable carriers of information than analog
signals and can be copied from one device to another with exact precision. You might have
noticed that when you make a copy of an audio tape, the sound quality of the copy is not as good
as the original tape. On the other hand, when you copy a CD, the copy is as good as the original.
Computers use digital signals.

Because digital signals are processed inside a computer, the language of a computer, called
machine language, is a sequence of 0s and 1s. The digit 0 or 1is called a binary digit, orbit.
Sometimes a sequence of 0s and 1s is referred to as a binary code or a binary number.

Bit is a binary digit 0 or 1. A sequence of eight bits is called a byte. Moreover, 210 bytes = 1024
bytes is called a kilobyte (KB).

Every letter, number, or special symbol (such as*or { ) on your keyboard is encoded as a
sequence of bits, each having a unique representation. The most used encoding scheme on
personal computers is the seven-bit American Standard Code for Information Interchange
(ASCII). The ASCII data set consists of 128 characters numbered 0 through 127. That is, in the
ASCII data set, the position of the first character is 0, the position of the second character is 1,
and so on. In this scheme, A is encoded as the binary number 1000001. In fact, A is the 66th
character in the ASCII character code, but its position is 65 because the position of the first
character is 0. Furthermore, the binary number 1000001is the binary representation of 65. The
character 3 is encoded as 0110011. Note that in the ASCII character set, the position of the
character 3 is 51, so the character 3 is the 52nd character in the ASCII set. It also follows that
0110011 is the binary representation of 51.

Inside the computer, every character is represented as a sequence of eight bits, that is, as a byte.
Now the eight-bit binary representation of 65 is 01000001. Note that we added 0 to the left of the
seven-bit representation of 65 to convert it to an eight-bit representation. Similarly, the eight-bit
binary representation of 51 is 00110011.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

ASCII is a seven-bit code. Therefore, to represent each ASCII character inside the computer, you
must convert the seven-bit binary representation of an ASCII character to an eight-bit binary
representation. This is accomplished by adding 0 to the left of the seven-bit ASCII encoding of a
character. Hence, inside the computer, the character A is represented as 01000001, and the
character 3 is represented as 00110011.

There are other encoding schemes, such as EBCDIC (used by IBM) and Unicode, which is a
more recent development. EBCDIC consists of 256 characters; Unicode consists of 65,536
characters. To store a character belonging to Unicode, you need two bytes.

The Evolution of Programming Languages:


The most basic language of a computer, the machine language, provides program instructions in
bits. Even though most computers perform the same kinds of operations, the designers of the
computer may have chosen different sets of binary codes to perform the operations. Therefore,
the machine language of one machine is not necessarily the same as the machine language of
another machine. The only consistency among computers is that in any modern computer, all
data is stored and manipulated as binary codes.

Early computers were programmed in machine language. To see how instructions are written in
machine language. The need to remember specific codes made programming not only very
difficult, but also error prone. Assembly languages were developed to make the programmer’s
job easier. In assembly language, an instruction is an easy-to-remember form called a mnemonic.
It is much easier to write instructions in assembly language. However, a computer cannot
execute assembly language instructions directly. The instructions first must be translated into
machine language. A program called an assembler translates the assembly language instructions
into machine language.

Assembler: A program that translates a program written in assembly language into an


equivalent program in machine language.

Moving from machine language to assembly language made programming easier, but a
programmer was still forced to think in terms of individual machine instructions. The next step
toward making programming easier was to devise high-level languages that were closer to
natural languages, such as English, French, German, and Spanish. Basic, FORTRAN, COBOL,
Pascal, C, C++, C#, and Java are all high-level languages. You will learn the high-level language
C++. The instruction written in C++ is much easier to understand and is self-explanatory to a
new user who is familiar with basic arithmetic. As in the case of assembly language, however,
the computer cannot directly execute instructions written in a high-level language. To run on a
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

computer, these C++ instructions first need to be translated into machine language. A program
called a compiler translates instructions written in high level languages into machine code.

Compiler: A program that translates instructions written in a high-level language into the
equivalent machine language.

Processing a C++ Program:


In the previous sections, we discussed machine language and high-level languages and showed a
C++ program. Because a computer can understand only machine language, you are ready to
review the steps required to process a program written in C++.

Consider the following C++ program:


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

My first C++ program:


Recall that a computer can understand only machine language. Therefore, to run this program
successfully, the code must first be translated into machine language. In this section, we review
the steps required to execute programs written in C++. The following steps are necessary to
process a C++ program as shown in Figure 4.

1. Use a text editor to create a C++ program following the rules, or syntax, of the high-level
language. This program is called the source code, or source program. The program must
be saved in a text file that has the extension .cpp.
Source program: A program written in a high-level language.
2. The C++ program given in the preceding section contains the statement
#include<iostream>. In a C++ program, statements that begin with the symbol # are
called preprocessor directives. These statements are processed by a program called
preprocessor.
3. After processing preprocessor directives, the next step is to verify that the program obeys
the rules of the programming language—that is, the program is syntactically correct—and
translate the program into the equivalent machine language. The compiler checks the
source program for syntax errors and, if no error is found, translates the program into the
equivalent machine language. The equivalent machine language program is called an
object program.
Object program: The machine language version of the high-level language program.
4. The programs that you write in a high-level language are developed using an integrated
development environment (IDE). The IDE contains many programs that are useful in
creating your program. For example, it contains the necessary code (program) to display
the results of the program and several mathematical functions to make the programmer’s
job somewhat easier. Therefore, if certain code is already available, you can use this code
rather than writing your own code. Once the program is developed and successfully
compiled, you must still bring the code for the resources used from the IDE into your
program to produce a final program that the computer can execute. This prewritten code
(program) resides in a place called the library. A program called a linker combines the
object program with the programs from libraries.
Linker: A program that combines the object program with other programs in the
library and is used in the program to create the executable code.
5. You must next load the executable program into main memory for execution. A program
called a loader accomplishes this task.
Loader: A program that loads an executable program into main memory.
6. The final step is to execute the program.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

Figure 4: Steps to execute a simple c++ program.

As a programmer, you need to be concerned only with Step 1. That is, you must learn,
understand, and master the rules of the programming language to create source programs. As
noted earlier, programs are developed using an IDE. Well-known IDEs used to create programs
in the high-level language C++ include Visual Studio 2020 (from Microsoft), and C++ Builder
(from Borland). You can also use Dev-C++ IDE from Bloodshed Software to create and test C++
programs. These IDEs contain a text editor to create the source program, a compiler to check the
source program for syntax errors, a program to link the object code with the IDE resources, and a
program to execute the program.

These IDEs are quite user friendly. When you compile your program, the compiler not only
identifies the syntax errors, but also typically suggests how to correct them. Moreover, with just
a simple command, the object code is linked with the resources used from the IDE.

Programming with the Problem Analysis–Coding–Execution


Cycle:
Programming is a process of problem solving. Different people use different techniques to solve
problems. Some techniques are nicely outlined and easy to follow. They not only solve the
problem, but also give insight into how the solution was reached. These problem-solving
techniques can be easily modified if the domain of the problem changes.

One common problem-solving technique includes analyzing a problem, outlining the problem
requirements, and designing steps, called an algorithm, to solve the problem.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

Algorithm: A step-by-step problem-solving process in which a solution is arrived at in a


finite amount of time.

1. In a programming environment, the problem-solving process requires the following three


steps:
2. Analyze the problem, outline the problem and its solution requirements, and design an
algorithm to solve the problem.
3. Implement the algorithm in a programming language, such as C++, and verify that the
algorithm works.
4. Maintain the program by using and modifying it if the problem domain changes.

Figure 5: Problem analysis-coding-execution cycle.

Analyzing the problem is the first and most important step. This step requires you to do the
following:
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

1. Thoroughly understand the problem.


2. Understand the problem requirements. Requirements can include whether the program
requires interaction with the user, whether it manipulates data, whether it produces
output, and what the output looks like. If the program manipulates data, the programmer
must know what the data is and how it is represented. That is, you need to look at sample
data. If the program produces output, you should know how the results should be
generated and formatted.
3. If the problem is complex, divide the problem into sub problems and repeat Steps 1 and
2. That is, for complex problems, you need to analyze each sub problem and understand
each sub problem’s requirements.

After you carefully analyze the problem, the next step is to design an algorithm to solve the
problem. If you broke the problem into sub problems, you need to design an algorithm for each
sub problem. Once you design an algorithm, you need to check it for correctness. You can
sometimes test an algorithm’s correctness by using sample data. At other times, you might need
to perform some mathematical analysis to test the algorithm’s correctness.

Once you have designed the algorithm and verified its correctness, the next step is to convert it
into an equivalent programming code. You then use a text editor to enter the programming code
or the program into a computer. Next, you must make sure that the program follows the
language’s syntax. To verify the correctness of the syntax, you run the code through a compiler.
If the compiler generates error messages, you must identify the errors in the code, remove them,
and then run the code through the compiler again.

When all the syntax errors are removed, the compiler generates the equivalent machine code, the
linker links the machine code with the system’s resources, and the loader places the program into
main memory so that it can be executed.

The final step is to execute the program. The compiler guarantees only that the program follows
the language’s syntax. It does not guarantee that the program will run correctly. During
execution, the program might terminate abnormally due to logical errors, such as division by
zero. Even if the program terminates normally, it may still generate erroneous results. Under
these circumstances, you may have to reexamine the code, the algorithm, or even the problem
analysis.

Your overall programming experience will be successful if you spend enough time to complete
the problem analysis before attempting to write the programming instructions. Usually, you do
this work on paper using a pen or pencil. Taking this careful approach to programming has
several advantages. It is much easier to discover errors in a program that is well analyzed and
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

well designed. Furthermore, a carefully analyzed and designed program is much easier to follow
and modify. Even the most experienced programmers spend a considerable amount of time
analyzing a problem and designing an algorithm.

Next, we provide examples of various problem-analysis and algorithm-design techniques.

Example:

Programming Methodologies:
Two popular approaches to programming design are the structured approach and the object-
oriented approach, which are outlined below.

Structured Programming:
Dividing a problem into smaller sub problems is called structured design. Each sub problem is
then analyzed, and a solution is obtained to solve the sub problem. The solutions to all the sub
problems are then combined to solve the overall problem. This process of implementing a
structured design is called structured programming. The structured-design approach is also
known a stop-down design, bottom-up design, stepwise refinement, and modular programming.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

Object-Oriented Programming:
Object-oriented design (OOD) is a widely used programming methodology. In OOD, the first
step in the problem-solving process is to identify the components called objects, which form the
basis of the solution, and to determine how these objects interact with one another. For example,
suppose you want to write a program that automates the video rental process for a local video
store. The two main objects in this problem are the video and the customer.

After identifying the objects, the next step is to specify for each object the relevant data and
possible operations to be performed on that data. For example, for a video object, the data might
include:

• movie name
• starring actors
• producer
• production company
• number of copies in stock

Some of the operations on a video object might include:

• checking the name of the movie


• reducing the number of copies in stock by one after a copy is rented
• incrementing the number of copies in stock by one after a customer returns a
particular video.

This illustrates that each object consists of data and operations on that data. An object combines
data and operations on the data into a single unit. In OOD, the final program is a collection of
interacting objects. A programming language that implements OOD is called an object-oriented
programming (OOP) language. For some problems, the structured approach to program design
will be very effective. Other problems will be better addressed by OOD.

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

You might also like