In c 1 Write a program using the following function headers
In c++
1.
Write a program using the following function headers:
double getLength();
double getWidth();
double getArea(double len, double wid);
void displayData(double len, double wid, double area);
Write function main which calls these four functions. Make sure that the length and width are positive numbers.
Hand in 2 runs showing valid and invalid data.
2.
Write the function calls for the following headers and prototypes:
a.void showValue(int quantity)
b.void display(int arg1, double arg2, char arg3); pass the following to the call: int age; double income; char initial
c.double calPay(int hours, double rate);
Solution
// Question 1
// C++ code determine rectange area
#include <iostream>
#include <string.h>
#include <fstream>
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <stdlib.h>
#include <vector>
using namespace std;
double getLength()
{
double len;
while(true)
{
cout << \"Enter length: \";
cin >> len;
if(len > 0)
break;
else
cout << \"Invalid Input, length should be positive.\ \";
}
return len;
}
double getWidth ()
{
double wid;
while(true)
{
cout << \"Enter width: \";
cin >> wid;
if(wid > 0)
break;
else
cout << \"Invalid Input, width should be positive.\ \";
}
return wid;
}
double getArea(double len, double wid)
{
double area = len*wid;
return area;
}
void displayData(double len, double wid, double area)
{
cout << \"Length: \" << len << endl;
cout << \"Width: \" << wid << endl;
cout << \"Area: \" << area << endl;
}
int main()
{
double length,width;
double area;
length = getLength();
width = getWidth();
area = getArea(width, length);
displayData(length, width, area);
return 0;
}
/*
output:
Enter length: 12
Enter width: 3
Length: 12
Width: 3
Area: 36
Enter length: -2
Invalid Input, length should be positive.
Enter length: 3
Enter width: -4
Invalid Input, width should be positive.
Enter width: 0
Invalid Input, width should be positive.
Enter width: 3
Length: 3
Width: 3
Area: 9
*/
// Question 2
// C++ code
#include <iostream>
#include <string.h>
#include <fstream>
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <stdlib.h>
#include <vector>
using namespace std;
void showValue(int quantity)
{
cout << \"Quantity: \" << quantity << endl;
}
double calPay(int hours, double rate)
{
return hours*rate;
}
void display(int arg1, double arg2, char arg3)
{
cout << \"Age: \" << arg1 << endl;
cout << \"Income: \" << arg2 << endl;
cout << \"Initials: \" << arg3 << endl;
}
int main()
{
int quantity,age;
double income;
char initial;
quantity = 10;
age = 53;
initial = \'C\';
showValue(quantity);
int hours = 12;
double rate = 100;
income = calPay(hours,rate);
display(age,income,initial);
return 0;
}
/*
output:
Quantity: 10
Age: 53
Income: 1200
Initials: C
*/



