Lab 17
Problem 1: Compute the area and perimeter of a circle.
In this program, you need to write two functions:
The first function is named by CalRec, which has no returned value and accepts three parameters: radius, area, and circumference (radius is the parameter passed by value; area and circumference are parameters passes by reference). This function will calculate the area and circumference of the circle as:
area=pi*radius*radius;
And calculate the perimeter of the rectangle as:
perimeter=2*pi(radius;
The second function is named by PrintRec, which has no returned value and accepts two parameters: area and circumference (both are parameters passed by value). This function will print the results as:
The area of the circle is ***.
The circumference of the circle is ***.
In the main function, you prompt user to enter the radius of the circle, and then call the function CalRec to get area and circumference, and then call the function PrintRec to print the results.
Problem 2: Write three functions to convert feet to inches, convert inches to centimeters, and convert centimeters to meters. Write a program that prompts a user for a measurement in feet and converts and outputs this value in meters.
Facts to use: 1 ft = 12 inches, 1 inch = 2.54 cm, 100 cm = 1 meter.
The basic structure for the program is:
#include <iostream>
using namespace std;
//function declarations(prototypes)
int main()
{
double feet, inches, cms, meters;
cout < "Enter your measurement in feet" < endl;
cin > feet;
//call functions and assign the returned value to inches, cms and meters respectively.
cout < feet < " feet equals " < inches < " inches" < endl;
cout < feet < " feet equals " < cms < " centimeters" < endl;
cout < feet < " feet equals " < meters < " meters" < endl;
return 0;
}
double feetToInches(double feet)
{
}
double inchesToCentimeters(double inches)
{
}
double centimetersToMeters(double cm)
{
}
Problem 3: Write a program that prompts the user to enter a year and then determines whether it is a leap year.Your program should have a loop and continue while the user enters the character ‘y’.
Write three functions according to the following descriptions:
Ø getYear has no formal parameters, asks the user to enter a year, and returns an integer value that is assigned to the integer variable year.
Ø isLeap has no returned value and two formal parameters, year and flag, determines whether the year is a leap year, and set flag to be true if the year is a leap year and false if it is not. A year is a leap year if: (1) it is divisible by 4, but is not divisible by 100 or (2)it is divisible by 400. (The year 2000 was a leap year.)
Hint: decide which is value parameter and which is reference parameter.
Ø moreData has no formal parameters and returns a char value.
For example: function moreData will do the following:
cout<”If you want to continue, please enter ‘y’, otherwise please enter any character: ”;
cin>ch;
and then reture ch.
#include <iostream>
using namespace std;
//function prototypes;
int main()
{
int year;
char ch;
bool flag;
ch=moreData();
while(ch==’y’)
{
year=getYear();
isLeap(year, flag);
if (flag)
cout<”Leap Year!”<endl;
else
cout<”Plain Year!”<endl;
ch=moreData();
}
return 0;
}
//three function bodies are here