0% found this document useful (0 votes)
33 views11 pages

Exercises Chapter3 1

Uploaded by

segnilemi029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views11 pages

Exercises Chapter3 1

Uploaded by

segnilemi029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C++ ENG: BASMA RAMADAN

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.

For more visit: http://www.cplusplus.com/doc/tutorial/typecasting/


C++ ENG: BASMA RAMADAN

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>

To call these functions:


Function_Name(Parameter1, Parameter2, ...)

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 { }.

Types of User-defined Functions in C++

1- Function that doesn't take parameter and doesn't return result.


Exercise: Write a function that print a message: Welcome, this is a function.
C++ ENG: BASMA RAMADAN

2- Function that take a parameter and doesn't return


Exercise: Write a function that take your name and print a message say: Welcome, your name.

3- Function doesn’t take an argument but return a value


Exercise: Write a function that return your name.
C++ ENG: BASMA RAMADAN

4- Function with argument and return value


Exercise: write a function that take a number and return it is type odd or even.
C++ ENG: BASMA RAMADAN

Difference between Local and Global Variable


Variables that are declared inside a function or a block are called local variables and are said to
have local scope. These local variables can only be used within the function or block in which
these are declared.
Variables that are defined outside of all the functions and are accessible throughout the program
are global variables and are said to have global scope.

Parameter Local Global

Scope It is declared inside a function. It is declared outside the function.

Value If it is not initialized, a garbage If it is not initialized zero is stored


value is stored as default.

Lifetime It is created when the function It is created before the program's


starts execution and lost when the global execution starts and lost
functions terminate. when the program terminates.

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.

Memory It is stored on the stack unless It is stored on a fixed location


storage specified. decided by the compiler.

Exercise:
C++ ENG: BASMA RAMADAN

More Exercises
Exercise 1: Write a program in C to find the square of any number using the function.

Exercise 2: Write a C++ function to find the Max of two numbers.


C++ ENG: BASMA RAMADAN

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

cout<<" MENU "<<"\n";


cout<<"==================================================="<<"\n";
cout<<" 1.Add"<<"\n";
cout<<" 2.Subtract"<<"\n";
cout<<" 3.Multiply"<<"\n";
cout<<" 4.Divide"<<"\n";
cout<<" 5.Modulus"<<"\n";
}
int Add(int a,int b){
return(a+b);
}

int Substract(int a, int b){


return(a-b);
}

int Multiply(int a, int b){


return(a*b);
}
float Divide(int a,int b){
return(a/b);
}
int Modulus(int a, int b){
return(a%b);
}

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"

You might also like