Está en la página 1de 32

Computer Data Structures

rd

3 Year Communications
2

nd

Term

-C++ Revision
-Sheet (Revision) Solution
-Assignment (0) Solution


1- Revision on C++ Programming.
2- Software Design Process.
3- Advanced C++ Issues: Pointers and Classes.
4- Data Structures
Lists.
Stacks.
Queues.
Tables & Hashing.
Binary Trees *
Graphs*
5- Algorithms [Analysis & Design]
Sort Algorithms.
Search Algorithms.
Graphs Binary Trees *

Studying the efficient methods of representing and storing the given data from
the user in your program (efficiency here means program size, execution time,
Data Manipulation flexibility...etc).

Example :
To represent student grades in your C++ program there are many ways lets
take the following two ways:
1-Using the Arrays


2-Using the pointers

Course Grading Policy:


Final Exam:: 70 Marks
Two Midterms each of 15 marks
Or one midterm & Term Project each of 15 marks

Outline:Basic Data Types


int

Integral numbers

Real numbers
float
double
char

ASCII Charcters

w_char

UNICODE Charcters

bool

Boolean

void
Operators
Compound Data Types
Arrays [Single Dimensions Multi Dimensions ]
Character Strings
Structures
Classes
Defined Data Types
typedef

Type Definition

enum

Enumerations

Named Constants
#define
Const

Flow Control
If_else
for loops
do_while
switch case
jump statements [break - continue - goto ] , exit function.
Functions
Prototypes and Definitions
Passing Params by value and by reference
Pointers

Standard Input Output

File Processing

Important Library Functions

data_type var1< = init1>, var2< = init2>,;


var1,var2,.. are a variable identifiers.
init1,init2,.. are initialization values.
Ex:
To know the size of any type in bytes to define the range ;
use the operator sizeof().
To know the range of any integral type use the constants defined in the
header file <limits.h>
To know the range of any floating point type use the constants defined
in the header file <floats.h>
Ex: use the following C++ statement to know the integer size on your compiler

1-Integral Data [Decimal , Octal & Hexa-decimal]

Hexadecimal numbers are prefixed by 0x


Octal numbers are prefixed by 0
The integer size is 32 bits on the dev C++ on the 32 bits platform.

Integer range
For signed integer the maximum is : 231 1 the minimum is 231
For unsigned integer the maximum is 232 1
Also using the <limits.h> library and the flowing constants:

The output is :

2- Real Numbers [IEEE 754 Standard]


i-

Single precession [32 bits]

ii- Double precession [64 bits]

To get the range use the library <float.h> and the following constants

The output is:

3- Characters [Extended ASCII Code] of the size 1 Byte =8 bits

You could initialize the char by its ASCII value and treat it as an integer or
assigning a keyboard character to it using the single quote
4- Wide characters [UNICODE] of size 2 Bytes = 16 bits.
5-Boolean type

6-Void

Used with the functions to mean no return.


And used with the pointers to mean that the pointers points to any type.
Pointersfunctions

Arithmetic Operators
+ [addition] , - [Subtraction] , * [multiplication] , / [Division] , % [Remainder]
++ [Increment by One] , -- [Decrement By One].
Shift Operators :
>> [Shift right of the equivalent binary number ]
<< [Shift left of the equivalent binary number]
Right Shift
Left Shift

Number 5 in binary is 101 so right shift two positions results in number 1

Bitwise Operations
& [Bitwise AND] , | [Bitwise OR] , ^ [Bitwise XOR]
Binary EquivalentOperator

Logical Operators
&& [logical AND] ,

|| [Logical OR] , ! [Logical NOT]

Example:

Explanation:
False =0 true = 1 || &&
Then: A || B = 1 OR 1 = 1

; A&&B=1 AND 1 = 1

Binary Number^ | &


Then:
A | B = 1001 OR 0110 = 1111 = 15 decimal
A & B = 1001 AND 0110 = 0000 = zero decimal

Assignment Operators
= [Assign ] , *= [Assign product], /= [Assign quotient]
%= [assign remainder], += [Assign Sum], -= [Assign Difference]
<<= [Assign left shift], >>= [Assign right shift ]
&= [Assign bitwise AND] , |= [Assign bitwise OR] , ^= [Assign bitwise XOR]
Relational
< [less than ] , > [ Greater than] , <= [less than or equal]
>= [Greater than or equal ] , == [Equal] , != [Not equal]

Array is a new type which contain a multiples of specific basic data type.
Declaration using the square brackets
Example :

Indexing items of the array starting from index 0 to index size-1

The array of characters is a special type of array as you can read it or output it
using the name of the array .

The array of characters ends with the Null Character 0

Structure is a new type which may contain different basic data types.
The structure sytax is :

struct struct_name; //forward declaration


struct struct_name
{
structure_members_declarations;
}; // dont forget the ending semicolon
struct_name object_name;//declaring an instance (object) of the structure.
struct_name object_name[size] ;
//declaring array of instances (objects) of the structure struct_name.

Example:

To access the structure members use the

operator.

If the member is of pointer type use the arrow operator

Example of the initialization for the above structure :

- Will be studied later on through the course.

Type definition syntax:

typedef type new_name;


typedef doesnt create a new data type ; it rather creates useful and simple
mnemonics
Examples :

Later on through the program you could define the following variables:

The type definition will be used extremely through this course.

To make sense of a set of finite integer numbers which has a significant meaning.

Example:

The old C fashion to define the constants:

#define const_name value


Example:

The new C++ fashion of defining the constants:

type const const_name= initialization;


Example:

Used to make decisions through the program which make the required actions
of the program
i.e. jump to a certain point of the variable satisfy some conditions.

The basic pattern for the if statement:

If(condintion_expression)
{ true_statements;}
<else{false_statements;}>
The else & its false statement is optional.
The true and false statements could be themselves if statements, allowing
for a series of conditional tests nested on any depth.

The basic pattern for the switch case statement:

switch (switch_expression)
case const_expression_i : case_statement_i;
break;
default: default_statement;
The 2nd &3rd line is repeated for different case constants and so different
case statements.

for(<initialization_expression>;<test_expression>;<increment_expression>)
{ loop_statements;}

Each of initialization , test and increment statements are optional


So different forms of the for loops are:

The general pattern for the while statement:

while(condition_expression)
{loop_statements;}

The general pattern for the do_while statement:

do {loop_statements;}
while(condition_expression);

The syntax is

break;
Used only inside (while,do,and for loops) or a switch statements
It terminates the iteration or the switch statement.

The syntax is

continue;
Transfers control the test condition statement in while and do iterations
and to the increment statement in the for loop;

The function could return any type except an array or function type.

Function prototype syntax :


Prototype

<type> func_name(parameter_declaration_list);

Function Signature :
The function name + Number & Type of the function parameters

Function Definition syntax :


<type> func_name(parameter_declaration_list)
{
Function_implemenation
return value_of_type
}

Examples:
- Function that its parameters are of the basic data types

- Function that its parameters are of the compound data types

- Function that return a void [when returning to the original instruction


pointer and code segment there are no pop for any other variables]
So to make use of this type of functions either make standard output
Or return by passing the parameters by the reference.

You could use the statement return; within a void function to make it
terminate in an earlier step;
Example:

There are two syntaxes for the passing by reference:


1

The parameters in the prototype are references to the type

The calling variables are of the original type

And the calling :

The parameters in the prototype are pointers to the type.

The calling variables are reference to the type.

And the calling:

The standard input is by the default is the keyboard.


The standard output is by the default is the terminal window in the screen.

-Either using non standard input and output of type ifstream and ofstream
-Or redirects the standard input and output to work with the files.

Consider that the ifstream and ofstream is a compound data types like
structures , then you should declare objects of these data types.
The general pattern for using these classes [ifstream and ofstream]:
Dont forget to

After finishing the processing with input and output files release them by:

Example:

You should use notepad++ software to work neatly with the files.

Also without using a user defined handles you could use the standard I/O objects to
work with the files by using the redirection function freopen defined in stdio.h

Then cin and cout and other standard functions which work by the default for
the screen and keyboard now redirected to the input and output files.

1The requied is to sum all the numbers starting from a given integer number
num1 till the integer number num2 ; assuming num2>num1
The straightforward way of thinking is to write a code which loops all
numbers starting from num1 till num2
During each loop iterate accumulate the sum [which is initially zero].
Requires n steps calculating the sum where n=num2-num1+1
Instead think of the problem as an arithmetic sequence and derive an
analytic sum in terms of num1 and num2
sum = num1+ (num1+1)+ (num1+2)+...... + (num2-1) num2
sum = num2+(num2-1)+ (num2-2)+...... + (num1+1) num1
2*sum=(num1+num2)+(num1+num2) ..... (num1+num2)
where n=num2-num1+1
2*sum=n*(num1+num2)
sum=

(num1+num2)(num2-num1+1)
2

Fast calculation of the sum in a single step!!!!


C++ Program for the straightforward method 1st idea:

n times

C++ Program for the arithmetic sequence sum[ 2nd idea] :

2 To get the maximum


Assume the first item in the array is the maximum
Loop over all the elements of the array ; in each iterate compare the
assumed maximum to the current item if its greater then the current
item is the new maximum.
At the final iterate the maximum value is obtained
To get the minimum
Assume the first item in the array is the minimum
The same algorithm as above but check for the current item in each
iterate if less than the assumed minimum.

The Full Program:

3-

4-

First the function must pass the parameters by reference not value
Second there are generally 3 methods of swapping two variables:
1-Using a temporal variable

object of a structure _
time inefficientlarge memory _of huge size

2-Using the Addition and Subtraction

Proof:
a a b
b a b a b b a
a a b a b a b
So the final values of a and b is swapped

Max or Min value


Pipelined Data Hazard
in parallelsequential Processor

3-Using the XOR function

Proof:
Two important laws from the logic:
a a=0
a 0=a
a a b
b a b = a b b a 0 = a
a a b = a b a b 0 = b
So the final values of a and b is swapped

Pipelined Data hazard


extra in parallel sequential Processor
typememory need
The driver code:

5-

Circle Area = r2
Circle Circumference = 2 r

Theres a common need for the constant pi ; So a named constant


pi= 3.141592 will be done through the c++ program.
Its required a function that gives the area and circumference so the
function will pass the parameters by the reference.
Type of data should be float or double not integer.

Dont forget to
Instead

to use pow () function.


may be replaced by

The Assignment Statement:

Understand the assignment requirements and re-write the code with your method.

The purpose is not to cheat the solution but to understand it.


We will study this term different sorting algorithms;
A simple sorting algorithm is the Bubble Sort which you stood already
in the first year programming course.
The default standard Input
The default standard Output
The default standard print

is the keyboard.
is the terminal window.
is the printer.

But its required to redirect the standard input and standard output to
the files.
Which means using cin and cout but work with files.
The algorithm repeatedly stepping through the list
o In each step compare each pair of adjacent items
o Swap them if they are in the wrong order
o Number of steps at the worst case equal the number of list elements
So we will need the swap function done in problem 4 in the revision sheet.
The complete program without using redirection will be as the following :

The Complete Program but redirect the standard input output to work with files

Here cin get the input from the standard input which is redirected to the file
data.in
And cout and even the system(PAUSE) output the data to the redirected
standard output which is the file data.out

The input file should be put in the same directory as the project files saved.
otherwise in the input file constructor you must write the full input file directory.
So before compiling the project.
Right click on the free area at the project folder , and where the executable file
is expected to be created , create a text file and rename it to data.in
If the file name extension is not appeared goto tools>>folder options then at
the view tab uncheck the option Hide extensions for known file types.

Compress all the above files and send them to the indicated email.

Further assistance via profvip.abotaleb@gmail.com

También podría gustarte