Handouts For Lab 1
Handouts For Lab 1
Handouts For Lab 1
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.
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.
and secondary storage are examples of output devices. Figure 3 shows some 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.
• 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
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.
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.
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.
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
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.
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
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
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.
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
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.
****************************