Anda di halaman 1dari 21

Name:ali samy ali mohamed

Assignment -Laboratory 2
Code: 20162171 Week 2 .

Level:one (cs) (Function)

Structured
Programming

To :
dr/salmareda
2.9.1::Write a C++ program that allows the user to input the temperature in Fahrenheit as a
parameter to a function then outputs the temperature in Celsius. C = ((F - 32) * 5 / 9)

the solution
#include "stdafx.h"
#include <iostream>
using namespace std;
float temp(float);

int main()
{
float f;
cout << "enter a temp in fahrenheit" << endl;
cin >> f;
cout << "the temp in celsius =" << temp(f) << endl;

return 0;
}

float temp(float fahrenheit)


{
return ((fahrenheit - 32) * 5 / 9);
}

1
2.9.2 :: Write a C++ program that gets three values x, y, and z as a
parameters to a function then outputs the average of those three numbers.

the solution
#include "stdafx.h"

#include <iostream>

using namespace std;


float sum (float, float, float);
float average(float, float, float);
int main()

{
float num1, num2, num3;
cout << "enter three number" << endl;
cin >> num1 >> num2 >> num3;
cout << "the sum =" << sum(num1, num2, num3) << endl;
cout << "the average=" << average(num1, num2, num3) << endl;
return 0;
}
float sum (float x, float y, float z)
{
return (x + y + z);
}
float average(float x, float y, float z)
{
return (x + y + z) / 3;

2
2.9.3:: Write a C++ program that allows the user to input the radius of a circle as a parameter
to a two functions the first, it computes the area and the second it computes the
circumference.

the solution
#include "stdafx.h"
#include <iostream>
using namespace std;
float area(float);
float circumference(float);
const float pi = 3.14;
int main()
{
float R;
cout << "enter the radius of a circle=" << endl;
cin >> R;
cout << "the area =" << area(R) << endl;
cout << "the circumference =" << circumference(R) << endl;
return 0;
}
float area ( float radius)
{
return (pi * radius * radius);
}
float circumference(float radius)
{
return (2 * pi * radius);
}

3
2.9.4:: Write a C++ program that allows the user to input three float values as a
parameter to a 5 functions named (Summation, Subtraction, Modulus, Division,
& Multiplication) then outputs the result of them each per line..

the solution
#include "stdafx.h"
#include<iostream>
using namespace std;
float Summation(float, float);
float Subtraction(float, float);
int moduls(int, int);
float Division(float, float);
float Multiplication(float, float);
int main()

{
float num1, num2;
cout << "enter two number " << endl;
cin >> num1 >> num2;
cout <<"the summation ="<<Summation(num1, num2) << endl;
cout << "the subtract =" << Subtraction(num1, num2) << endl;
cout << "the moduls =" << moduls(num1, num2) << endl;
cout << "the division =" << Division(num1, num2) << endl;
cout << "the multiply ="<<Multiplication(num1, num2)<< endl;

return 0;

float Summation(float x, float y)


{
return (x + y);
}
float Subtraction(float x, float y)
{
return (x - y);
}
4
int moduls(int x, int y)
{
return (x%y);
}
float Division(float x, float y)
{
return (x / y);
}
float Multiplication(float x, float y)
{
return (x*y);
}

5
2.9.5:: Write a C++ program that computes the smallest and the largest
of three integer numbers using predefined functions:

the solution

#include "stdafx.h"
#include<iostream>
using namespace std;
void Compute();
int main()
{
Compute();
return 0;
}

void Compute()
{
int x, y, z;
cout << " Please enter the three value : X , Y and Z"<<endl;
cin >> x >> y >> z;

if (x > y && x > z)


cout << " X is the bigger than Y and Z " << endl;

else if (x < y && x < z)


cout << " X is the smaller than Y and Z " << endl;

else if (x > y && x < z)


cout << " x is bigger than y and x is the smaller than z" << endl;

else if (x < y && x > z)


cout << " x is smaller than y and x is the bigger than z" << endl;

else
cout << " you should enter the number \n ";

if (y > x && y > z)


cout << " y is bigger than z and x " << endl;

else if (y < z && y < x)


cout << " y is the smaller than x and Z " << endl;

6
else if (y > x && y < z)
cout << " y is bigger than x and - y is the smaller than z " << endl;

else if (y < x && y > z)


cout << " y is smaller than x and - y is the bigger than z " << endl;
else
cout << " you should enter the number " << endl;

if (z > x && z > y)


cout << " Z is bigger than y and x " << endl;

else if (z < x && z < y)


cout << " Z is smaller than y and x " << endl;

else if (z > x && z < y)


cout << " z is bigger than x and z is the smaller than y " << endl;

else if (z < x && z > y)


cout << " z is smaller than x and z is the bigger than y " << endl;

else
cout << " you should enter the number " << endl;
}

7
2.9.6 Write a C++ program that allows the user to input an integer value as a parameter then
use predefined functions to compute its square root, log, and power of 5.

the solution
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int number(int);
int main()
{
double num;
cout << "enter the number " << endl;
cin >> num;
cout << "the sequare root of number = " << sqrt(num) << endl;
cout << "the log of number = " << log (num) << endl;
cout << "the power of number to 5 = " << pow(num, 5) << endl;
return 0;

}
int number(int value)
{
return sqrt( value) ;
return log( value);
return pow( value, 5);
}
Other solution

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num;
cout << "enter the number " << endl;
cin >> num;
cout << "the sequare root of number = " <<sqrt(num) << endl;
cout << "the log of number = " << log (num) << endl;
cout << "the power of number to 5 = " << pow(num, 5) <<
endl;
return 0;}
8
2.9.7:: Write a C++ program function that uses a call by reference to convert
from inches to centimeters knowing that 1 inch = 2.54 centimeters.

the solution
#include "stdafx.h"
#include <iostream>
using namespace std;
float converting(float &, float &);
int main()
{
float inch, cent;

cout << "enter the inches " << endl;


cin >> inch;
converting(inch, cent);
cout << "the inches = " << cent << " centimeters" << endl;
return 0;
}

float converting(float &inches,float &centimeters)


{
centimeters = ( inches/2.54 );
return (centimeters);
}

9
2.9.8:: Write a C++ program that has a function named "reduce" which
takes two positive integer arguments, call them "num" and "denom",
treats them as the numerator and denominator of a fraction, and
reduces the fraction. That is to say, each of the two arguments will
be modified by dividing it by the greatest common divisor of the two
integers;
the solution

10
2.9.9:: Write a C++ program that allows the user to input
string as a parameter then use predefined function to print
this string again in uppercase.

the solution:

11
2.9.10:: Write a C++ program that allows the user to input the lengths of two
sides of a triangle and the angle between them in degrees then compute the
length of the third side by using the formula a2 = b2 + c2 - 2bc(cos()).
the solution

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
float triangle ( float, float);
int main()
{
float x, y;
cout << "enter the b =" << endl;
cin >> x;
cout << "enter the c =" << endl;
cin >> y;
cout << "a =" << triangle( x , y) << " cm " << endl;
return 0;
}
float triangle( float b, float c)
{
float a;
a = pow (b, 2) + pow(c, 2) - 2 * b*c*(cos(0));
return (sqrt(a));
}

12
the solution
a)
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double u, v, w;
cout << "enter the u =" << endl;
cin >> u;
cout << "enter the v = " << endl;
cin >> v;
cout << "enter the w = " << endl;
cin >> w;
cout << "the result = " << sqrt(u + v)*pow(w, 2) << endl;

return 0;
}

13
b)
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
double x, y;
cout << "enter the x = " << endl;
cin >> x;
cout << "enter the y =" << endl;
cin >> y;
cout << "the result =" << log10 (pow (x,y)) << endl;

return 0;
}

14
c)

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x, y;
cout << "enter the x =" << endl;
cin >> x;
cout << "enter the y = " << endl;
cin >> y;
cout << "the result =" <<sqrt((pow(x-y, 3))) << endl;

return 0;
}

15
d)
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, c, w, z;
cout << "enter the a =" << endl;
cin >> a;
cout << "enter the c =" << endl;
cin >> c;
cout << "enter the w =" << endl;
cin >> w;
cout << "enter the z =" << endl;
cin >> z;
cout << "the result =" << abs((a / c - w * z)) << endl;
return 0;
}

16
2.9.12 ::Write statements that compute and display the absolute
difference of two type double variables, x and y (|x - y|).

the solution
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int x , y;
cout << "enter the first number =" << endl;
cin >> x;
cout << "enter the seconed number =" << endl;
cin >> y;
cout << "the absolute = " <<abs(x - y) << endl;

return 0;
}

17
2.9.13::

the solution
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double x1, x2, y1, y2;
cout << "enter x1 " << endl;
cin >> x1;
cout << "enter x2 " << endl;
cin >> x2;
cout << "enter y1 " << endl;
cin >> y1;
cout << "enter y2 " << endl;
cin >> y2;
cout << "the distance =" <<sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)) <<
endl;
return 0;
}

18
2.9.14
the solution
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void printO();
void printH();
void printM();
void printY();
void printG();
void printO();
void printD();
int main()
{
printO();
cout << "";
printH();
cout << endl;
printM();
cout << "";
printY();
cout << endl;
printG();
cout << "";
printO();
cout << "";
printD();
cout << endl;
}

void printO()
{
cout << "O";
}
19
void printH()
{
cout << "H";
}
void printM()
{
cout << "M";
}
void printY()
{
cout << "Y";
}
void printG()
{
cout << "G";
}

void printD()
{
cout << "D";
}

20

Anda mungkin juga menyukai