Sunteți pe pagina 1din 279

Computer Basics

and Qbasic Programming


Dr. Yasser Fouda
Ph. D. of Computer Science
Computer Science Division, Faculty of Science,
Mansoura University

Computer Basics Dr. Yasser Fouda 1


Contents
Chapter(1): Introduction to Computer
Chapter(2): Data Representation
Chapter(3): Number Representation
Chapter(4): Flowcharts
Chapter(5): Introduction to Qbasic
Computer Basics Dr. Yasser Fouda 2
Course Information
Course Name: Computer Science (1)
Course Code: 101
Credit Hours: 2
Tutorial: 1 hours
Level: First

Computer Basics Dr. Yasser Fouda 3


Grading System
Mid-Term: 10%
Final: 60%
Oral: 10%
Practical: 20%
Warnings:
Absent in mid-term and oral exam, will take zero.
Computer Basics Dr. Yasser Fouda 4
Chapter (1)
Introduction to Computer
What is the Computer?
A computer is a device which can process data and perform computations
and logical operations without human intervention.
You can think of a computer as a data processor. Using this definition, a
computer accepts input data, process the data, and creates output data.

Input Data Computer Output Data

Computer Basics Dr. Yasser Fouda 5


Computer Applications
In Business and Industry
In Publication Field
In Education Field
In Government Organizations
In Medical Field
In Science Field
In Entertainment Field
Computer Basics Dr. Yasser Fouda 6
Parts of a Computer
1. Input Devices (Keyboard, Mouse, etc.)
2. Output Devices (Monitor, Speakers, etc.)
3. Central Processing Unit (CPU) and memory
4. Motherboard (Circuit Board)
5. Expansion Cards (Video Card, Sound Card, or NIC)
6. Hard Drive Ports and Connections (USB, Firewire, etc.)

Computer Basics Dr. Yasser Fouda 7


Parts of a Computer
(Input Devices)
Mouse

Keyboard

Microphone

Scanner

Webcam

Computer Basics Dr. Yasser Fouda 8


Parts of a Computer
(Output Devices)
Monitor

Printer

Speaker/Headphone

Computer Basics Dr. Yasser Fouda 9


Parts of a Computer
(Central Processing Unit (CPU))
CPU is a device that interprets and runs the commands that you give to the
computer.
Also referred to as the processor.
Two major brands are Intel and AMD.
Examples: Intel Core 2 Duo, AMD Turion X2

Computer Basics Dr. Yasser Fouda 10


Parts of a Computer
(Memory)
Memory is where information is stored and retrieved by the CPU.
There are two main types of memory:
Random Access Memory (RAM): It is the main memory and allows you
to temporarily store commands and data.

Read Only Memory (ROM): It is the memory that retains its contents
even after the computer is turned off.

Computer Basics Dr. Yasser Fouda 11


Parts of a Computer
(Motherboard)
Motherboard is the main circuit board inside the computer.
Has tiny electronic circuits and other components on it.
Basic functions of motherboard:
Connects input, output, and processing devices together (CPU, Memory,
Hard Drive, DVD Drive)
Tells the CPU how to run.

Computer Basics Dr. Yasser Fouda 12


Parts of a Computer
(Motherboard)

Computer Basics Dr. Yasser Fouda 13


Parts of a Computer
(Expansion Cards)
Video Card is connected to the computer monitor and is used to display
information on the monitor.

Network Interface Card (NIC) is connected to other computers so that


information can be exchanged between them.

Sound Card converts audio signals (microphone or audio tape) which can
be stored as a computer audio file.

Computer Basics Dr. Yasser Fouda 14


Parts of a Computer
(Storage Devices)
Hard Disk

Flash Drive

CD-ROM

DVD-ROM

Computer Basics Dr. Yasser Fouda 15


Parts of a Computer
(Ports and Connections)
Universal Serial Bus (USB) Port
FireWire (similar to USB but faster)
Network Port (Ethernet)
Parallel Port and Serial Port
Display Adapter
Power

Computer Basics Dr. Yasser Fouda 16


Using a Computer
Basic steps for using the computer:
1. To turn on a computer, press the power button on the system unit.
2. Log on to the operating system by your user name and password.
3. Computers allow you to create, modify, and save files.
4. To shut down a computer, click Start, and then, at the bottom of the Start
menu, click Shut Down.

Computer Basics Dr. Yasser Fouda 17


Using a Keyboard

Computer Basics Dr. Yasser Fouda 18


Using a Mouse
A mouse is a small device that you can use to move, select, and open items
displayed on your monitor.
Most mouse devices have at least two buttons, left and right.
Mouses action:
o Point
o Double Click
o Left Click
o Right Click
o Click-and-Drag or Drag-and-Drop
Computer Basics Dr. Yasser Fouda 19
Common Computer Terminology
(Hardware)
Hardware Refers to all the physical components of a computer, includes all
input devices, processing devices, storage devices, and output devices.
The parts that made up a computer divided into three subsystems: The CPU,
main memory, and the input/output (I/O) subsystems. The figure 1.1 shows the
three subsystems of a stand-alone computer.
Central processing unit (CPU): performs operations on data. It has three
parts: an arithmetic logic unit (ALU), a control unit, and a set of registers
(figure 1.2).
ALU performs arithmetic and logical operations.
Computer Basics Dr. Yasser Fouda 20
Common Computer Terminology
(Hardware)
Arithmetic operation: The simplest unary operations are increment (add 1)
and decrement (subtract 1). The simplest binary operations are add, subtract,
multiply, and divide. The control unit is responsible for selecting one of
these operations.
Logical operation: The simplest logical unary operation is the NOT
operation. The simplest logical binary operations are AND, OR, and XOR.
The control unit is responsible for selecting one of these operations.

Computer Basics Dr. Yasser Fouda 21


Figure 1.1 Computer hardware subsystem

CPU Memory

Input/Output

Computer Basics Dr. Yasser Fouda 22


Figure 1.2 CPU

R1
R2
R3

ALU I

Control Unit
PC
Registers

Computer Basics Dr. Yasser Fouda 23


Common Computer Terminology
(Hardware)
Registers: are fast stand-alone storage locations that hold data temporarily.
Multiple registers are needed to facilitate the operation of the CPU.
Data Registers: Some of these registers are in figure 1.2. For simplicity,
we show only three general registers: two for input data and one for
output data (registers R1, R2, and R3).
Instruction Register: Computers store not only data but also the
corresponding program inside memory. The CPU is responsible for
fetching instructions, one by one, from memory, storing them in the
instruction register (register I in figure 1.2).

Computer Basics Dr. Yasser Fouda 24


Common Computer Terminology
(Hardware)
Program Counter: Another common register in the CPU is the program
counter (register PC in Figure 1.2). The program counter keeps track of the
instruction currently being executed. After execution of the instruction, the
counter is incremented to point to the address of the next instruction in
memory.
Control Unit: is like the part of the human brain that controls the operation
of each part of the body. It controls the operations of the memory, ALU,
and the input/output subsystem.

Computer Basics Dr. Yasser Fouda 25


Common Computer Terminology
(Hardware)
Main memory: is another subsystem in computer (Figure 1.3). It is a
collection of storage locations, each with a unique identifier.
Data are transferred to and from memory in groups of bits called words.
A word can be a group of 8 bits, 16 bits, 32 bits, or sometimes 64 bits.
If the word is 8 bits, it is referred to as a byte, the term byte is a common in
computer science.

Computer Basics Dr. Yasser Fouda 26


Hardware Addresses Values

0000000000000000 01111001
0000000000000010 10010100
0000000000000100 10000000

. .
. .
. .

1111111111111101 11110000
1111111111111110 11100000
1111111111111111 00000111

Figure 1.3 Main memory Memory


Computer Basics Dr. Yasser Fouda 27
Common Computer Terminology
(Hardware)
Address space: To access a word in memory requires an identifier. Although
programmers use a name to identify a word, at the hardware level, each word
is identified by an address.
The total number of uniquely identifiable locations in memory is called the
address space.
For example, a memory with 64 kilobytes and a word size of 1 byte has an
address space that ranges from 0 to 65535
The following table shows the units used to refer to memory:

Computer Basics Dr. Yasser Fouda 28


Common Computer Terminology
(Hardware)
unit Exact number of bytes Approximation
Kilobyte 210 1024 103
Megabyte 220 1048576 106
Gigabyte 230 1073741824 109
Terabyte 240 1012
Petabyte 250 1015
Exabyte 260 1018
Memory Units
Computer Basics Dr. Yasser Fouda 29
Common Computer Terminology
(Type of memory)
RAM ROM
Makes up most of the main memory in a computer Are written by the manufacturer
Can be read from and written to by the user. The user is allowed to read but not write to ROM.
It is a volatile It is nonvolatile
RAM technology is divided into two broad ROM technology is divided into three broad
categories: categories:
1. Static RAM (SRAM) 1. Programmable ROM (PROM)
2. Dynamic RAM (DRAM) 2. Erasable Programmable ROM (EPROM)
3. Electronically Erasable Programmable ROM
(EEPROM)

Computer Basics Dr. Yasser Fouda 30


Common Computer Terminology
(Operating Systems )
An operating system is an interface between the hardware of a computer
and the user that facilitates the execution of other programs and the access
to hardware and software resources.
Functions of Operating system:
o Controls and manages the hardware connected to your computer.
o Provides an interface that helps you to interact with the computer.
o Ensures that your computer is functioning properly.

Computer Basics Dr. Yasser Fouda 31


Common Computer Terminology
(Operating Systems )

Windows XP is an example of the operation system.

Can be either text-based (DOS) or graphics-based (Windows).

Graphics-based operating system help you install hardware or software with


easy-to-use interface.

Computer Basics Dr. Yasser Fouda 32


Common Computer Terminology
(Program )
The hardware and the operating system together are referred to as a
platform.

Programs, use this platform to perform tasks.

The term program refers to a specific set of instructions given to the


computer to accomplish a specific task.

Computer Basics Dr. Yasser Fouda 33


Common Computer Terminology
(Program )
There are many types of programs:
Some programs allow you to perform tasks such as writing letters,
doing calculations, or sending e-mail messages.
Other programs allow you to create illustrations, play games, watch
movies, or communicate with other computer users.

Computer Basics Dr. Yasser Fouda 34


Common Computer Terminology
(Data and Information)
The words "data" and "information" are often used as if they are synonyms.
Nevertheless, they have different meanings.
Data: Raw material, from which you can draw conclusions. Facts from which
you can deduce new facts.
Information: Knowledge, intelligence, a particular piece of data with a
special meaning or function. Information is often the result of combining,
comparing, and performing calculations on data.

Computer Basics Dr. Yasser Fouda 35


Common Computer Terminology
(Data and Information)
Programs process data that you provide as input to your computer.
Data can be in the form of text, graphics, audio, or video depending on the
type of program.
Program processes the data and displays the output (information) on the
screen.
You can save this output (information) in a file.
The file can be an audio file, a text file, a graphics file, or a video file.
Computer Basics Dr. Yasser Fouda 36
Common Computer Terminology
(Networks)
Network is a group of computers and associated devices that are linked
together to facilitate sharing information.
Can also be used to share data and devices such as printers.
A typical network has the following three components:
o Server(A computer that stores and manages files for multiple users on a
network)
o Workstation (This is a computer that is directly used by someone to do
work).
o Communication channel (Cable or telephone line).
Computer Basics Dr. Yasser Fouda 37
Categories of Networks
(Local Area Network (LAN))
LAN Connects devices within a limited area (home or a small group of
offices).
Contains printers, servers and computers.
Systems are close to each other.
Contained in one office or building.
Organizations often have several LANS.

Computer Basics Dr. Yasser Fouda 38


Categories of Networks
(Metropolitan Area Network (MAN))
MAN spans a city or a town and provides services to individual users or
organizations.
Large network that connects different
organizations.
Shares regional resources.
A network provider sells time.

Computer Basics Dr. Yasser Fouda 39


Categories of Networks
(Wide Area Network (WAN))
WAN is a network that connects devices in geographically separated areas.
It is used to connect networks across different countries.
The Internet is an example of a WAN.
Two or more LANs connected
Over a large geographic area
Typically use public or leased lines
Phone lines
Satellite
The Internet is a WAN
Computer Basics Dr. Yasser Fouda 40
Internet
Internet is a worldwide collection of public networks that are linked to each
other for information exchange.
Internet offers a range of services to its users (file transfers between Internet
users and electronic mail)
The World Wide Web (WWW), or the Web, is another service that the
Internet provides.
An intranet is similar to the Web but is accessible only to authorized users of
the organization.

Computer Basics Dr. Yasser Fouda 41


Chapter (2)
Data Representation
As we saw a computer is a data processing machine. In this chapter we need
to understand the nature of data, also we discuss the different data and how
they are represent inside a computer.
Data Types: Data today come in different forms such as numbers, text, images,
audio, and video (fig. 2.1).
An engineering program uses a computer mostly to process numbers: to do
arithmetic, to solve algebraic or trigonometric equations, to find the roots of
a differentia equation, and so on.
Computer Basics Dr. Yasser Fouda 42
Data Representation
(Data Types)
A word processing program, on the other hand, uses a computer mostly to
process text: justify, move, delete, and so on.

An image processing program uses a computer to manipulate images: create,


shrink, expand, rotate, and so on.
A computer also handles audio. You can listen Quran from a computer and
can enter your voice as a data.
Finally, a computer cab be used not only to show movies, but also to create the
special effects seen in movies.
Computer Basics Dr. Yasser Fouda 43
Data Representation
(Data Types)
Data

Text Number Image Audio Video

Fig. 2.1 Different types of data

Computer Basics Dr. Yasser Fouda 44


Data inside the computer
All data type from outside a computer are transformed into a uniform
representation when stored in a computer and then transformed back when
leaving the computer. The uniform representation is called a bit pattern.
A bit (binary digit) is the smallest unit of data that can be stored in a
computer, it is either 0 or 1.
A bit represents the state of a device that can take one of two states. For
example, a switch can be either on or off.
Computer Basics Dr. Yasser Fouda 45
Data inside the computer
The convention is to represent the on state as 1 and the off state as 0.
An electronic switch can represent a bit. In other words, a switch can store
one bit of information. Today, computers use various two-state devices to
store data.
To represent different types of data, we use a bit pattern, a sequence, or as
it is sometimes called, a string of bits.
Figure 2.2 shows a bit pattern made of 16 bits. It is a combination of 0s
and 1s. This means that if you need to store a bit pattern made of 16 bits,
you need 16 electronic switches.
Computer Basics Dr. Yasser Fouda 46
Data inside the computer

1000101010111111
Fig. 2.2 Bit pattern
How does computer memory know what type of data a stored bit pattern
represents?
It does not. Computer memory just stores the data as bit patterns.
It is the responsibility of input/output devices or programs to interpret a bit
pattern as a number, text, or some other type of data.
Computer Basics Dr. Yasser Fouda 47
Data inside the computer
In other words, data are coded when they enter a computer and decoded
when they are represented to the user (Fig. 2.3).
By tradition, a bit pattern of length eight is called a byte. This term has also
been used to measure the size of memory or other storage devices.
For example, a computer memory that can store 8 million bits of
information is said to have a memory of 1 million bytes.

Computer Basics Dr. Yasser Fouda 48


Data inside the computer
Text Coding 1001111 M 1111111 Decoding Text

Number Coding 1111101 E 1101101 Decoding Number

Image 1111111 M
Coding 1011000 Decoding Image
O
Audio Coding 1000000 0101001 Decoding Audio
R
Video Coding 1011001 1111111 Decoding Video
Y
Fig. 2.3 Examples of bit patterns 49
Computer Basics Dr. Yasser Fouda
Representing Data
(Text)
A piece of text in any language is a sequence of symbols used to represent
an idea in that language.
For example, the English language uses 26 symbols (A,B,C,,Z) to represent
uppercase letter, 26 symbols (a, b, c,,z) to represent lowercase letters, 10
symbols (0,1,2,,9) to represent numeric characters and symbols (., ?, :, ; ,
,!) to represent punctuation. Other symbols such as the blank, the newline,
and the tab are used for text alignment and readability.

Computer Basics Dr. Yasser Fouda 50


Representing Data
(Text)
You can represent each symbol with a bit pattern. In other words, text such
as BYTE, which is made of four symbols, can be represented as a 4 bit
patterns, each pattern defining a single symbol (fig. 2.4).

B Y T E
101101 111100 110100 000101
Fig. 2.4 Representing symbols using bit patterns
Computer Basics Dr. Yasser Fouda 51
Representing Data
(Text)
How many bits are needed in a bit pattern to represent a symbol in a
language? It depends on how many symbols are in the set.
The relationship is not linear, it is logarithmic. If you need two symbols the
length is 1 bit (2 2 is 1). If you need four symbols, the length is 2 bits
(2 4 is 2).
Table in the next slide shows the relationship, which you can easily see.
A bit pattern of 2 bits can take four different forms: 00, 01, 10, 11. Each of
these forms can represent a symbol. In the same way, a bit pattern of 3 bits
can take eight different forms: 000, 001, 010, 011, 100, 101, 110, and 111.
Computer Basics Dr. Yasser Fouda 52
Representing Data
(Text)
Number of symbols Bit pattern length
2 1
4 2
8 3
16 4

128 7
256 8

65536 16
Number of symbols and bit pattern length
Computer Basics Dr. Yasser Fouda 53
Representing Data
(Codes)
Different sets of bit patterns have been designed to represent text symbols.
Each set is called a code, and the process of representing symbols is called
coding.
The American National Standards Institute developed a code called ASCII
(American Standard Code for Information Interchange.
This code uses 7 bits for each symbol. This means 128 (27 ) different
symbols can be defined by this code. The full bit patterns for ASCII code are
in the next slide.
Computer Basics Dr. Yasser Fouda 54
Computer Basics Dr. Yasser Fouda 55
Representing Data
(Codes)
Figure 2.5 shows how BYTE is represented in ASCII code.

B Y T E
1000010 1011001 1010100 1000101
Figure 2.5 Representation of the word BYTE in ASCII code
Computer Basics Dr. Yasser Fouda 56
Representing Data
(Codes)
The following highlights some features of ASCII:
1. Uses a 7-bit pattern ranging from 0000000 to 1111111.
2. The first pattern 0000000 represents the null character.
3. The last pattern 1111111 represents the delete character.
4. There are 31 control (nonprintable) characters.
5. The numeric characters (0 to 9) are coded before letters.
Computer Basics Dr. Yasser Fouda 57
Representing Data
(Codes)
6. There are several special printable characters.
7. The uppercase letters (A Z) come before the lowercase letters (a z).
8. The upper- and lowercase characters are distinguished by only 1 bit. For
example, the pattern for A is 1000001; the pattern for a is 1100001. The
only difference is in the sixth bit from the right.
9. There are six special characters between the upper and lower case letters.

Computer Basics Dr. Yasser Fouda 58


Representing Data
(Codes)
EBCDIC: In the early age of computers, IBM developed a code called
Extended Binary Coded Decimal Interchange Code (EBCDIC).
This code uses 8-bit patterns, so it can represent up to 256 symbols.
However, this code is not used in any computers other than IBM
mainframes.
Unicode: Neither of the foregoing codes represent symbols belonging to
languages other than English. For that, a code with much more capacity is
needed.

Computer Basics Dr. Yasser Fouda 59


Representing Data
(Codes)
A coalition of hardware and software manufacturers have designed a code
named Unicode that uses 16 bits and can represent up to 65536 (216 )
symbols.
Different sections of the code are allocated to symbols from different
language in the world.
Some parts of the code are used for graphical and special symbols.
The JAVA language uses this code to represent characters. Microsoft
Windows uses a variation of the first 256 characters.
Computer Basics Dr. Yasser Fouda 60
Representing Data
(Codes)
ISO: The International Organization for Standardization, known as ISO,
has designed a code using 32-bit patterns.
This code can represent up to 4294967296 (232 ) symbols, definitely enough
to represent any symbol in the world today.

Computer Basics Dr. Yasser Fouda 61


Representing Data
(Numbers)
In a computer, numbers are represented using the binary system. In this
system, a pattern of bits ( a sequence of 0s and 1s) represent a number.
However, a code such as ASCII is not used to represent data. The reason for
this and discussion of number representation are presented in the next
chapter.

Computer Basics Dr. Yasser Fouda 62


Representing Data
(Images)
Images today are represented in a computer by one of two methods:
bitmap graphic or vector graphic.
Image

Bitmap Vector

Image representation methods


Computer Basics Dr. Yasser Fouda 63
Representing Data
(Images)
Bitmap Graphic: In this method, and image is divided into a matrix of
pixels (picture elements), where each pixel is a small dot.
The size of the pixel depends on what is called the resolution.
For example, an image can be divided into 1000 pixels or 10000 pixels. In the
second case, although there is a better representation of the image (better
resolution), more memory is needed to store the image.
After dividing an image into pixels, each pixel is assigned a bit pattern. The
size and the value of the pattern depend on the image.
Computer Basics Dr. Yasser Fouda 64
Representing Data
(Images)
For an image made of only black and white dots, a 1-bit pattern is enough to
represent a pixel. A pattern of o represents a black pixel and a pattern of 1
represents a white pixel.
The patterns are then recorded one after another and stored in the computer. Figure
2.7 shows an image of this kind and its representation.
If an image is not made of pure white and pure black pixels, you can increase the
size of bit pattern to represent gray scales.
For example, to show four levels of gray scale, you can use 2-bit patterns. A black
pixel can be represented by 00, a dark gray pixel by 01, a light gray pixel by 10, and a
white pixel by 11.
Computer Basics Dr. Yasser Fouda 65
Representing Data
(Images)
0 0 0 1 1 0 0 0
0 0 1 1 1 1 0 0
0 0 1 1 1 1 0 0
0 0 0 1 1 0 0 0
Image Matrix Representation

00011000 00111100 001111000 00011000


Linear Representation

Figure 2.7 Bitmap graphic method of a black and white image


Computer Basics Dr. Yasser Fouda 66
Representing Data
(Images)
To represent color images, each colored pixel is decomposed into three
primary color: red, green, and blue (RGB). Then the intensity of each color
is measured, and a bit pattern (usually 8 bits) is assigned to it.
In other words, each pixel has three bit patterns: one to represent the
intensity of the red color, one to represent the intensity of the green color,
and one to represent the intensity of the blue color.
For example, figure 2.8 shows four bit patterns for some pixels in a color
image.
Computer Basics Dr. Yasser Fouda 67
Representing Data
(Images)
R G B

Red (with 100% intensity) 11111111 00000000 00000000
Green (with 100% intensity) 00000000 11111111 00000000
Blue (with 100% intensity) 00000000 00000000 11111111
White (with 100% intensity) 11111111 11111111 11111111

Figure 2.8 Representation of color pixels


Computer Basics Dr. Yasser Fouda 68
Representing Data
(Images)
Vector Graphic: The problem with the bitmap graphic method is that the
exact bit patterns representing a particular image must be stored in a
computer. Later, if you want to rescale the image, you must change the size
of the pixels, which creates a ragged or grainy look.
The vector graphic method, however, does not store the bit patterns. An image
is decomposed into a combination of curves and lines. Each curve or line is
represented by a mathematical formula.

Computer Basics Dr. Yasser Fouda 69


Representing Data
(Images)
For example, a line may be described by the coordinates of its endpoints, and
a circle may be described by the coordinates of its centers and the length of
its radius.
The combination of these formulas is stored in a computer. When the image
is to be displayed or printed, the size of the image is given to the system as
an input.
The system redesigns the image with the new size and uses the same formula
to draw the image. In this case, each time an image is drown, the formula is
reevaluated.
Computer Basics Dr. Yasser Fouda 70
Representing Data
(Audio)
Audio is a representation of sound or music. Although there is no standard
to store sound or music, the idea is to convert audio to digital data and use
bit patterns to store them.
Audio is by nature analog data. It is continuous (analog), not discrete (digital).
Figure 2.9 shows the steps to change audio data to bit patterns. They are as
follows:

Computer Basics Dr. Yasser Fouda 71


Representing Data
(Audio)
1. The analog signal is sampled. Sampling means measuring the value of the
signal at equal intervals.
2. The samples are quantized. Quantization means assigning a value (from a
set) to a sample. For example, if the value of a sample is 29.2 and the set is
the set of integers between 0 and 63, the sample is assigned a value of 29.
3. The quantized values are changed to binary patterns. For example, the
number 25 is changed to the binary pattern 00011001.
4. The binary patterns are stored.
Computer Basics Dr. Yasser Fouda 72
Representing Data
(Audio)

Sampling

Quantization
Figure 2.9 Audio representation

0000010000001111 Coding
4 6 4 25 15
Computer Basics Dr. Yasser Fouda 73
Representing Data
(Video)
Video is a representation of images (called frames) in time. A movie is a
series of frames shown one after another to create the illusion of motion.
So, if you know how to store an image inside a computer, you also know
how to store a video; each image or frame is changed to a set of bit patterns
and stored.
The combination of the images represents the video. Note that today video
is normally compressed.

Computer Basics Dr. Yasser Fouda 74


Hexadecimal Notation
The bit pattern is designed to represent data when they are stored inside a
computer. However, people find it difficult to manipulate bit patterns.
Writing a long stream of 0s and 1s is tedious and prone to error.
Hexadecimal notation is based on 16. This means that there are 16
symbols (hexadecimal digits): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.
The importance of hexadecimal notation becomes clear when converting a
bit pattern into hexadecimal notation.

Computer Basics Dr. Yasser Fouda 75


Hexadecimal Notation
Each hexadecimal digit can represent 4 bits, and 4 bits can represented by a
hexadecimal digit. The following table shows the relationship between a bit
pattern and hexadecimal digit.

A 4-bit pattern can be represented by a hexadecimal digit, and vice versa

Computer Basics Dr. Yasser Fouda 76


Hexadecimal Notation
Bit pattern Hexadecimal digit Bit pattern Hexadecimal digit
0000 0 1000 8
0001 1 1001 9
0010 2 1010 A
0011 3 1011 B
0100 4 1100 C
0101 5 1101 D
0110 6 1110 E
0111 7 1111 F
Computer Basics Dr. Yasser Fouda 77
Hexadecimal Notation
(Conversion)
Converting from a bit pattern to hexadecimal is done by organizing the
pattern into groups of four and finding the hexadecimal value for each
group of 4 bits.
For hexadecimal to bit pattern conversion, convert each hexadecimal digit to
its 4-bits equivalent (Figure 2.10).

Computer Basics Dr. Yasser Fouda 78


Hexadecimal Notation
(Conversion)

1111 1100 1110 0100 Hexadecimal

F C E 4

Fig. 2.10 Binary to hexadecimal and hexadecimal to binary transformation

Computer Basics Dr. Yasser Fouda 79


Hexadecimal Notation
(Conversion)
Note that the hexadecimal notation is written in two formats. In the first
format, you add a lowercase (or upper case) x before the digits to show that
the representation is in hexadecimal.
For example, xA34 represents a hexadecimal value in this convention. In the
other format, you indicate the base of the number (16) as the subscript after
the notation. For example, 3416 shows the same value in the second
convention. We use both conventions in these lectures.

Computer Basics Dr. Yasser Fouda 80


Hexadecimal Notation
(Conversion)
Example: Show the hexadecimal equivalent off the bit pattern 110011100010
Solution: xCE2.
Example: Show the hexadecimal equivalent off the bit pattern 0011100010.
Solution: x0E2.
Example: What is the bit pattern for x24C?
Solution: 001001001100.

Computer Basics Dr. Yasser Fouda 81


Octal Notation
Another notation used to group bit patterns together is octal notation. Octal
notation is based on 8. This means that there are eight symbols (octal digit):
0, 1, 2, 3, 4, 5, 6, 7. The importance of octal notation becomes clear as you
learn to convert a bit pattern into octal notation.
Each octal digit can represent 3 bits, and 3 bits can represented by an octal
digit. The following table shows the relationship between a bit pattern and an
octal digit.
A 3-bit pattern can be represented by an octal digit, and vice versa
Computer Basics Dr. Yasser Fouda 82
Octal Notation

Bit pattern Octal digit Bit pattern Octal digit


000 0 100 4
001 1 101 5
010 2 110 6
011 3 111 7

Computer Basics Dr. Yasser Fouda 83


Octal Notation
(Conversion)
Converting from a bit pattern to octal is done by organizing the pattern into
groups of three and finding the octal value for each group of 3 bits.
For octal to bit pattern conversion, convert each octal digit to its 3-bits
equivalent (Figure 2.11).

Computer Basics Dr. Yasser Fouda 84


Octal Notation
(Conversion)

1 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0 Octal

1 7 6 3 4 4

Fig. 2.11 Binary to octal and octal to binary transformation

Computer Basics Dr. Yasser Fouda 85


Octal Notation
(Conversion)
Note that the octal notation is also written in two formats. In the first
format, you add 0 (zero) before the digits to show that the representation is
in octal (sometimes a lowercase o is used) .
For example, 0634 represents an octal value in this convention. In the other
format, you indicate the base of the number (8) as the subscript after the
notation. For example, 6348 shows the same value in the second convention.
We use both conventions in these lectures.

Computer Basics Dr. Yasser Fouda 86


Octal Notation
(Conversion)
Example: Show the octal equivalent of the bit pattern 101110010.
Solution: 0562, o562, or 5628 .
Example: Show the octal equivalent of the bit pattern 1100010.
Solution: 0142, o142, or 1428 ..
Example: What is the bit pattern for 1428 ?
Solution: 010100.

Computer Basics Dr. Yasser Fouda 87


Chapter(3)
Number Representation
Two numbering systems are dominant today in the world of computers:
decimal and binary.
Decimal system: Today, the world uses the decimal system for numbers
developed by Arabian mathematicians in the eighth century.
The first people to use a decimal numbering system were the ancient
Egyptians. The Babylonians improved on the Egyptian system by making
the positions in the numbering system meaningful.
Why the second position in the decimal system is ten and the third position
is hundred?
Computer Basics Dr. Yasser Fouda 88
Number Representation
(Decimal system)
The answer lies in the power of the base of the system, which in decimal is
10.
Thus, the first position is 10 raised to the power 0, the second position is 10
raised to the power 1, and the third position is 10 raised to power 2. Figure
3.1 shows the relationship between the powers and the number 243.

Computer Basics Dr. Yasser Fouda 89


Number Representation
(Decimal system)
104 103 102 101 100
10000 1000 100 10 1
Decimal positions
243

2*100 + 4*10 + 3*1

Two Hundred Forty-Three Figure 3.1 Decimal system


Computer Basics Dr. Yasser Fouda 90
Number Representation
(Binary system)
Whereas the decimal system is based on 10, the binary system is based on
2. There are only two digits in the binary system, 0 and 1.
Figure 3.2 shows the positional weights for a binary system and the value 243
in binary.
In the position table, each position is double the previous position. Again,
this is because the base of the system is 2.

Computer Basics Dr. Yasser Fouda 91


Number Representation
(Binary system)
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
Binary positions
1 1 1 1 0 0 1 1

1*128 + 1*64 + 1*32 + 1*16 + 0*8 + 0*4 + 1*2 + 1*1

Two Hundred Forty-Three Figure 3.2 Binary system


Computer Basics Dr. Yasser Fouda 92
Number Representation
(Conversion)
Before discussing how numbers in the form of bit pattern are stored inside a
computer, you should understand how to manually convert a number from
the decimal system to the binary system, and vice versa.
We start by converting a number from the binary system to the decimal
system. Start with the binary number and multiply each binary digit by its
weight.
Since each binary bit can be only 0 or 1, the result will be either 0 or the
value of the weight. After multiplying all the digits, add the results.
Binary to decimal conversion is shown in figure 3.3.
Computer Basics Dr. Yasser Fouda 93
Number Representation
(Conversion)

0 1 0 1 1 0 1 Binary number

64 32 16 8 4 2 1 Position values

0 + 32 + 0 + 8 + 4 + 0 + 1 results

45 Decimal number
Figure 3.3 Binary to decimal conversion
Computer Basics Dr. Yasser Fouda 94
Number Representation
(Conversion)
Example: Convert the binary 10011 to decimal.
Solution:

Binary 1 0 0 1 1
weights 16 8 4 2 1

16 + 0 + 0 + 2 + 1
Decimal 19

Computer Basics Dr. Yasser Fouda 95


Number Representation
(Conversion)
To convert from decimal to binary, use repetitive division. The original
number, 45 in the example, is divided by 2. The reminder (1) becomes the
first binary digit, and the second digit is determined by dividing the quotient
(22) by 2. Again, the reminder (0) becomes the binary digit, and the quotient
is divided by 2 to determine the next position.
This process continue until the quotient is 0. Decimal to binary conversion
is shown in figure 3.4.

Computer Basics Dr. Yasser Fouda 96


Number Representation
(Conversion)
Stop when the
quotient is 0 Quotient

0 1 2 5 11 22 45

Reminder
1 0 1 1 0 1
Binary
Figure 3.4 Decimal to binary conversion
Computer Basics Dr. Yasser Fouda 97
Number Representation
(Conversion)
Example: Convert the decimal number 35 to binary.
Solution:
0 1 2 4 8 17 35 Decimal

Binary 1 0 0 0 1 1

Computer Basics Dr. Yasser Fouda 98


Integer Representation

Now that you know how to transform from decimal system to the binary
system, let u see how to store integers inside a computer.
Integers are whole numbers (i.e., numbers without fraction). For example,
134 is an integer, but 134.23 is not. As another example, -134 is an integer,
but -134.567 is not.
An integer can be positive or negative. A negative integer ranges from
negative infinity to 0; a positive integer ranges from 0 to positive infinity.

Computer Basics Dr. Yasser Fouda 99


Integer Representation

However, no computer can store all the integers in this range. To do so


would require an infinite number of bits, which means a computer with
infinite storage capability.
To use computer memory more efficiently, two board categories of integer
representation have been developed: unsigned integers and signed integers.
Signed integers may also be represented in three distinct ways (Figure 3.6).

Computer Basics Dr. Yasser Fouda 100


Integer Representation

Integer
Representation

Unsigned Signed

Sign-and- Ones Twos


Figure 3.6 Taxonomy of integers Magnitude Complement Complement
Computer Basics Dr. Yasser Fouda 101
Integer Representation

An unsigned integer is an integer without a sign. Its range is between 0 and


positive infinity. However, since no computer can possibly represent all the
integers in this range, most computers define a constant called the maximum
unsigned integer.
An unsigned integer ranges between 0 and this constant. The maximum
unsigned integer depends on the number of bits the computer allocates to
store an unsigned integer.

Computer Basics Dr. Yasser Fouda 102


Integer Representation

The following defines the range of unsigned integers in a computer, where


N is the number of bits allocated to represent one unsigned integer.
Range: 0 2 1
The following table shows two common ranges for computers today
Number of Bits Range
8 0255
16 065535

Computer Basics Dr. Yasser Fouda 103


Integer Representation

Representation: Storing unsigned integers is a straightforward process as


outlined in the following steps:
1. The number is changed to binary.
2. If the number of bits is less than N, 0s are added to the left of the binary number so
that there is a total od N bits.
Example: Store 7 in an 8-bit memory location.
Solution: First change the number to binary 111. Add five 0s to make a total
of 8 bits, 00000111. The number is stored in the memory location.
Computer Basics Dr. Yasser Fouda 104
Integer Representation

Example: Store 258 in an 16-bit memory location.


Solution: First change the number to binary 100000010. Add seven 0s to
make a total of 16 bits, 0000000100000010. The number is stored in the
memory location.
The following table shows how unsigned integers are stored in two different
computers: one using 8-bit allocation and one using 16-bit allocation.
Note that the decimal 258 and decimal 24760 cannot be stored in a computer
using 8-bit allocation for an unsigned integer.
Computer Basics Dr. Yasser Fouda 105
Integer Representation

Decimal 1245678 cannot be stored in either of these computers. A condition


called overflow occurs.
Decimal 8-bit allocation 16-bit allocation
7 00000111 0000000000000111
234 11101010 0000000011101010
258 Overflow 0000000100000010
24760 Overflow 0110000010111000
1245678 Overflow Overflow

Computer Basics Dr. Yasser Fouda 106


Integer Representation

Interpretation: How do you interpret an unsigned binary representation in


decimal?
The process is easy. Change the N bits from the binary system to the decimal
system as shown at the beginning of the chapter.
Example: Interpret 00101011 in decimal if the number was stored as an
unsigned integer.
Solution: Using the procedure shown in figure 3.3, the number in decimal is
43.
Computer Basics Dr. Yasser Fouda 107
Integer Representation
(Applications)
Unsigned integers representation can improve the efficiency of storage
because you do not need to store the sign of the integer. This means that the
entire bit allocation can be used for storing the number.
Unsigned integers representation can be used whenever you do not need
negative integers. The following lists some cases:
1. Counting, You start counting from 1 and go up,
2. Addressing: Address of memory location are positive numbers starting form 0 and
going up to a number representing the total memory capacity in bytes.
Computer Basics Dr. Yasser Fouda 108
Integer Representation
(Sign-and-Magnitude)
Storing an integer in the sign-and magnitude format requires 1 bit to
represent the sign (0 for positive, 1 for negative).
This means that in an 8-bit allocation, you can only use 7 bits to represent the
absolute value of the number (number without the sign).
Therefore, the maximum positive value is one half the unsigned value. The
following defines the range of sign-and magnitude integer in a computer,
where N is the number of bits allocated to represent one sign-and-magnitude
integer:
: 21 1 + 21 1
Computer Basics Dr. Yasser Fouda 109
Integer Representation
(Sign-and-Magnitude)

There are two 0s in sign-and-magnitude representation: positive and negative


In an 8-bit allocation:

+0 00000000
-0 10000000

Computer Basics Dr. Yasser Fouda 110


Integer Representation
(Sign-and-Magnitude)
The following table shows common ranges for computers today. Note that in
this system there are two 0s: +0 and -0.
Number of Bits Range
8 -127 -0 +0 +127
16 -32767 -0 +0 +32767
32 -2147483647 -0 +0 +2147483647

Range of sign-and-magnitude integers

Computer Basics Dr. Yasser Fouda 111


Sign-and-Magnitude
(Representation)
Storing sign-and-magnitude integers is a straightforward process:
1. The number is changed to binary; the sign is ignored.
2. If the number of bits is less than N-1, 0s are added to the left of the number so that
there is a total of N-1 bits.
3. If the number is positive, 0 is added to the left (to make it N bits). If the number is
negative, 1 is added to the left (to make it N bits).
In sign-and-magnitude representation, the leftmost bit defines the sign of the number.
If it is 0, the number is positive. If it is 1, the number is negative.
Computer Basics Dr. Yasser Fouda 112
Sign-and-Magnitude
(Representation)
Example: Store +7 in an 8-bit memory location using sign-and magnitude
representation.
Solution: First change the number to binary 111. Add four 0s to make a total
7 bits, 0000111. Add an extra zero because the number is positive. The result
is 00000111.
Example: Store -258 in an 16-bit memory location using sign-and magnitude
representation.

Computer Basics Dr. Yasser Fouda 113


Sign-and-Magnitude
(Representation)
Solution: First change the number to binary 100000010. Add six 0s to make a
total 15 bits, 000000100000010. Add an extra 1because the number is
negative. The result is 1000000100000010.
The following table shows how sign-and-magnitude are stored in two
different computers: one using 8-bit allocation and one using 16-bit
allocation.

Computer Basics Dr. Yasser Fouda 114


Sign-and-Magnitude
(Representation)
Decimal 8-bit allocation 16-bit allocation
+7 00000111 0000000000000111
-124 11111100 1000000001111100
+258 Overflow 0000000100000010
-24760 Overflow 1110000010111000

Computer Basics Dr. Yasser Fouda 115


Sign-and-Magnitude
(Interpretation)
How do you interpret a sign-and-magnitude binary representation in
decimal? The process is simple:
1. Ignore the first (leftmost) bit.
2. Change the (N-1) bits from binary to decimal as shown at the beginning of the
chapter.
3. Attach a + or a sign to the number based on the leftmost bit.
Example: Interpret 10111011 in decimal if the number was stored as sign-
and-magnitude integer.

Computer Basics Dr. Yasser Fouda 116


Sign-and-Magnitude
(Interpretation)
Solution: Ignoring the leftmost bit, the remaining bits are 0111011. This
number in decimal is 59. The leftmost bit is 1, so the number is -59.

Computer Basics Dr. Yasser Fouda 117


Ones Complement
(Format)
You may have noticed that the representation of a number in the binary
system is a matter of convention.
In sign-and-magnitude representation, we adopted the convention that the
leftmost bit represents the sign; this bit is not part of the value.
The designers of ones complement representation adopted a different
convention: To represent a positive number, use the convention adopted for
an unsigned integer. To represent a negative number, complement the
positive number.
Computer Basics Dr. Yasser Fouda 118
Ones Complement
(Format)
In other words, +7 is represented just like an unsigned number; -7 is
represented as the complement of +7.
In ones complement, the complement of a number is obtained by changing
all 0s to 1s and all 1s to 0s.
The following defines the range of ones complement integer in a computer,
where N is the number of bits allocated to represent a ones complement
integer:
: 21 1 + 21 1
Computer Basics Dr. Yasser Fouda 119
Ones Complement
(Format)
There are two 0s in ones complement representation: positive and negative
In an 8-bit allocation:

+0 00000000
-0 11111111

The following table shows common ranges for computers today. Note that in
this system there are two 0s: +0 and -0.

Computer Basics Dr. Yasser Fouda 120


Ones Complement
(Format)

Number of Bits Range


8 -127 -0 +0 +127
16 -32767 -0 +0 +32767
32 -2147483647 -0 +0 +2147483647

Range of ones complement integers

Computer Basics Dr. Yasser Fouda 121


Ones Complement
(Representation)
Storing ones complement integers requires the following steps:
1. The number is changed to binary; the sign is ignored.
2. 0s are added to the left of the number to make a total of N bits.
3. If the sign is positive, no more action is needed. If the sign is negative, every bit is
complemented (changed from 0 to 1 or from 1 to 0).

In ones complement representation, the leftmost bit defines the sign of the number.
If it is 0, the number is positive. If it is 1, the number is negative.

Computer Basics Dr. Yasser Fouda 122


Ones Complement
(Representation)
Example: Store +7 in an 8-bit memory location using ones complement
representation.
Solution: First change the number to binary 111. Add five 0s to make a total 8 bits,
00000111. The sign is positive, so no more action is needed.
Example: Store -258 in an 16-bit memory location using ones complement
representation.
Solution: First change the number to binary 100000010. Add seven 0s so that there
is a total 16 bits, 0000000100000010. The sign is negative, so each bit is
complemented. The result is 1111111011111101.
Computer Basics Dr. Yasser Fouda 123
Ones Complement
(Representation)
The following table shows how ones complement numbers are stored in two
different computers: one using 8-bit allocation and one using 16-bit
allocation. Decimal 8-bit allocation 16-bit allocation
+7 00000111 0000000000000111
-7 11111000 1111111111111000
+124 01111100 0000000001111100
-124 10000011 1111111110000011
+24760 Overflow 0110000010111000
-24760 Overflow 1001111101000111
Computer Basics Dr. Yasser Fouda 124
Ones Complement
(Interpretation)
How do you interpret a ones complement binary representation in decimal? The
process involves these steps:
1. If the leftmost bit is 0 (positive number),
A. Change the entire number from binary to decimal.
B. Put a plus sign (+) in front of the number.
2. If the leftmost bit is 1 (negative number),
A. Complement the entire number (change all 0s to 1s, and vice versa).
B. Change the entire number from binary to decimal.
C. Put a negative sign (-1) in front of the number.
Computer Basics Dr. Yasser Fouda 125
Ones Complement
(Interpretation)
Example: Interpret 11110110 in decimal if the number was stored as ones
complement integer.
Solution: The leftmost bit is 1, so the number is negative. First complement
it. The result is 00001001. The complement in decimal is 9. So the original
number was -9.
Overflow: If you try to store a ones complement integer such as +256 in an
8-bit memory location, you get a condition called overflow. Why?

Computer Basics Dr. Yasser Fouda 126


Twos Complement
(Format)
As previously mentioned, ones complement representation has two 0s (+0 and -0).
This can create some confusion in calculation.
In addition, you will see in the next chapter that if you add a number and its
complement (+4 and -4) in this representation, you get negative -0 instead of +0.
Twos complement representation solves all these problems.
The following defines the range of twos complement integer in a computer, where
N is the number of bits allocated to represent a twos complement integer:
: 21 + 21 1

Computer Basics Dr. Yasser Fouda 127


Twos Complement
(Format)
The following table shows common ranges for computers today. Note that in
this system there is only one 0 and that the beginning of the range is 1 less
than that of ones complement.
Number of Bits Range
8 -128 0 +127
16 -32768 0 +32767
32 -2147483648 0 +2147483647

Computer Basics Dr. Yasser Fouda 128


Twos Complement
(Representation)
Storing twos complement requires the following steps:
1. The number is changed to binary; the sign is ignored.
2. If the number of bits is less than N, 0s are added to the left of the number so that
there is a total of N bits.
3. If the sign is positive, no further action is needed. If the sign is negative, leave all the
rightmost 0s and the first 1 unchanged. Complement the rest.

In twos complement representation, the leftmost bit defines the sign of the number.
If it is 0, the number is positive. If it is 1, the number is negative.
Computer Basics Dr. Yasser Fouda 129
Twos Complement
(Representation)
Example: Store +7 in an 8-bit memory location using twos complement
representation.
Solution: First change the number to binary 111. Add five 0s that there is a total 8
bits, 00000111. The sign is positive, so no more action is needed.
Example: Store -40 in an 16-bit memory location using twos complement
representation.
Solution: First change the number to binary 101000. Add ten 0s so that there is a
total 16 bits, 0000000000101000. The sign is negative, so leave the rightmost 0s up
to the first 1 (including 1) unchanged and complement the rest. The result is
1111111111011000.
Computer Basics Dr. Yasser Fouda 130
Twos Complement
(Representation)
The following table shows how twos complement numbers are stored in two
different computers: one using 8-bit allocation and one using 16-bit
allocation. Decimal 8-bit allocation 16-bit allocation
+7 00000111 0000000000000111
-7 11111001 1111111111111001
+124 01111100 0000000001111100
-124 10000100 1111111110000100
+24760 Overflow 0110000010111000
-24760 Overflow 1001111101001000
Computer Basics Dr. Yasser Fouda 131
Twos Complement
(Representation)

There is only one 0 in twos complement: In an 8-bit allocation

0 00000000

Computer Basics Dr. Yasser Fouda 132


Twos Complement
(Interpretation)
How do you interpret a twos complement binary representation in decimal? The process
involves these steps:
1. If the leftmost bit is 0 (positive number),
A. Change the whole number from binary to decimal.
B. Put a plus sign (+) in front of the number.
2. If the leftmost bit is 1 (negative number),
A. Leave the rightmost bits up to the first 1 (inclusive) unchanged. Complement the rest of the bits
B. Change the whole number from binary to decimal.
C. Put a negative sign (-1) in front of the number.

Computer Basics Dr. Yasser Fouda 133


Twos Complement
(Interpretation)
Example: Interpret 11110110 in decimal if the number was stored as twos
complement integer.
Solution: The leftmost bit is 1. The number is negative. Leave 10 at the right
alone and complement the rest. The result is 00001010. The twos
complement number is 10. So the original number was -10.

Computer Basics Dr. Yasser Fouda 134


Twos Complement
(Interpretation)
Twos complement can be achieved by reversing all bits except the rightmost bits up to the
first 1 (inclusive). If you twos complement a positive number, you get the corresponding
negative number. If you twos complement a negative number, you get the corresponding
positive number. If you twos complement a number twice, you get the original number.

The twos complement representation is the standard representation for


storing integers in computers today. In the next chapter, you will see why this
is the case when you see the simplicity of operations using twos
complement.

Computer Basics Dr. Yasser Fouda 135


Summary of Integer Representation
To get you the big picture of number representation methods, examine the
following table. In this table, assume that N is 4. The memory location can
store only 4 bits.
If you look at the contents of the memory location, you can interpret the
number in one of the four representations.
Although the interpretation is the same for positive integers, it is different for
negative integers.

Computer Basics Dr. Yasser Fouda 136


Contents of memory Unsigned Sign-and-Magnitude Ones Complement Twos complement

0000 0 +0 +0 +0

0001 1 +1 +1 +1

0010 2 +2 +2 +2

0011 3 +3 +3 +3

0100 4 +4 +4 +4

0101 5 +5 +5 +5

0110 6 +6 +6 +6

0111 7 +7 +7 +7

1000 8 -0 -7 -8

1001 9 -1 -6 -7

1010 10 -2 -5 -6

1011 11 -3 -4 -5

1100 12 -4 -3 -4

1101 13 -5 -2 -3

1110 14 -6 -1 -2

1111 15 -7 -0 -1
Computer Basics Dr. Yasser Fouda 137
Excess System
Another representation that allows you to store positive and negative
numbers in a computer is called the Excess system.
In this system, it is easy to transform a number from decimal to binary, and
vice versa. However, operations on the numbers are very complicated.
The only application in use today is in storing the exponential value of a
fraction. In an Excess conversion, a positive number, called the magic
number, is used in the conversion process. The magic number is normal
21 or (21 -1), where N is the bit allocation.
Computer Basics Dr. Yasser Fouda 138
Excess System
For example, If N is 8, the magic number is either 128 or 127. In the first
case, we call the representation Excess_128, and in the second case, it is
Excess_127.
To represent a number in Excess, use the following procedure:
1. Add the magic number to the integer.
2. Change the result to binary and add 0s so that there is a total of N bits.
Example: Represent -25 in Excess_127 using an 8-bit allocation.
Computer Basics Dr. Yasser Fouda 139
Excess System
Solution: First Add 127 to -25 and get 102. This number in binary is
1100110. Add 1 bit to make it 8 bits in length. The representation is
01100110.
To interpret a number in Excess, use the following procedure:
1. Change the number to decimal.
2. Subtract the magic number from the integer.
Example: Interpret 11111110 if the representation is Excess_127.
Computer Basics Dr. Yasser Fouda 140
Excess System
Solution: First change the number to decimal. It is 254. Then subtract 127
from the number. The result is decimal 127.

Computer Basics Dr. Yasser Fouda 141


Floating-Pointing Representation
To represent a floating-point number ( a number containing an integer and a
fraction), the number is divided into two parts: the integer and the fraction.
For example, the floating point number 14.234 has an integer of 14 and a fraction
of 0.234.
To convert a floating-point number to binary, use the following procedure:
1. Convert the integer part to binary.
2. Convert the fraction part.
3. Put a decimal point between the two parts.
Computer Basics Dr. Yasser Fouda 142
Floating-Pointing Representation
Procedure for converting integer part is the same as that presented in the
beginning chapter.
To convert a fraction to binary, use repetitive multiplication. For example, to
convert 0.125 to binary, multiply the fraction by 2; the result is 0.250. The
integer part of the result (0) is extracted and becomes the leftmost binary
digit. Now multiply by 2 the fraction part (0.250) of the result to get 0.50.
Again, the integer part of the result (0) is extracted and becomes the next
binary digit. This process continue until the fraction part becomes 0 or you
reach the limit of the number of bits you can use (Figure 3.7)
Computer Basics Dr. Yasser Fouda 143
Floating-Pointing Representation

Stop when the


result is 0

0.125 0.250 0.500 1.000 0.000

0 . 0 0 1
binary

Figure 3.7 Changing fractions to binary


Computer Basics Dr. Yasser Fouda 144
Floating-Pointing Representation
Example: Transform the fraction 0.875 to binary.
Solution:
Fraction 0.875 1.750 1.50 1.0 0.0

Binary: 0. 1 1 1

Computer Basics Dr. Yasser Fouda 145


Floating-Pointing Representation
Example: Transform the fraction 0.4 to a binary of 6 bits.
Solution:

Fraction 0.4 0.8 1.6 1.2 0.4 0.8 1.6

Binary: 0. 0 1 1 0 0 1

Computer Basics Dr. Yasser Fouda 146


Floating-Pointing Representation
(Normalization)
To represent the number 71.3125 (+1000111.0101), store the sign, all of the
bits, and the position of the decimal point in memory.
Although this is possible, it makes operations on numbers difficult. You need
a standard representation for floating-point numbers.
The solution is normalization, the moving of the decimal point so that there
is only one 1 to the left of the decimal point.
1.xxxxxxxxxxxxx

Computer Basics Dr. Yasser Fouda 147


Floating-Pointing Representation
(Normalization)
To indicate the original value of the number, multiply the number by 2 ,
where e is the number of bits that the decimal points moved: positive for left
movement, negative for right movement.
A positive or negative sign is then added depending on the sign of the
original number. The following table shows examples of normalization.

Computer Basics Dr. Yasser Fouda 148


Floating-Pointing Representation
(Normalization)
Original Number Move Normalized
+1010001.11001 6 +26 1.01000111001
-111.000011 2 22 1.11000011
+0.00000111001 6 +26 1.11001
-0.001110011 3 23 1.110011

Examples of normalization

Computer Basics Dr. Yasser Fouda 149


Floating-Pointing Representation
(Normalization)
After a number is normalized, you store only three pieces of information
about the number: sign, exponent, and mantissa (the bits to the right o the
decimal point).
For example, +1000111.0101 becomes
+ 26 1.0001110101

Sign + Exponent : 6 Mantissa: 0001110101

Note that the 1 to the left of the decimal point is not stored; it is understood
Computer Basics Dr. Yasser Fouda 150
Floating-Pointing Representation
(Normalization)
The sign of the number can be stored using 1 bit (0 or 1).
The exponent (power of 2) defines the movements of the decimal point.
Note that the power can be negative or positive. Excess representation is the
method used to store the exponent. The number of bits allocated (N)
defines the range of numbers that a computer can store.
The mantissa is the binary number to the right of the decimal point. It
defines the precision of the number. The mantissa is stored as an unsigned
integer.

Computer Basics Dr. Yasser Fouda 151


Floating-Pointing Representation
(IEEE Standards)
The Institute of Electrical and Electronics Engineers (IEEE) has defined
three standards for storing floating-point numbers; two are used to store
numbers in memory (single precision and double precision).
These formats are shown in Figure 3.8. Note that the number inside the
boxes is the number of bits for each field.
We discuss the single precision format and leave the double-precision format
as an exercise.

Computer Basics Dr. Yasser Fouda 152


Floating-Pointing Representation
(IEEE Standards)
Excess_127

1 8 23 a. Single precision
sign Exponent Mantissa

Excess_1023

1 11 52 b. Double precision
sign Exponent Mantissa

Figure 3.8 IEEE Standards for floating-point representation


Computer Basics Dr. Yasser Fouda 153
Floating-Pointing Representation
(Single Precision)
The procedure for storing a normalized floating-point number in memory
using single-precision format is as follows:
1. Store the sign as 0 (positive) or 1 (negative).
2. Store the exponent (power of 2) as Excess_127.
3. Store the mantissa as an unsigned integer.
Example: Show the representation of the normalized number
+ 26 1.01000111001
Computer Basics Dr. Yasser Fouda 154
Floating-Pointing Representation
(Single Precision)
Solution:
The sign is positive, and it is represented as 0. The exponent is 6. In
Excess_127 representation, add 127 to it and get 133. In binary, this is
10000101. The mantissa is 01000111001. When you increase the bit length to
23, you get 01000111001000000000000. Note that you can not ignore the 0 on
the left because this is a fraction. The number in memory is a 32-bit number as
shown next:
sign exponent mantissa
0 10000101 01000111001000000000000
Computer Basics Dr. Yasser Fouda 155
Floating-Pointing Representation
(Single Precision)
The following table shows more examples of floating-point representation.
Number Sign Exponent Mantissa
22 1.11000011 1 10000001 11000011000000000000000
+26 1.11001 0 01111001 11001000000000000000000
23 1.110011 1 01111100 11001100000000000000000

Computer Basics Dr. Yasser Fouda 156


Floating-Pointing Representation
(Single Precision)
The following procedure interprets a 32-bit floating-point number stored in memory.
1. Use the leftmost bit as the sign.
2. Change the next 8 bits to decimal and subtract 127 from it. This is the exponent.
3. Add 1 and a decimal point to the next 23 bits. You can ignore any extra 0s at the right.
4. Move the decimal point to the correct position using the value of the exponent.
5. Change the whole part to decimal.
6. Change the fraction part to decimal.
7. Combine the whole and the fraction parts.
Computer Basics Dr. Yasser Fouda 157
Floating-Pointing Representation
(Single Precision)
Example: Interpret the following 32-bit floating-point number
10111110011001100000000000000000
Solution: The leftmost bit is the sign (-). The next 8 bits are 01111100. This
is 124 in decimal. If you subtract 127 from it, you get the exponent -3. The
next 23 bits are the mantissa. If you ignore the extra 0s, you get 110011.
After you add 1 to the left of the decimal point, the normalized number in
binary is:
23 1.110011
Computer Basics Dr. Yasser Fouda 158
Chapter (4)
Flowcharts
Computer Basics Dr. Yasser Fouda 159
Problem Solving in Computer
Computer programs are written to solve problems. There are five basic steps
to solve problem in computer:
1. Analysis the problem and develop the specifications,
2. Design a solution,
3. Code the program in a programming language,
4. Test and debugging the program,
5. Validate the program.

Computer Basics Dr. Yasser Fouda 160


Problem Solving in Computer
1. Analysis the problem and develop the specifications
The programmer must completely understand and able to write a precise statement of
the problem.
There are several questions must be asked:
i. What are the inputs to the problem? What is known, what will be given, and in what form?
ii. What are the outputs desired? What kind of report, chart, or information is required?
iii. What would be the processing steps necessary to go from the given input to the desired
output?

Computer Basics Dr. Yasser Fouda 161


Problem Solving in Computer
2. Design a solution:
Design an algorithm consists of a precise set of steps and rules, when applied to the
problem, will lead to the best solution.
The algorithm should satisfy the following criteria's:
i. Has input, perform process, and gives output,
ii. Must be clear and unambiguous,
iii. Must correctly solve the problem,
iv. Can be followed with paper and pencil,
v. Must execute in finite a number of steps.

Computer Basics Dr. Yasser Fouda 162


Problem Solving in Computer
The algorithm is usually a good way to begin to design a solution to the
problem.
However, it is not always easy to begin coding the program form the
algorithm because the program flow is not clear.
The conditional statements to skip to another step do not provide enough of
a picture of what should happen when.
So we need flowchart.
Computer Basics Dr. Yasser Fouda 163
Problem Solving in Computer
3. Code the program in a programming language
If the solution has been completely and carefully design, translation into programming
language will be relatively straightforward.
The programmer can write the code step by step exactly according to the solution.
The more complete the solution, the less time and energy it takes to write the code.

Computer Basics Dr. Yasser Fouda 164


Problem Solving in Computer
4. Test and debugging the program
Testing means that running the program, executing all the
instructions/functions, and testing the logic by entering sample data to check
the output.
Debugging is the process of finding and correcting program code mistakes:
Syntax errors,
Logic error,
Run-time errors.

Computer Basics Dr. Yasser Fouda 165


Problem Solving in Computer
5. Validate the program:
Once the program has been written in its entirety, and is working for a few test
cases, it should be validated by extensive testing.
Just because it works for one set of test inputs does not mean it will work in
every case.
A board range of test values should be applied.

Computer Basics Dr. Yasser Fouda 166


Flowchart
In order express the flow of processing in a much more lucid fashion, many
programmer use a flowchart, which is a structured picture map showing the steps
of the algorithm.
The shapes usually used for each part of the flowchart are described in the next
slides.
The flowchart consists of a set of flowchart symbols connected by arrows. Each
symbol contains information about what must be done at that point and the arrows
show the flow of execution of the algorithm, that is, they show the order in
which the instructions must be executed.

Computer Basics Dr. Yasser Fouda 167


Flowchart
Read
Record A flowchart is a symbolic
diagram that shows the
data flow and sequence
of operations in a
Decision system.
Process Process
1 2

Output
Computer Basics Dr. Yasser Fouda 168
Defining Flow Chart Symbols
There are no one definition for flowcharting symbols

There are some guidelines for commonly used symbols

Just be sure that when using these symbols you are consistent with their
meaning

Computer Basics Dr. Yasser Fouda 169


Generally Accepted Meanings
Start or Stop symbol: All points where the flow begins or ends are
represented by the terminal symbol.

oval

Computer Basics Dr. Yasser Fouda 170


Generally Accepted Meanings

The Process Symbol represents any process, function, or action


and is the most frequently used symbol in flowcharting.

Rectangle

Computer Basics Dr. Yasser Fouda 171


Generally Accepted Meanings
The Document Symbol is used to represent any type of hard copy input or
output (i.e. reports)

Computer Basics Dr. Yasser Fouda 172


Generally Accepted Meanings
Off-page Connector Symbols are used to indicate the flow chart continues
on another page. Often the page number is placed in the shape for easy
reference

Computer Basics Dr. Yasser Fouda 173


Generally Accepted Meanings
The Input/output Symbol represents data that is available for input or
resulting from processing .

Parallelogram

Computer Basics Dr. Yasser Fouda 174


Generally Accepted Meanings
The Decision Symbol is a junction where a decision must be made. A
single entry may have any number of alternative solutions, but only one can
be chosen.
This usually takes the form of a question with possible answers Yes or No
(or True or False).

Diamonds

Computer Basics Dr. Yasser Fouda 175


Generally Accepted Meanings
The Connector Symbol circle: used as a connecting point for arrows
coming from different directions.

Circle

Computer Basics Dr. Yasser Fouda 176


Example of a Flow Chart
Turn on the Log on to the
Start
Computer Network

Login
Yes
No OK?
Start
Applications
Yes
Retry
Notify System
Administrator STOP
Computer Basics No Dr. Yasser Fouda 177
Examples
Example(1): Construct a simple flowchart for two ATM machine one for
withdrawal and one for deposit.

Computer Basics Dr. Yasser Fouda 178


Examples
Solution:

Computer Basics Dr. Yasser Fouda 179


Examples
Example(2): Design one machine flowchart to determine if the transaction
is a deposit or withdrawal. If the password is not good, nothing else should
be done except an error message, and if there is not enough balance for the
withdrawal, an error message should be given.

Computer Basics Dr. Yasser Fouda 180


Examples
Solution:

Computer Basics Dr. Yasser Fouda 181


Examples
Example(3) Design a flowchart that will calculate and print the average
grade on three tests for an entire class.

Computer Basics Dr. Yasser Fouda 182


Examples
Solution:

Computer Basics Dr. Yasser Fouda 183


Examples
Example(4): Go through the entire planning process to write a flowchart
that will handle the checkout for a hotel room. This hotel charges 55$ for
one person, 60$ for two, and 65$ for three or more.

Computer Basics Dr. Yasser Fouda 184


Examples
Solution:

Computer Basics Dr. Yasser Fouda 185


Examples
Example(5): Draw a flowchart from the following algorithm to input two
numbers and print the larger.
1. Get number1 and number2
2. Compare to see which is larger
3. Print the larger number

Computer Basics Dr. Yasser Fouda 186


Examples
Solution:

Computer Basics Dr. Yasser Fouda 187


Examples
Example(6): Write a flowchart to input a series of test scores until the user
enters negative one (-1) and then print out the average score of all numbers.
You do not know how many numbers are coming into the flowchart.

Computer Basics Dr. Yasser Fouda 188


Examples
Solution:

Computer Basics Dr. Yasser Fouda 189


Chapter(5)
Qbasic Programming

Computer Basics Dr. Yasser Fouda 190


Introduction
QuickBasic (Qbasic) is a programming language developed by Microsoft for
use in the MS-DOS operating system.
It is the successor of earlier forms of BASIC (Beginners All-Purpose
Symbolic Instruction Code), a simple programming language for beginning
programmers.
Qbasic is an ideal programming language for beginners because of its
intuitive commands, simple structure and flexibility.

Computer Basics Dr. Yasser Fouda 191


Introduction
It is well-documented, and hundreds of tutorials and sample programs are
available for download on the Internet.
Of all of high level languages, QBASIC is probably the easiest to learn.
The beginning user soon discovers that small programs can be written and
quickly entered into the computer, producing interesting results.
The goal of this chapter is to learn how to write computer programs in
QBASIC Language.

Computer Basics Dr. Yasser Fouda 192


Starting Qbasic
Command Prompt

Will launch the emulator DOS


operating system? Press Alt + Enter to
display the widescreen
Computer Basics Dr. Yasser Fouda 193
Come to the directory where the
QBasic (in this example c: \ qbasic)
and start a program qbasic
C:\qbasic>qbasic

Interface QBasic interpreter.


Press the ESC key to close the
splash screen

Computer Basics Dr. Yasser Fouda 194


Computer Basics Dr. Yasser Fouda 195
QBASIC Commands

The command tells QBasic interpreter to do something.


I can write small or big letters
The new line is crossed by pressing the ENTER

Computer Basics Dr. Yasser Fouda 196


Qbasic (Variables)
The variable: is data that is stored in computer memory
(RAM). Location variables in RAM is called "address."

The following program


prints the variable X on the
screen:
PRINT X

Variable X is assigned a
free address 1000000, and
there is 0
Computer Basics Dr. Yasser Fouda 197
Qbasic (Variable)
The following program sets the X at 15, and then prints the
variable:

X = 15
PRINT X

15

Variable X is assigned the


value 15

Computer Basics Dr. Yasser Fouda 198


Qbasic (Variable)
Variables are accessed line of its name.
Variable names can have a combination of letters and numbers.
The following list represents the names of valid variables:
Y

num

VALUE

xYz

abc123
Computer Basics Dr. Yasser Fouda 199
Qbasic (Variable)
You can also use multiple variables in the program:

X = 82
The memory addresses of variables
does not have to be exactly the same Y = 101
Z = 79

PRINT X
PRINT Y
PRINT Z
82
101
79
Computer Basics Dr. Yasser Fouda 200
QBASIC language Contents
The following Contains are used by QBASIC System:
1. Letters of the alphabet(A,B,C,.Z).
2. Digit Numbers ( 0,1,2..9).
3. Characters and Symbols( +,-,*, /, =,^, ( ),<,> , >=,=< ,<>,@,#,!,?,",% ).
4. Special Words ( go to, if , read, print, input,).

Computer Basics Dr. Yasser Fouda 201


Constants in QBASIC
Constants In QBASIC division into two types:
1. Numeric Constants: there are three types of numeric constants:
I. Real: the numbers used may be written in decimal form such as(6.9,-52.76,0.095, -3269.0)
II. Integer: Whole numbers may be written without the decimal point such as (89,132,7698)
III. Exponential Form: this form requires a number fallowed by the letter E, such as
(2.8E05,0.57E-03,0.07E-9, and 29.8E7).
2. String Constants. a string consists of a sequence of characters enclosed in
double quote marks. strings usually consist of names or address or cities such as
"Computer", Mansoura".

Computer Basics Dr. Yasser Fouda 202


Variables in QBASIC
Again Variables in QBASIC division into two types:
1. Numeric Variables: the variables used to represent numbers are the letters
of the alphabet .these may followed by a digit or letter. including 0 .for
example: (A,b,c..,A0,b1,c2,Ab,ba,bv,zx).
2. String Variables: a string variables must consists of a letter of the
alphabet followed by the $ character, such as (A$,BC$,A2$,ZW$).

Computer Basics Dr. Yasser Fouda 203


Arithmetic Expressions
Expressions are used in QBASIC in order to perform calculations. in general
,an expression may consist of a single constant or a single variables, or of
arithmetic operation involving two or more constants or two or more
variables ,or of any arithmetic combination of constants and variables.
Although complicated mathematical expressions will not occur frequently,
some skill is required to translate mathematical expressions into QBASIC
expressions in performing this task ,it is necessary to know that the QBASIC
compiler using the following order of precedence in performing arithmetic
operations:
Computer Basics Dr. Yasser Fouda 204
Arithmetic Expressions
Order Symbol Remark

first )( Operations within parentheses are performed first.

second ^ Exponentiation is performed before other arithmetic


operations.
third / ,* Multiplication and division are equal in order of precedence.

forth -,+ Addition and subtraction are performed last and are equal in
order of precedence.

Computer Basics Dr. Yasser Fouda 205


Arithmetic Expressions
Example(1): Suppose( a=5, b=4 ,c=2 ,i=2,j=1) Execute the following expressions in order
of precedence:
1. a + b + c / 12
2. (a + b+ c) / 12
3. a+b/cI*j+a^2
4. a*bc/I+j+a^b+c^3
5. a * j ^ b I / c * j + 20
6. j/(I+c)
7. j / I +c
Computer Basics Dr. Yasser Fouda 206
Arithmetic Expressions
8. ( a * ( b + I ) * c ) ^ 2
9. ( j + I * ( b / c a ^ 2 ) ) + i
10. ( I + j ) / a
11. I + j / a
12. ( j * ( a ^ 2 10 ) ) / 2
13. i + j ^ 2 3 * a
14. ( ( I + j ) / 3 * a ) ^ 2 / 2

Computer Basics Dr. Yasser Fouda 207


Arithmetic Expressions
Example(2): Write following expressions in appropriate formula in QBASIC
language:
Math expression Qbasic expression
Y=a.b r.s + 2 Y=A*b-R*S+R^2
P=(x+y)(x-y)/ 2 P=(X+Y)*(X-Y)/X^2
Y= 2 -2x-4 Y=X^2-2*X-4
Y=x+ 2 /(c.b) + s.r b Y=X+r^2/(C*B)+S*R-B
T=t(1-d).y-t.(1-d)y T=t*(1-d)*Y-t*(1-d)*Y
A=(s.r-y.z+x)/5 A=(S*R-Y*Z+X)/5
Computer Basics Dr. Yasser Fouda 208
Arithmetic Expressions
Math expression Qbasic expression

A=S+R^2-S*R/(S+7)
=+ 2
( + 7)
A=P*Q/(C*B)
=

P=A+B*S/J-d
=+

( + )( + ) R=(S+B)*(A+D)/B
=

Computer Basics Dr. Yasser Fouda 209
Library Functions
Each basic interpreter has within it the capability of calculating certain
special functions, some times called library functions.
These functions are indicated by three letters naming the function, fallowed
by argument enclosed in parentheses.
The argument may be either a number, a variable, or a an expression.
In the following table library functions as might be found in most basic
interpreters.

Computer Basics Dr. Yasser Fouda 210


Library Functions
Function Description of function div Integer division. Ex:10div3=3

ABS(x) Absolute value of x ,x Cos(x) Cosine of x, x in radians

SGN(x) =(-1 or 0 or 1) for (x<0 or x=0 or x>0). Tan(x) Tangent of x, x in radians

SQR(x) Square root of x. Atn(x) Arctangent of x, x in radians

INT(x) The largest integer not exceeding x. Exp(x) The value of e power x

RND(x) Create random number value between 0 and 1. Log(x) The natural logarithm of x ,ln(x)

Sin(x) Sin of x, x in radians Mod Modulus-Rest of division. Ex:10mod3=1


Computer Basics Dr. Yasser Fouda 211
Library Functions
Example: convert following mathematical expressions in appropriate
formula QBASIC language:
Math expression Qbasic expression

S= 2 + 3 () S=sin((x*3.14/180)^2)+cos(x*3.14/180)^3

D=log log( 2 ) D=log(x)/log(10)-log(x^2)/log(10)

=

and = +4 R=abs(d-s)/n and Y=s*sqr(s)+4

1 Y=(1-exp(x))/(1+exp(x)) and M=log(x)+b*log(y)
= and M=ln(x)+bln(y)
1+

Computer Basics Dr. Yasser Fouda 212


The QBasic language statement
(The REM Statement )
The general form of the REMARK statement is:
REM[remark]
Where remark may be any remark the programmer chooses to make to describe the program,
to identify various parts of program, or even to serve as a blank line between parts of the
program.
for example:
o REM this program is named calculate
o Rem program written in 2010-03-30
o Rem input modul
o Rem output modul
Computer Basics Dr. Yasser Fouda 213
The QBasic language statement
(Print Statement )
The general form of the Print statement is:
Print expression (, or ;) expression etc.
Where expression is any number, any string of words, any variable, or any
combination of numbers and variables in an algebraic expression. commas or
semicolons must be used to separate expressions.

Computer Basics Dr. Yasser Fouda 214


The QBasic language statement
(Print Statement )
For example:
Print
Print X
Print a,b,c
Print s;t;u;r;v
Print "the sum is";s9
Print a$,B$,c$
Remark : the symbol " " use in print statement for print strings.
Computer Basics Dr. Yasser Fouda 215
The QBasic language statement
(Input Instructions )
There are three types of input instructions include:
1. Let statement.
2. Input statement.
3. Read / Data statement.

Computer Basics Dr. Yasser Fouda 216


The QBasic language statement
(Input Instructions )
The general form of the Let statement is:
Let variable = expression
Where variable may be either a numeric variable or a string variable and expression may be
either an arithmetic expression or a string expression. strings cannot be placed into numeric
variables, or vice versa.
The following are examples of let statement:
o Let X=25
o Let A$="computer"
o Let R3=5*x*(X-9)
o Let M$=N$
Computer Basics Dr. Yasser Fouda 217
The QBasic language statement
(Input Instructions )
Example: Write a program to compute the following functions: = 2
+ 2 , = 2 + , = ( + )2
Solution:
Let x=5
Let y=7
A=X^2+Y^2
B=A^2+X*Y
C=(A+B)^2
Print a,b,c

Computer Basics Dr. Yasser Fouda 218


The QBasic language statement
(Input Instructions )
Example: Write a program to compute the area and circumference of
triangle it sides x=4,y=3,z=5.
Solution
REM Program for compute triangle area and circumference
x=3:y=4:z=5
C=x+y+z
S=C/2
Ar=sqr(s*(s-x)*(s-y)*(s-z))
Print "the circumference is";C
Print "the area is";AR
Computer Basics Dr. Yasser Fouda 219
The QBasic language statement
(Input Instructions )
Example: Write a program to find horizontal component and vertical
component for force =70N slope 45 degree From x-axis.
Solution
Rem program to force components
F=70
Th=45*3.141592/180
Fx=F*cos(th)
Fy=F*sin(th)
Print Fx,Fy

Computer Basics Dr. Yasser Fouda 220


The QBasic language statement
(Input Statement)
The general form of the input statement is
Input variable [,variable]
Where variable stands for any suitable variable chosen by the programmer.
Also it is the programmers option to use more than one variable ,separating
them by commas, in order to enter additional values. When the program is
running and control comes to an input statement, the computer print question
mark (?) and pauses so that the user may enter the proper value for that
variable.
Computer Basics Dr. Yasser Fouda 221
The QBasic language statement
(Input Statement)
This opportunity to enter a value makes the program very flexible.
The following are examples of Input statement:
o Input X
o Input A, B, C
o Input A$, R$
o Input M$,E$,F$,N

Computer Basics Dr. Yasser Fouda 222


The QBasic language statement
(Input Statement)
Example: Write a program to compute the real roots of a equation 2
+ + = 0.
Solution:
REM Program for compute roots of equation
Input A,B,C
X1=(-b+ SQR(b^2-4*a*c)) /(2*a)
X2=(-b-SQR (b^2-4*a*c)) /(2*a)
Print" the first root is";X1
Print " the second root is";X2
Computer Basics Dr. Yasser Fouda 223
The QBasic language statement
(Input Statement)
Example: Write a program to compute the area and circumference of circle
its radius R.
Solution:
REM Program for compute circle area and circumference
Pi = 3.141592
Input "radius of circle is ; R
A=R^2*Pi
C= 2*pi*r
Print " the area is"; A
Print "the circumference is ; C
Computer Basics Dr. Yasser Fouda 224
The QBasic language statement
(Input Statement)
Example: Write a program to input the length ,width, and high of wall then
compute the number of bricks in wall ,if you know the brick sides is (24812cm).
Solution:
REM Program for compute bricks number
Input "the wall length ; L
Input "the wall width ; W
Input "the wall high"; H
V=L*W*H
N=V/(24*8*12)
Print "bricks number is , N
Computer Basics Dr. Yasser Fouda 225
The QBasic language statement
(Read/Data Statement)
The general form of the Read and Data statements are:
Read variable [,variable] etc.
Data datum [,datum] etc.
Where variable is any valid numeric variable or string variable and datum is any
valid number or string constant.

Computer Basics Dr. Yasser Fouda 226


The QBasic language statement
(Read/Data Statement)
following are examples of Read / Data statements:
Read X,Y,Z
Data 12.7,35,-29.75

Read A,N$,M$,T
Data 7.4, "address","telephone",66.7

Computer Basics Dr. Yasser Fouda 227


The QBasic language statement
(Read/Data Statement)
Example: Write a program to read 4 variables then compute arithmetic
mean and geometric mean.
Solution:
Read x1,x2,x3,x4
Ar= (x1+x2+x3+x4)/4
Ge=(x1*x2*x3*x4)^(1/4)
Print "arithmetic mean is";ar
Print "geometric mean is";ge
Data 50,67,80,77

Computer Basics Dr. Yasser Fouda 228


The QBasic language statement
(Read/Data Statement)
Example: Write a program to read student name and 6 degree then compute
and print the average of student degree.
Solution:
Read A$,a,b,,c,d,e,f
S=a+b+c+d+e+f
AV=S/6
Print A$,AV
Data Ali,50,65,87,97,90,70

Computer Basics Dr. Yasser Fouda 229


The QBasic language statement
(The Restore Statement )
The general form of the restore statement is
Restore
Whenever control executes the restore statement, the system restores the data
block pointer to the first item of data. then the next read statement starts the
process of reading the data all over again from the very first data statement.

Computer Basics Dr. Yasser Fouda 230


The QBasic language statement
(The Restore Statement )
Example:
Read a,b,c
Restore
Read x,y,z
Data 5,7,9

The x,y,z have the values5,7,9


Computer Basics Dr. Yasser Fouda 231
Control Statements
Control statements include
1. Go To statement
2. If Then statement
3. If Go To statement
4. On Go To statement

Computer Basics Dr. Yasser Fouda 232


Control Statements
(The Go To Statement )
The general form of the Go To statement is:
Go To Line number
Where line number represents the next line number to which control will go instead of
the following line number. the following are examples of the go to statement:
Go to 10,
Go to 5.
This statement is sometimes called the unconditional go to statement.

Computer Basics Dr. Yasser Fouda 233


Control Statements
(The Go To Statement )
Example: Write a program to compute the average of 6 subjects for number
of students.
Solution
10 Input N$,S1,S2,S3,S4,S5,S6
AV=(S1+S2+S3+S4+S5+S6)/6
Print N$, AV
Go To 10

Computer Basics Dr. Yasser Fouda 234


Control Statements
(The IF THEN Statement )
The general form of the If then Statement is:
IF Condition THEN Line Number
Where condition has the form :
Arithmetic expression relation Arithmetic
expression Or String expression relation
String expression And relation represents one of the symbols from the
following table:

Computer Basics Dr. Yasser Fouda 235


Control Statements
(The IF THEN Statement )
Symbol Example
= A=B
< A<B
> A>B
<= A<=B
=> A>=B
<> A<>B

Computer Basics Dr. Yasser Fouda 236


Control Statements
(The IF THEN Statement )
Example: Write a program to compute the Summation (S) of values from 1
to 100 where: S = 1+2+3+100.
Solution
S=0 :
I=1
5 S=S+I
I=I+1
If I <= 100 then 5
Print S

Computer Basics Dr. Yasser Fouda 237


Control Statements
(The IF THEN Statement )
Example: Write a program to compute the summation for even numbers
from 0 to N.
Solution
Input N
S=0 : I=0
5 S=S+I
I=I+2
If I < N then 5
Print S

Computer Basics Dr. Yasser Fouda 238


Control Statements
(The IF THEN Statement )
Example: Write a program to compute Y value where:
Y=A+B if A<= B
Y=A*B if A > B
Solution
Input A,B
If A> B then Y= A*B
If A<= B then Y=A+B
Print Y
Computer Basics Dr. Yasser Fouda 239
Control Statements
(The IF THEN Statement )
Example Write a program to compute Summation of X values from list of 10
different numbers.
Solution
I=1
5 Read X
S=S+X
I=I+1
If I<= 10 then 5
Print "sum=";S
Data 5,12,15,18,20,30,7,5,57,75
Computer Basics Dr. Yasser Fouda 240
Control Statements
(The IF THEN Statement )
Example: Write a program to compute S value where : S=1-1/3+1/5-1/7+.1/n.
Solution:
Input N
I=1
J=1
5 T=1/I
S=S+T*j
I=I+2
J=-J
If I <=N then 5
Print S
Computer Basics Dr. Yasser Fouda 241
Control Statements
(The IF THEN Statement )
Example: Write a program to compute the summation of following series
(use 20 terms) S=-3/5+7/10-11/15+15/20-.
Solution :
N=1: K=-1: i=3: j=5
5 T=i/j
S = S + T*k
N=n+1: k=-k: i=i+4: j=j+5
If n<=20 then 5
Print S

Computer Basics Dr. Yasser Fouda 242


Control Statements
(The IF THEN Statement )
Example: Write a program to input any number then determine if the
number is odd or even.
Solution:

Input X OR Input X
If (x/2)= int(x/2) then print "x is even" If x mod 2 =0 then print "x is even"
If (x/2) < > int(x/2) then Print "x is odd" If x mod 2 < > 0 then print "x is odd"

Computer Basics Dr. Yasser Fouda 243


Control Statements
(The IF THEN Statement )
Example: Write a program input any number then determine if the number
is integer or not.
Solution:
Input X
If x= int(x) then print "x is integer
If x<> int(x) then Print "x is not integer"

Computer Basics Dr. Yasser Fouda 244


Control Statements
(The IF THEN Statement )
Example Write a program ask about point axis and circle radius ,it is center the
original point, then compute if the point is inside, or on or outside the circle.
Solution
Input x,y ,r,m1,m2
D= sqr((x-m1)^2+(y-m2)^2)
If d<r then print "Point inside the circle"
If d>r then print "Point outside the circle"
If d=r then print "Point on the circle"

Computer Basics Dr. Yasser Fouda 245


Control Statements
(The IF THEN Statement )
Example: Write a program that read 5 forces in point with angle to x- axis, then compute
the resultant of these forces in x and y axis if you know that = 2 + 2
I =1
Solution: 10 Read F,th
Rx=F * Cos(th*3.14/180)
Ry=F * Sin(th*3.14/180)
S1= S1 +Rx
S2= S2 +Ry
I=I+1
If I<=5 then 10
R= SQR(S1^2+S2 ^2)
Print S1,S2,R
Data 50,30,70,45,60,90,80,180,50,270
Computer Basics Dr. Yasser Fouda 246
Control Statements
(The IF THEN Statement )
2 3
Example Write a program to compute A value: = 10
=1 + 3 ()

Solution
X=1
10 S=S+abs((X^2-3*X)/(X+cos(x*3.14/180)^3)))
X=X+1
If X<=10 then 10
A=S^2
Print A

Computer Basics Dr. Yasser Fouda 247


The IF Go To Statement
The general form of the If go to Statement is:
IF Condition Go To Line Number
Example: Write a program to compute the average of 50 students have 6 subjects.
Solution REM this program for compute the average
I=0
10 Input N$,S1,S2,S3,S4,S5,S6
AV=(S1+S2+S3+S4+S5+S6)/6
Print N$,AV
I=I+1
If I < =50 go to 10
End
Computer Basics Dr. Yasser Fouda 248
The IF Go To Statement
Example Write a program to compute the ( N! ) value. where
N!=1*2*3.N
Solution REM this program for compute the factorial
Input N
I=1
F=1
5 F=F*I
I=I+1
If I<=n go to 5
Print F
Computer Basics Dr. Yasser Fouda 249
The IF Go To Statement
Example Write a program to compute the sum and count of positive and negative numbers from list
of (30) number.
Solution K=0: j=0
5 Read A
If A > 0 go to 10
SN=SN+A
J=j+1
Go to 20
10 SP=SP+A
K=k+1
20 If j+k < 30 go to 5
Print "Positive numbers" ;SP,k
Print "negative number" ;SN, j
Computer Basics Data 30,60,-70,,9,-5,
Dr. Yasser Fouda 250
The IF Go To Statement
Example: Write a program to compute the sum and count of odd and even
and summation of all values from list of (N) numbers.
Solution:

Computer Basics Dr. Yasser Fouda 251


The IF Go To Statement
J=0 : k=0 : SE=0 : SO=0 : SX=0
Read N
5 Read X
Y=X/2
If Y=INT(Y) go to 10 10 SE = SE + X
SO = SO + X K =k +1
J=j+1 20 SX=SX+X
Go to 20 If k+j < N go to 5
Print " sum of odd number is ";SO, "count of odd number is"; j
Print " sum of even number is ";SE, "count of even number is"; k
Print "sum of all numbers is "; SX
Data 10, 12,6,9,7,8,5,56,77,3,99

Computer Basics Dr. Yasser Fouda 252


The IF Go To Statement
Example Write a program to find y value from the equation = 2 4
+ 6 for X value from 0-10.
Solution
X=0
5 Y= X^2-4*X+6
Print "X=";X,"Y=";Y
X=X+1
If X<=10 go to 5

Computer Basics Dr. Yasser Fouda 253


The IF Go To Statement
Example: Write a program to compute reaction of steel cable (R) to
temperature (T) that equal (293,313,333,353) using the following
equation: = 8.85 108 1 + 6.5 103 ( 237)
Solution
T=293
R=8.85E-8*(1+6.5E-3*(T-273))
10 PRINT R
T=T+20
If T<= 353 go to 10

Computer Basics Dr. Yasser Fouda 254


Compound IF then
The general form of compound if ...then is:
Simple relation (and, or) simple relation
The most logical operators are in following table:
Logical operators Remark
X1 and X2 True if x1 and x2 are true otherwise false.
X1 or X2 True if either x1 or x2 or both true otherwise false.

Computer Basics Dr. Yasser Fouda 255


Compound IF then
Example: Write a program to compute the Y value where:
+
=
2
2 + 3 = 1 = 3
= 2 + 3 + 5 > 2 > 4
3 + 2 2 +

Execute the program to N from A,B values.


Computer Basics Dr. Yasser Fouda 256
Compound IF then
Solution:
Print " A B X Y": print "------------------"
Read N
I=1
5 Read A,B Y=X^3+2*X^2+X
X=(A+B)/2 Go to 30
If A=1 or B=3 go to 10 10 Y= X^2 +X-3
If A>2 and B>4 go to 20 Go to 30
20 Y=X^2+3*X+5
print A;" ";B;" ";X;" ";Y
30 I=I+1
If I<=N go to 5
Data 5,1,3,2,4,3,5,1,2,5,4
Computer Basics Dr. Yasser Fouda 257
Compound IF then
Example: Write a program to input 3 numbers then find the maximum one.
Solution
Input a,b,c
Max=a
If b>a and b>c then max=b
If c>a and c>b then max =c
Print "the maximum is";max

Computer Basics Dr. Yasser Fouda 258


Compound IF then
Example: Write a program ask about triangle sides ,and determine if it is right
triangle, and compute it is area and circumference.
Solution
Input x,y,z
If x+y>z and y+z>x and x+z>y then Print "right triangle"
C=x+y+z
S=C/2
Ar=sqr(s*(s-x)*(s-y)*(s-z))
Print "the circumference is,C
Print "the area is,ar

Computer Basics Dr. Yasser Fouda 259


The ON Go To Statement
The general form of the on go to statement is
ON expression Go To Line number[, line number] etc.
Where expression may be a single variable or any combination of variables and
numbers. this expression must be evaluated, must be a positive number, and then must
be truncated to a positive integer. if the integer is 1,then control goes to the first line
number following the go to, if the integer is 2 , then control goes to the second line
number following the go to, and so on. an expression less than 1 or greater than the
number of line numbers given constitutes an error that will stop the program.

Computer Basics Dr. Yasser Fouda 260


The ON Go To Statement
Example: Write a program to find Z value where :
2 =1
2 3 3 =2
=
2 =3
< 1 > 3

Computer Basics Dr. Yasser Fouda 261


The ON Go To Statement
Solution: Read K
On K go to 10,20,30
Z=k
Go to 40
10 Z=2
Go to 40
20 Z=2*k^3-3
Go to 40
30 Z=-2
40 Print K,Z
Data 2
Computer Basics Dr. Yasser Fouda 262
The ON Go To Statement
Example: Write a program to compute Q value where:
2 + 1 = 1
= 2 + = 2
log 2 + 3 = 3

Computer Basics Dr. Yasser Fouda 263


The ON Go To Statement
Solution:
Read X,Y
Read K
On K go to 10 ,20,30
10 Q=ABS(X^2+1)
Go to 40
20 Q=SQR(ABS(X^2+Y))
Go to 40
30 Q=log (Y^2+3*X)
40 Print Q
Data 5,10,3
Computer Basics Dr. Yasser Fouda 264
The For Next Statements
The general form of the for and next statements are:
For loop variable = exp1 to exp2 [step exp3]
Next loop variable
Where loop variable is a numeric variable, and must be the same variable in both of
the statements. The expressions denoted by exp1,exp2,and exp3 may consist of
numeric constants or variables , or combinations of numeric variables and constants
with one condition. that is the value of exp3, when added repeatedly to exp1,must
finally go beyond exp2.if the step has been omitted ,then exp3 is set equal to 1. The
purpose for the companion for and next statements is to guide control through the set
of statements located between the for and next statements.
Computer Basics Dr. Yasser Fouda 265
The For Next Statements
Example: Write a program to print the summation and multiplication of the
numbers from 1 to 50.
Solution
S=0:P=1
For n=1 to 50
S=S+n
P=P*n
Next n
Print "the summation is,S
Print the multiplication is,P
Computer Basics Dr. Yasser Fouda 266
The For Next Statements
Example Write a program to compute P value where:
= 12 + 32 +52 ++252 .
Solution
P=0
For i=1 to 25 step 2
P=P+i^2
Next i
Print P

Computer Basics Dr. Yasser Fouda 267


The For Next Statements
Example: Write a program to find the maximum value from list of 10
numbers.
Solution REM Program for find maximum value
READ x
MX=X
For i= 2 to 10
Read X
If X>MX then MX=X
Next i
Print "the maximum is,MX
Computer Basics Data 20,3,27,98,70,54,1,60,34,90
Dr. Yasser Fouda 268
The For Next Statements
Example: Write a program to find the summation of 10 different numbers.
Solution
REM Program for find the summation
For i= 1 to 10
Read X
S=S+X
Next i
Print "summation is"; S
Data 5,8,90,67,45,3,2,88,6,1

Computer Basics Dr. Yasser Fouda 269


The For Next Statements
Example: Write a program to compute the ( N! ) value. where
N!=1*2*3.N
Solution
REM Program for find factorial value
Input N
F=1
For i= 1 to N
F=f*i
Next i
Print "the factorial is ";f

Computer Basics Dr. Yasser Fouda 270


The For Next Statements
Example: Write a program to compute the summation of the series
30
=1 +1.

Solution
For x=1 to 30
S=S+x/(x+1)
Next x
Print S

Computer Basics Dr. Yasser Fouda 271


The For Next Statements
Example: Write a program to compute the mean and standard deviation SD
2
=1 =1 /

where: = ( =1 )/ ,and =
(1)

Solution:

Computer Basics Dr. Yasser Fouda 272


The For Next Statements
Solution Input N
For i=1 to n
Input x
S=S+x
SS=SS+x^2
Next i
Me = S/n
V=(SS-S^2/n)/(n-1)
SD=SQR(V)
Print "mean=";me
Print Standard Deviation=';SD
Computer Basics Dr. Yasser Fouda 273
The For Next Statements
Example If N$ represent student name and X represent student degree
,write a program to print names and averages of the successful students only,
if you know the successful degree 50% and the number of subject (5) and
the number of students(150).
Solution

Computer Basics Dr. Yasser Fouda 274


The For Next Statements
Solution:
For i= 1 to 150
Input "name";N$
S=0
For K=1 to 5
Input X
If X<50 go to 10
S=S+X
Next K
M=S/5
Print "mean=';m
10 Next i
Computer Basics Dr. Yasser Fouda 275
The For Next Statements
Example Write a program to compute the summation of the series S=1 +x+x^2/2!+x^3/3!.
Solution
Read x
For i=0 to 3
F=1
For j=1 to i
F=f*j
Next j
S=S+x^i/f
Next i
Print S
Data 2
Computer Basics Dr. Yasser Fouda 276
The For Next Statements
Example Write a program to compute the summation of following series(use 20 term)
S=1+1/2!+1/3!+1/4!+1/5!+.
Solution N=1 : k=1
5 f=1
For i=1 to k
F=f*i
Next i
S=S+1/f
K=k+1
N=n+1
If n<=20 then 5
Computer Basics Print S Dr. Yasser Fouda 277
The For Next Statements
Example Write a program read x value then compute S value from the
following series:
2 3 10
1 1 1 1 1 1 1
= + + + +
2 3 10
Repeat the procedure to 4 of x values.

Computer Basics Dr. Yasser Fouda 278


The For Next Statements
Solution: I=1
10 Read X
N= (x-1)/x
For j=1 to 10
S=S+n^j
Next j
Print X,S
I=I+1
If I<=4 then 10
End
Data 4
Computer Basics Dr. Yasser Fouda 279

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