Lab 10 - ocal.info

Lab 10
Void function
Program 1
 Write a C++ program that includes a function to take as
arguments a length expressed in feet (int) and inches (int)
and return the total number of inches (int) in that many
feet and inches. Program will include function declaration,
function call and function definition.
For example, total_inches(1, 2) is supposed to return 14,
because 1 foot and 2 inches is the same as 14 inches.
Solution to Program 1
#include<iostream>
using namespace std;
double total_inches(int feet, int inches);
int main()
{
cout<<total_inches(1,2);
}
double total_inches(int feet, int inches)
{
inches = 12*feet + inches;
return inches;
}
Program 2
 Write a C++ program that includes two functions with
the same name “ave”. The first function takes two
arguments of type double and returns a value of type
double. The first function calculates average of two
numbers.
 The second function takes three arguments of type
double and returns a value of type double. The second
function calculates average of three numbers.
 Program will include function declaration,
function call and function definition.
Solution to Program 2
#include<iostream>
using namespace std;
double ave(double n1, double n2);
double ave(double n1, double n2, double n3);
int main()
{
cout<<ave(1,2)<<endl;
cout<<ave(1,2,3);
}
double ave(double n1, double n2)
{
return ((n1 + n2)/2.0);
}
double ave(double n1, double n2, double n3)
{
return ((n1 + n2 + n3)/3.0);
}
Program 3
 Write a C++ program that includes a function called
“celsius” that takes one argument of type of double and
no return value. The function converts a Fahrenheit
temperature to a Celsius temperature using the
following equation
C = (5/9)(F − 32)
Program will include function declaration, function call and function
definition.
Solution to Program 3
#include<iostream>
using namespace std;
void celsius(double fahrenheit);
int main()
{
celsius(32);
}
void celsius(double fahrenheit)
{
cout<< ((5.0/9.0)*(fahrenheit - 32));
}
Program 4
 Write a C++ program that includes two functions.
First function is called “print” that takes one argument
of type string and returns void. The second function is
called “myname” that takes one argument of type
string and returns void. The second function will be
called inside the first function. The second function
will print name and the first function will print “hello”.
 Program will include function declaration,
function call and function definition.
Solution to Program 4
#include<iostream>
using namespace std;
void print(string name);
void myname(string name1);
int main()
{
print("ahmed");
}
void print(string name)
{
cout<<"hello ";
myname(name);
}
void myname(string name1)
{
cout<<name1;
}
Program 5
 Write a C++ program that includes a void function
called “max” that takes two arguments of type int.
The function prints out the maximum of the two
numbers or prints out they are equal. Program will
include function declaration, function call and
function definition.
Solution to Program 5
#include<iostream>
using namespace std;
void max(int firstnumber, int secondnumber);
int main()
{
max(2,1);
}
void max(int firstnumber, int secondnumber)
{
if(firstnumber>secondnumber)
cout<<firstnumber<<" is bigger";
else if(secondnumber>firstnumber)
cout<<secondnumber<<" is bigger";
else
cout<<"both numbers are equal";
}
Program 6
 Write a C++ program that includes a function called
“read_filter” that has no parameters and that returns a
value of type double. The function read_filter prompts
the user for a value of type double and reads the value
into a local variable. The function returns the value
read provided this value is greater than or equal to zero
and returns zero if the value read is negative.
Program 7
 Write C++ program that includes a function called
“factorial” that takes one argument of type int a return
a value of type int. The function calculates factorial of
a number.
Program 8
 Write a C++ program that includes a function called “product” that takes three
arguments of type of int and returns a int. The function multiples the three
arguments.
Program 9
 Write a C++ program that includes two functions
called “absolute”. The first function takes one
argument of type int and returns int. The second
function takes one argument of type float and
returns float.
Program 10
 Write a C++ program that includes two functions with
the same name “sum”. The first function takes two
arguments of type double and returns a value of type
double. The first function calculates sum of two
numbers.
 The second function takes three arguments of type
double and returns a value of type double. The second
function calculates sum of three numbers.