Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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
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
File Processing
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 :
To get the range use the library <float.h> and the following constants
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
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
Bitwise Operations
& [Bitwise AND] , | [Bitwise OR] , ^ [Bitwise XOR]
Binary EquivalentOperator
Logical Operators
&& [logical AND] ,
Example:
Explanation:
False =0 true = 1 || &&
Then: A || B = 1 OR 1 = 1
; A&&B=1 AND 1 = 1
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 :
The array of characters is a special type of array as you can read it or output it
using the name of the array .
Structure is a new type which may contain different basic data types.
The structure sytax is :
Example:
operator.
Later on through the program you could define the following variables:
To make sense of a set of finite integer numbers which has a significant meaning.
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.
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.
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;}
while(condition_expression)
{loop_statements;}
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.
<type> func_name(parameter_declaration_list);
Function Signature :
The function name + Number & Type of the function parameters
Examples:
- Function that its parameters are of the basic data types
You could use the statement return; within a void function to make it
terminate in an earlier step;
Example:
-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
n times
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
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
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
5-
Circle Area = r2
Circle Circumference = 2 r
Dont forget to
Instead
Understand the assignment requirements and re-write the code with your method.
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.