Computer Science: References
Computer Science: References
Computer Science
References:
4- C++ By Dissection
By Ira Pohl
2010-2011
1
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
Computer
Hardware Software
اﻟﻜﯿﺎن اﻟﺼﻠﺪ اﻟﻜﯿﺎن اﻟﻨﺎﻋﻢ
:وﯾﻄﻠﻖ ﻋﻠﻰ ﺟﻤﯿﻊ ﻣﻜﻮﻧﺎت اﻟﺤﺎﺳﺒﺔ اﻻﻟﻜﺘﺮوﻧﯿﺔ وﻣﻠﺤﻘﺎﺗﮭﺎ وﺗﺸﻤﻞ ﻋﻠﻰ ﺳﺒﯿﻞ اﻟﻤﺜﺎل
Screen, Keyboard, CD Drive, Printer, Mouse…,etc.
ﻧﻘﻞ اﻟﻤﻌﻄﯿﺎت ﺑﯿﻦ وﺣﺪة اﻟﻤﻌﺎﻟﺠﺔ ووﺣﺪة اﻟﺨﺰن وﻣﻨﺎﻓﺬ اﻟﺪﺧﻞ واﻟﺨﺮج
*Controls overall system operations
Store the binary data is take different manners in the compact disk (CD), magnetic tape
(floppy disk), flash disk….etc.
Memory in general
Memory
Address lines Size Data lines
A memory address is a particular location of a larger memory array. Usually one memory
address contains one word of data. A word is one packet of information for the computer
and is usually composed of many bits. Computers exist that use 1-bit words, 8-bit words,
16-bit words, 32-bit words, and 64-bit words. Handling computer data in 8-bit words is so
common that the 8-bit word has its own name, the byte.
Capacity (memory size) is an important aspect of system performance; it is a useful and
convenient way to describe the size of memory. At the individual part level, a computer’s
memory may be described as containing 65,536 bits; or, alternately, it may be called an
“8K × 8” memory. Most computer memory sizes are described as a specific number of
words. It is assumed that the word size is geared to the particular computer that is used
with the memory. Computer memory sizes are given in Kilo, Mega, or Gega The exact
size of a 1K block is 1,024, or 210 . and 1M blocks is 1048576 or 220 , and 1G blocks is
1073741824 or 230 .
4
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
d- Address lines= =
Input/Output Ports
In parallel port the computer exchanges information using a parallel configuration, all bits
of information represented by a byte or word are input or output simultaneously.
In serial port the computer exchanges information using a serial configuration, all bits of
information are input or output one at a time.
5
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
Example: Sketch a figure to show the data transfer in parallel and serial ports unit for
number (29)10 which is equal to (11101)2 .
Ans:
1 1 1
1 1 1 0 1
1 1 1
Parallel Serial
1 I/O 1 1 I/O
port port
0 0 0
1 1 1
To transfer information internally between units, the computers use buses. Buses are
groups of conductors that connect the units to one another. A bus is a parallel data
communication path over which information is transferred a byte or word at a time.
Therefore computer has uses three main types of buses. These are:
1- Data Bus
The bidirectional data bus, handles the transfer of all data and instructions between the
units of the computer. The bidirectional data bus can only transmit in one direction at a
time.
2- Address Bus
The unidirectional address bus consists of all the signals necessary to define any of the
possible memory address locations within the computer.
3- Control Bus
The unidirectional control bus is used by the CPU to direct and monitor the actions of the
other functional areas of the computer.
Programming language: is a set of rules, symbols, and special words used to construct a
program.
4- M=(q+w+e+t+y)/5
5- If (M 0) print M, otherwise finish.
6- End.
Flow Chart:
Flow chart is a set of symbols to identify both the operation required in the solution and
the sequence in which they are performed.
The commonly symbols are consisting of:
Connection to
Connection another flow chart
Decision
The flow chart with including algorithm for example above become as:
Begin
Input: q, w, e, t, y
Outut: M
If No
Yes
Print: M
End
8
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
Question: Sketch flow chart including the algorithm to investigate the shape below.
Integer print
positive
float print
Input x
integer print
negative
float print
a b c d e f g h i j k l m
n o p q r s t u v w x y z
8 P Q R S T U V W X Y
9 Z [ \ ] ^ _ ‘ a b c
10 d e f g h i j k l M
11 n o p q r s t u v w
12 x y z { | } ~ del
Keywords:
In C++ are explicitly reserved words that have a strict meaning and may not be used in any
other way. They include words used for type declarations, such as int, char, and float;
words used for statement syntax, such as do, for, and if; and words used for access control,
such as public, protected, and private.
Identifier Names:
The names that are used to reference variables, functions, labels, and various other user-
defined objects are called identifiers. Identifiers can vary from one to several characters in
length. An identifier in C++ is a sequence of letters, digits, and underscores. An identifier
cannot begin with a digit. Uppercase and lowercase letters are treated as distinct. It is good
practice to choose meaningful names as identifiers. One- or two-letter identifiers can be
used when it is obvious what the name is being used for. Avoid using identifiers that are
distinguished only by case differences. In principle, identifiers can be arbitrarily long, but
many systems distinguish only up to the first 31 characters.
Question: are there the same identifiers :Count, COUNT, and count?
statement sequence
}
The global declarations is refer to library which is the file containing the standard
functions that can be used by your program. These functions include all I/O operations as
well as other useful routines.
The keyword main with the parentheses () represented beginning of program execute.
The brace symbols {} is the block of main command to be execute.
The statements sequence is the instructions used to perform in the program.
Example: write a program in C++ to print on the screen of computer the integer number 5.
Ans:
First program is: Second program is: Third program is:
#include<iostream.h> #include<iostream.h> #include<iostream.h>
main() main() main()
{ { {
int x=5; int x=5; cout<<5;
cout<<x; cout<<”x= ”<<x; }
} }
Data Types:
There are five atomic data types in the C++: character, integer, floating-point, double
floating-point, and valueless. Values of type char are used to hold ASCII characters or any
8-bit quantity. Variables of type int are used to hold integer quantities. Variables of type
float and double hold real numbers. (Real numbers have both an integer and a fractional
component.) The void type has three uses. The first is to declare explicitly a function as
returning no value; the second is to declare explicitly a function as having no parameters;
the third is to create generic pointers.
12
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
Type Modifiers:
Except type void, the basic data types may have various modifiers preceding them. A type
modifier is used to alter the meaning of the base type to fit the needs of various situations
more precisely. The list of modifiers is shown here:
signed
unsigned
long
short
The modifiers signed, unsigned, long, and short can be applied to integer base types. The
character base type can be modified by unsigned and signed. You can also apply long to
double. Access Modifiers that is used to control the ways in which variables may be
accessed or modified. This modifier is called const variable of type const may not be
changed during execution by your program.
Data Type Keyword Byte width Range modified Range
Character char 1 0~255 signed -127~127
unsigned 0~255
Short Integer short int 2 0~(216-1) signed -215~(215-1)
unsigned 0~(216-1)
Integer int 4 0~(232-1) signed -231~(231-1)
Long integer long int unsigned 0~(232-1)
Float float 4 0~(232-1) 6 digits of precision
1.18E–38 to 3.40E+38
Double double 8 0~(264-1) 10 digits of precision
2.23E–308 to 1.79E+308
Long double long double 10 0~(280-1) 10 digits of precision
3.37E–4932 to 1.18E+4932
13
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
Declaration of Variables:
As you probably know, a variable is a named location in memory that is used to hold a
value that can be modified by the program. All variables must be declared before they are
used. The general form of a declaration is shown here:
type variable list;
Variable list may consist of one or more identifier names with comma separators. Some
declarations are shown here:
int i, j, l;
char h = 'a';
short int si;
unsigned int ahmed;
double balance, loss;
Question: write the instruction to declaring about:
i- integer x and y
ii- floating point -1.233432345
iii- character 5,h
There are three basic places where variables can be declared: inside functions, in the
definition of function parameters, or outside all functions. These variables are called local
variables, formal parameters, and global variables, respectively.
Constants:
Constants refer to fixed values that may not be altered by the program. They can be of any
data type as:
const float pi=3.141;
It is sometimes easier to use a number system based on 8 or 16 instead of 10. The number
system based on 8 is called octal and uses the digits 0 through 7. The base 16 number
system is called hexadecimal and uses the digits 0 through 9 plus the letters A through F,
14
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
which stand for 10, 11, 12, 13, 14, and 15. Because of the frequency with which these two
number systems are used, C++ allows you to specify integer constants in hexadecimal or
octal instead of decimal if you prefer. A hexadecimal constant must begin with a 0x (a
zero followed by an x) or 0X, followed by the constant in hexadecimal form. An octal
constant begins with a zero. Here are two examples:
int hex = 0x80; // 128 in decimal
int oct = 012; // 10 in decimal
Example: what is the result of program that appearing on the screen of computer.
include<iostream.h>
main()
{
int d=0x12, s=0x11;
cout<<(d+s);
}
Ans: The result appearing on the screen is 35
Question: what is the result if:
I: int d=12, s=0x11;
II: int d=012, s=011;
III: int d=0x12, s=011;
Example: what is the result of program that appearing on the screen of computer.
include<iostream.h>
main()
{
char d=’2’, f=’8’, w;
w=d+f;
cout<<w;
}
The result is j
2 in ASCII code 50
8 in ASCII code 56
j in ASCII code 106
Question: what is the result if:
char d=’2’, f=’8’; int w;
15
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
C++ Operators:
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. There are four general classes of operators in C++: arithmetic, relational
and logical, and bitwise. In addition, there are some special operators for particular tasks
1- . Arithmetic Operators:
The table lists C++ arithmetic operators.
Operator Action
– Subtraction, also unary minus
+ Addition
* Multiplication
/ Division
% Modulus
–– Decrement
++ Increment
Precedence Operators
Highest ++ – – – (unary minus)
* / %
Lowest + –
The operators +, –, *, and / all work the same way in C++ as they do in most other
computer languages. They can be applied to almost any built-in data type allowed by C++.
When / is applied to an integer or character, any remainder is truncated; for example, 10 / 3
equals 3 in integer division. The modulus division operator (%) also works in C++ the way
it does in other languages. Remember that the modulus division operation yields the
remainder of an integer division. However, as such, % cannot be used on type float or
double. The reason the last line prints a 0 and 1 is because 1 / 2 in integer division is 0
with a remainder of 1. 1 % 2 yields the remainder 1. The unary minus, in effect, multiplies
its single operand by –1. That is, any number preceded by a minus sign switches its
16
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
sign.C++ allows two very useful operators not generally found in other computer
languages. These are the increment and decrement operators, ++ and ––. The operation ++
adds 1 to its operand, and –– subtracts 1.Therefore, the following are equivalent
operations:
x = x + 1;
is the same as
++x; Or x++;
Also,
x = x - 1;
is the same as
--x; Or x--
However, there is a difference when they are used in an expression. When an increment or
decrement operator precedes its operand, C++ performs the increment or decrement
operation prior to obtaining the operand’s value.
Example: writ a program in C|++ language to test the operation of arithmetic operators
with printing the result appearing on the screen of computer.
Ans:
#include<iostream.h>
//test arithmetic operators:
main()
{
int a=13, b=5;
cout<<a<<"+"<<b<<"="<<(a+b)<<endl;
cout<<"-b="<<(-b)<<endl;
cout<<a<<"*"<<b<<"="<<(a*b)<<endl;
cout<<a<<"/"<<b<<"="<<(a/b)<<endl;
cout<<a<<"%"<<b<<"="<<(a%b)<<endl;
cout<<"a++="<<a++<<endl;
cout<<"++a="<<++a<<endl;
cout<<"--a="<<--a<<endl;
cout<<"a--="<<a--<<endl;
}
17
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
i: 37/(5%3)
ii: m-8-n
iii: m%n++
iv: ++m-n—
v : m*=n++
vi: m+=--n
Type Conversion:
Let data types as: Character char; Integer int; Floating point float; Double float point
double.
double
double
18
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
Example: writ a program in C|++ language to test the operation of Relational and Logical
Operators with printing the result appearing on the screen of computer.
Ans:
#include<iostream.h>
// Program to test Relational and Logical Operators
main()
{
int A=57,B=57;
char C='9';
cout<<"(A<57)="<<(A<57)<<endl;
cout<<"(A<90)="<<(A<90)<<endl;
cout<<"(A<30)="<<(A<30)<<endl;
cout<<"(A<=57)="<<(A<=57)<<endl;
cout<<"(A>B)="<<(A>B)<<endl;
cout<<"(A>=B)="<<(A>=B)<<endl;
cout<<"(A==B)="<<(A==B)<<endl;
cout<<"(A!=B)="<<(A!=B)<<endl;
cout<<"(A==C)="<<(A==C)<<endl;
cout<<"(A&&0)="<<(A&&0)<<endl;
cout<<"(A||20)="<<(A||20)<<endl;
cout<<"(A=!C)="<<(A=!C)<<endl;
cout<<"A="<<A;
}
(A<57)=0 (A<90)=1 (A<30)=0 (A<=57)=1
(A>B)=0 (A>=B)=1 (A==B)=1 (A!=B)=0
(A==C)=1 (A&&0)=0 (A||20)=1 (A=!C)=0
A=0
i- -5 x 15 ii- -5 x 15
3- Bitwise Operators:
Bitwise operations are the testing, setting, or shifting of the actual bits in a byte or word,
which correspond to the standard char and int data types and variants. Bitwise operators
cannot be used on type float, double, long double, void, or other more complex types.
The bitwise AND, OR, and NOT (one’s complement) are governed by the same truth table
as were their logical equivalents except that they work on a bit-by-bit level.
Operator Action
& AND
| OR
^ Exclusive OR (XOR)
~ One’s Complement
>> Shift Right
<< Shift Left
Precedence Operators
Highest ~
>> <<
&
^
Lowest |
Example: writ a program in C|++ language to test the operation of Bitwise Operators with
printing the result appearing on the screen of computer.
Ans:
#include<iostream.h>// Program to test Bitwise Operators
21
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
main()
{
int Ali=0x5c,Basim=0x3B;
cout<<"Ali ="<<Ali<<", Basim ="<<Basim<<endl;
cout<<" Ali & Basim = "<<(Ali&Basim)<<endl;
cout<<" Ali | Basim = "<<(Ali|Basim)<<endl;
cout<<" Ali ^ Basim = "<<(Ali^Basim)<<endl;
cout<<" Ali >> 2 = "<<(Ali>>2)<<endl;
cout<<" Basim >> 4 = "<<(Basim>>4)<<endl;
cout<<" Ali << 3 = "<<(Ali<<3)<<endl;
cout<<" Basim << 1 = "<<(Basim<<1)<<endl;
cout<<" ~Ali = "<<(~Ali)<<endl;
cout<<" ~Basim ="<<(~Basim)<<endl;
}
The result appearing on the screen of computer is:
Basim =59 ,Ali =92 Basim >> 4 = 3
Ali & Basim = 24 Ali << 3 = 736
Ali | Basim = 127 BAsim << 1 = 118
Ali ^ Basim = 103 ~Ali = -93
Ali >> 2 = 23 ~Basim =-60
4- Remainder Operators:
The Comma Operator: The comma operator strings together several expressions. The left
side of the comma operator is always evaluated as void. This means that the expression.
For example,
x = (y=3, y+1);
First assigns y the value 3 and then assigns x the value of 4.
Question: what is the result of x if:
Int x, y = 10;
x = (y=y-5, 25/y);
22
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
Assignment Operator: In C++, the assignment operator is the single equal sign. When
assigning a common value to several values, you can “string together” several
assignments. For example,
int a =10, b =10, c = 10; can be assigns as: int a=b=c=10;
Another “shorthand” take the general form:
Variable operation = expression;
For example, these two assignments
x = x+10;
y = y/z;
can be recoded as shown:
x += 10;
y /= z;
Question: using assignments for the expressions:
i- x=-x+3 ii- y=3/y iii- t=2(t+2)
The Cast Operator: A cast is a special operator that forces one data type to be converted
into another. The C++ support the form of cast shown here:
(type) expression;
where type is the desired data type.
For example, the following cast causes the outcome of the specified integer division to be
of type double:
double d;
d = (double) 10/3;
The result is 3.3333333333
Question: what is the result of:
float d;
d=(double) 10/3;
23
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
The I/O Operators: In C++, the << and the >> are overloaded to perform I/O operations.
When used in an expression in which the left operand is a stream, the >> is an input
operator and the << is an output operator. In the language of C++, the >> is called an
extractor because it extracts data from the input stream. The << is called an inserter
because it inserts data into the output stream. The general forms of these operators are
shown here:
cin>> variable 1>> variable 2>>……>> variable N;
cout<< variable 1<< variable 2<<……<< variable N;
For example, the following fragment inputs two integer variables:
int i, j;
cin >> i >> j;
The following statement displays “This is a test 10 20”:
cout << "This is a test " << 10 << << ' ' << 4*5;
Example: writ a result of the programming that appearing on the screen of computer:
#include<iostream.h>
main()
{
int I=10, j=15, k=27;
cout<<I<<’\t’<<j<<’\t’<<k<<endl;
cout<<oct<<I<<’\t’<<j<<’\t’<<k<<endl;
cout<<hex<<I<<’\t’<<j<<’\t’<<k<<endl;
cout<<”Enter 3 integer, e,g. 11 11a 12”<endl;
cin>>I>>hex>>j>>k;
cout<<dec<<I<<’\t’<<j<<’\t’<<k<<endl;
}
The result appearing on the screen of computer is:
10 15 27
12 17 33
a f 1b
Enter 3 integer, e,g 11 11a 12
11 282 c
The & and * Pointer Operators: A pointer is the memory address of a variable. A pointer
variable is a variable that is specifically declared to hold a pointer to a value of its
specified type.
24
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
We can connect the value of data with the location in form: Let int a=33;
Location
name Value
type
Therefore we can represent the declaring of int a=33 in location for example 0x3fffd as:
0x3fffd
a 33
int
The first pointer operator is &. It is a unary operator that returns the memory address of its
operand. Remember that a unary operator only requires one operand. For example,
m = &count;
places into m the memory address of the variable count.
The second operator, *, is the complement of the &. It is a unary operator that returns the
value of the object located at the address that follows. For example, if m contains the
memory address of the variable count, then
q = *m;
places the value of count into q.
In another meaning: n=*&n and p=&*p
Example: write a program in C++ language to find the location of int a=33 in your
computer, then store it in new location.
Ans:
#include<iostream.h>
main()
{
int a=33;
int *p=&a;
cout<<"a="<<a<<" ,&a="<<&a<<endl;
cout<<"p="<<p<<" ,&p="<<&p<<endl;
}
The result is:
a=33 ,&a=0x0012ff88
25
Department of Electrical &Electronic Engineering-First Year-2010-2011 – By:Dr.Eyad I. Abbas
p=0x0012ff88 ,&p=0x0012ff84
Question: Rewrite the program to repeat the value of a from address pointer.