0% found this document useful (0 votes)
29 views

Function2022 Examples

The document contains instructions from Dr. Albert Shawky for writing C++ programs to: 1) Take student name and grade as input and output the grade in text format based on ranges. 2) Take two numbers and an operator as input, perform the arithmetic operation, and output the result. 3) Take a number as input and output its factorial. It also provides examples of functions to: 4) Calculate the sum of values in an array. 5) Find the location of the largest value in an array. 6) Find the location of a target value in an array.

Uploaded by

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

Function2022 Examples

The document contains instructions from Dr. Albert Shawky for writing C++ programs to: 1) Take student name and grade as input and output the grade in text format based on ranges. 2) Take two numbers and an operator as input, perform the arithmetic operation, and output the result. 3) Take a number as input and output its factorial. It also provides examples of functions to: 4) Calculate the sum of values in an array. 5) Find the location of the largest value in an array. 6) Find the location of a target value in an array.

Uploaded by

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

DR Albert Shawky

Write a program to do the following:


Asks the user about the student name and the grade then
display the
grades as follows:
 If the grade >=85, grade = “Excellent “
 If the grade <85 and >=75, grade = “Very Good”
 If the grade <75 and >=65, grade = “Good ”
 If the grade <65 and >=50, grade = “Pass”
 If the grade <50, grade = “Fail”
Save program as “grades”.

# include <iostream> # include <iostream>


using namespace std; using namespace std;
void main()
{ void fgrde( float a)
float grade; {
cout<<"plz enter the
grade of the student"; if(a>=85)
cin>>grade; cout<<"excellent";
if(grade>=85) else if(a>=75)
cout<<"excellent"; cout<<"very good";
else if(grade>=75) else if(a>=65)
cout<<"very good"; cout<<"good";
else if(grade>=65) else if(a>=50)
cout<<"good"; cout<<"pass";
else if(grade>=50) else
cout<<"pass"; cout<<"fail";
else }
cout<<"fail";
}
Void main()

{
float num;
cout<<"plz enter the
grade of the student";
cin>>num;
fgrde(num);
}

1
DR Albert Shawky

2. Using if statement, write a C++ program that asks


the user to enter two double numbers (num1 and num2)
and one arithmetic operator (+, -,*, or /) defined as
char identifier. Your program calculates and prints
the result arithmetic operation such that:
 If user enter + then the program calculates and
prints num1 + num2
 If user enter – then the program calculates and
prints num1 -num2
 If user enter * then the program calculates and
prints num1 * num2
 If user enter / and prints num1 / num2
 Otherwise the program displays an error message
“invalid
input Operator”.
And then rewrite the program using switch case.
Save program as “Operators”
# include <iostream>
using namespace std;
void main()
{
double num1,num2,result;
char oper;
cout<<"plz enter 2 number";
cin>>num1>>num2;
cout<<"plz enter operation";
cin>>oper;
if(oper=='+')
result=num1+num2;
else if (oper=='-')
result=num1-num2;
else if (oper=='*')
result=num1*num2;
else if (oper=='/')
result=num1/num2;
else
cout<<"invalibleinput oper";
cout<<"result"<<result<<endl;
system("pause");}
//the same with void function

2
DR Albert Shawky

# include <iostream>
using namespace std;

void calc(double num1,double num2,char oper)


{
double result;

if(oper=='+')
result=num1+num2;
else if (oper=='-')
result=num1-num2;
else if (oper=='*')
result=num1*num2;
else if (oper=='/')
result=num1/num2;
else
cout<<"invalibleinput oper";

cout<<"result"<<result<<endl;

}
void main()
{
double num1,num2;
char oper;

cout<<"plz enter 2 number";


cin>>num1>>num2;
cout<<"plz enter operation";
cin>>oper;

calc(num1,num2,oper);

system("pause");}

//the same with void function


# include <iostream>
3
DR Albert Shawky

using namespace std;

double calc(double num1,double num2,char oper)


{
double result;

if(oper=='+')
result=num1+num2;
else if (oper=='-')
result=num1-num2;
else if (oper=='*')
result=num1*num2;
else if (oper=='/')
result=num1/num2;
else
cout<<"invalibleinput oper";

return (result);
}
void main()
{
double num1,num2;
char oper;

cout<<"plz enter 2 number";


cin>>num1>>num2;
cout<<"plz enter operation";
cin>>oper;

double m = calc(num1,num2,oper);
cout<<”the result is “<<m;
system("pause");}

4
DR Albert Shawky

3. Write a C++ program that calculates the factorial


to the number entered by the user.

#include <iostream>
using namespace std;
void main ()
{
int f=1,a;
cout<<"Please enter num";
cin>>a;
for(int i=a;i>=1;i--)
{
f*=i;
}
cout<<"factorial="<<f;
system("pause");
}
# include <iostream> # include <iostream>
using namespace std; using namespace std;

int factorial( int a) void factorial( int a)


{ {

int f=1,a; int f=1,a;


for(int i=a;i>=1;i--) for(int i=a;i>=1;i--)
{ {
f*=i; f*=i;
} }
Rreturn (f); cout<<"factorial="<<f;

} }
Void main() Void main()

{ {
int num; int num;
cout<<"plz enter the cout<<"plz enter the
number "; number";
cin>>num; cin>>num;
factorial (num);
int z= factorial (num);
cout<<z;
} 5
}
DR Albert Shawky

30. Write a function named "sum" that takes as its arguments the following:
(1) an array of floating point values;
(2) an integer that tells how many floating point values are in the array.
The function should return as its value the sum of the floating point values in the
array. Thus, for example, if the
array that's passed to the function looks like this:

5.8 | 2.6 | 9.0 | 3.4 | 7.1


then the function should return the value 27.9 as its value.

float sum(float a[],int size)


{
Float total=0;
For (int 1=0;i<size;i++)
{
Total+=a[i];
}
Return total;
}

Void main()
{
float x[5];
cout<<”please enter values”;

For (int 1=0;i<5;i++)


{cin>>x[i];
}

Float z= sum(x,5);
cout<<”sum=”<<z;

6
DR Albert Shawky

31. Write a function named "location_of_largest" that takes as its


arguments the following:
(1) an array of integer values;
(2) an integer that tells how many integer values are in the array.
The function should return as its value the subscript of the cell containing the
largest of the values in the array.
float location(int a[],int size)
{
Int max=a[0],loc;
For (int i=0;i<size;i++)
{
If(a[i]>max)
{ max=a[i];
Loc=i;
}
}
Return loc;
}
Void main()
{ int x[5];
cout<<”please enter values”;
For (int 1=0;i<5;i++)
{cin>>x[i];
}
int z= location (x,5);
cout<<”location=”<<z;}

7
DR Albert Shawky

32. Write a function named "location_of_target" that takes as its


arguments the following:

(1) an array of integer values;


(2) an integer that tells how many integer values are in the array;
(3) an integer "target value".
The function should determine whether the given target value occurs in any of the
cells of the array, and if it does, the function should return the subscript of the cell
containing the target value.
If more than one of the cells contains the target value, then the function should
return the largest subscript of the cells that contain the target value.
If the target value does not occur in any of the cells, then the function should return
the sentinel value 1

float location(int a[],int size,int target)


{
Int loc = -1;
For (int i=0;i<size;i++)
{
If(a[i]== target)
{
Loc=i;
}
}
Return loc;
}
Void main()
{ int x[5],target;
cout<<”please enter values”;
For (int 1=0;i<5;i++)
{cin>>x[i];
}
Cout<<”enter target”;
Cin>> target;
int z= location (x,5, target);
cout<<”location=”<<z;
}
8

You might also like