Documente Academic
Documente Profesional
Documente Cultură
PROGRAMMING FUNDAMENTALS
WITH C++
void main()
{
cout << Square Root. Ex: sqrt(9.0) =
<< sqrt(9.0) << endl;
cout << Powers. Ex: pow(3.0, 4.0) =
<< pow(3.0,4.0) << endl;
cout << Absolute Value for int. Ex:
<< abs(-9) = << abs(-9)
<< endl;
cout << Absolute Value for long. Ex:
<< labs(-900) = << labs(-900)
<< endl;
Output:
Square Root. Ex: sqrt(9.0) = 3.0
Powers. Ex: pow(3.0,4.0) = 81
Absolute Value for int. Ex: abs(-9) = 9
Absolute Value for long. Ex: labs(-9000) = 9000
Absolute Value for double. Ex: fabs(-9.5) = 9.5
Ceiling (round up). Ex: ceil(4.1) = 5
Floor (round down). Ex: floor(4.7) = 4
Prof. amr Goneid, AUC 5
2. Modular Programming with
Functions
Level
0 Main Function
2 Function4 Function5
.
.Functions are natural building blocks for modular programming
Local main
Data Area Data Area
Main Body
Function Body
Invoke function
Next action
Output Params
void
Input Params Function
Action
Examples:
int cube ( int n ) ;
// A function receiving an int parameter (n) and returning
an int value.
float minxy ( float x , float y ) ;
// A function receiving two float parameters ( x , y ) and
returning a float value.
Function Actions
(Executable Statements)
}
x a
num n
y b
void main()
{
cout << The average of 3.0 and 7.0
<< is << average(3.0, 7.0)
<< endl;
cout << The average of 3.0, 4.0, and 8.0
<< is << average(3.0, 4.0,8.0)
<< endl;
}
}
float average(float x, float y, float z)
{
return ((x + y + z)/3.0);
}
Output:
The average of 3.0 and 7.0 is 5.0000
The average of 3.0, 4.0, and 8.0 is 5.0000
Prototypes of A , B , C
Main
Data x , y
DOS CS DS SS HEAP
LM HM
DS = Data Segment (Data)
CS = Code Segment ( Main & Modules code)
SS = Stack Segment (System Stack)
Heap = Rest of DOS memory
b N N-> y b N M <- y
a M M -> x a M N <- x
Swap addr Swap addr
b N Addr of b b M Addr of b
a M Addr of a a N Addr of a
Swap addr Swap addr