Exercises Chapter3 1
Exercises Chapter3 1
Exercises chapter3
Lab 5 and 6
Type Cast
A Cast operator is a unary operator which forces one data type to be converted into another data
type.
C++ supports four types of casting such as:
Implicit conversion
Explicit conversion
Static Cast
Exercise: Write a C++ program that takes a float number then convert it to integer with different
ways.
Functions
A function is a group of statements that is executed when it is called from some point of the
program.
1- Predefined Functions
C++ provides a library of predefined functions. The definitions of many common functions are
found in the cmath and cstdlib. Functions are pow (parameter1, parameter2) (𝑙𝑖𝑘𝑒𝑥 2 ),
sqrt(parameter1) (like √𝑥 ), abs(parameter1), and floor(parameter1) such as floor(48.79) = 48.0
For more functions go to the following link: http://www.cplusplus.com/reference/cmath/
To access these functions, your program must include camath and cstdlib library:
#include <cmath>
#include <cstdlib>
Exercises:
Exercise 1: Use sqrt, pow, and abs function with cout function.
Exercise 2: Write a C++ program that take a number from user then output the square of this
number.
Enter a number: 4
The square: 2
C++ ENG: BASMA RAMADAN
2- Defined Functions
Syntax: type name ( parameter1, parameter2, ...) { statements }
Where
- type is the data type specifier of the data returned by the function.
- name is the identifier by which it will be possible to call the function.
- parameters (as many as needed): Each parameter consists of a data type specifier followed by
an identifier, like any regular variable declaration (for Exercise: int x) and which acts within the
function as a regular local variable. They allow to pass arguments to the function when it is
called. The different parameters are separated by commas.
- statements is the function's body. It is a block of statements surrounded by braces { }.
Data sharing Data sharing is not possible as data Data sharing is possible as
of the local variable can be multiple functions can access the
accessed by only one function. same global variable.
Modification When the value of the local When the value of the global
of variable variable is modified in one variable is modified in one
value function, the changes are not function changes are visible in the
visible in another function. rest of the program.
Accessed by Local variables can be accessed You can access global variables by
with the help of statements, inside any statement in the program.
a function in which they are
declared.
Exercise:
C++ ENG: BASMA RAMADAN
More Exercises
Exercise 1: Write a program in C to find the square of any number using the function.
Exercises 3: Write a C++ program that will display the calculator menu.
The program will prompt the user to choose the operation choice (from 1 to 5). Then it asks the
user to input two integer vales for the calculation. See the sample below.
MENU
1. Add
2. Subtract
3. Multiply
4. Divide
5. Modulus
Enter your choice: 1
Enter your two numbers: 12 15
Result: 27
#include <cstdlib>
#include <iostream>
using namespace std;
void displaymenu(){
cout<<"==================================================="<<"\n";
C++ ENG: BASMA RAMADAN
int main()
{
C++ ENG: BASMA RAMADAN
//show menu
displaymenu();
int yourchoice;
int a;
int b;
char confirm;
do
{
cout<<"Enter your choice(1-5):";
cin>>yourchoice;
cout<<"Enter your two integer numbers:";
cin>>a>>b;
cout<<"\n";
switch(yourchoice)
{
case 1:cout<<"Result:"<<Add(a,b);break;
case 2:cout<<"Result:"<<Substract(a,b);break;
case 3:cout<<"Result:"<<Multiply(a,b);break;
case 4:cout<<"Result:"<<Divide(a,b);break;
case 5:cout<<"Result:"<<Modulus(a,b);break;
default:cout<<"invalid";
}
cout<<"\nPress y or Y to continue:";
cin>>confirm;
}
while(confirm=='y'||confirm=='Y');
C++ ENG: BASMA RAMADAN
system("PAUSE");
return EXIT_SUCCESS;
}
Homework tasks
Task 1: Write a C++ program that take a number from user then output the power of this
number.
Enter a number: 4
The power: 1
Task 2: Write a function take three numbers from user then output the minimum number.
Task 3: Write function that take a string then reverse it.
Sample String: "1234abcd"
Expected Output: "dcba4321"