Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// Passing arguments by-Value and using return types. #include<iostream> using n

ID: 3810877 • Letter: #

Question

// Passing arguments by-Value and using return types.

#include<iostream>
using namespace std;

// Constant factor converting feet to meters: 3.28 feet = 1 meter
const double FEET_TO_METERS_FACTOR = 0.304878048; // (1 / 3.28)

// TODO: Declare 4 function prototypes

// - Name: getFeet
// Parameters: <none>
// Returns: an int for the number of feet entered by the user

//       - Name: getInches
// Parameters: <none>
// Returns: a double for the number of inches entered by the user

// - Name: convertToMeters
// Parameters:
// - 1 int for feet (pass by value)
// - 1 double for inches (pass by value)
// Returns: a double for the number of meters

// - Name: displayResults
// Parameters:
//       - 1 int for feet (pass by value)
// - 1 double for inches (pass by value)
//       - 1 double for meters (pass by value)
//       Returns: <nothing>

// ********************************************
int main()
{
   // Declare program variables.

   int feet;
   double inches, meters;

   // TODO: Call function to get feet input.
   // An int value is returned - assign to the correct variable

   // TODO: Call function to get inches input.
   // A double value is returned - assign to the correct variable

   // TODO: Call function to convert feet and inches to meters.
   // Pass the two arguments to the function
   // Assign the returned value to the correct variable

   // TODO: Call function to print the results.
   // Pass the correct three arguments to the function
   // Nothing is returned, so do NOT provide a variable assignment

   cout << "End program - ";
   return 0;
}

// ********************************************
// TODO: Define getFeet()
// Refer to the function prototype above for parameter and return type info
//
// Repeat
// - Prompt the user to enter feet
// - Read the user's keyboard response
// Until a value of 0 or more is entered
//
// Note: You will need to declare a local variable inside the function to hold
// the user's entry for feet (be sure to use correct data type)

// TODO: Define getInches()
// Refer to the function prototype above for parameter and return type info
//
// Repeat
// - Prompt the user to enter inches
// - Read the user's keyboard response
// Until a value of 0 or more and less than 12 is entered
//
// Note: You will need to declare a local variable inside the function to hold
// the user's entry for inches (be sure to use correct data type)

// TODO: Define convertToMeters()
// Refer to the function prototype above for parameter and return type info
//
//       - Declare a new local variable called fractionalFeet that is type double
// - Type cast feet to be a double and assign to the fractionalFeet variable
// - On the same line where you are assigning the converted feet value to
// - fractionalFeet, also convert inches to feet (inches / 12) and add to the feet
// - Finally, return the result of multiplying fractionalFeet by FEET_TO_METERS_FACTOR

// TOTO: Define displayResults()
// Output statement to format results as shown
// using values passed to parameters:
// e.g.
//
// 10'-6.25" = 3.20757 meter(s).
//
// Use string literals along with value parameters:
// "'"
// "" = "
// " meter(s)."

/* Sample Output

Sample Run #1: <this line not in output>
===================== <this line not in output>
Enter feet: 5
Enter inches: 10.25

5'-10.25" = 1.78481 meter(s).
End program - Press any key to continue . . .

Sample Run #2: <this line not in output>
===================== <this line not in output>
Enter feet: -5
Enter feet: 3
Enter inches: -5
Enter inches: 15.75
Enter inches: 5.75

3'-5.75" = 1.06072 meter(s).
End program - Press any key to continue . . .
*/

Explanation / Answer

// C++ code

#include <iostream>
using namespace std;
// Constant factor converting feet to meters: 3.28 feet = 1 meter
const double FEET_TO_METERS_FACTOR = 0.304878048; // (1 / 3.28)
// TODO: Declare 4 function prototypes
// - Name: getFeet
// Parameters: <none>
// Returns: an int for the number of feet entered by the user
int getFeet();
// - Name: getInches
// Parameters: <none>
// Returns: a double for the number of inches entered by the user
double getInches();

// - Name: convertToMeters
// Parameters:
// - 1 int for feet (pass by value)
// - 1 double for inches (pass by value)
// Returns: a double for the number of meters
double convertToMeters(int feet,double inches);

// - Name: displayResults
// Parameters:
// - 1 int for feet (pass by value)
// - 1 double for inches (pass by value)
// - 1 double for meters (pass by value)
// Returns: <nothing>
void displayResults(int feet, double inches, double meters);

// ********************************************


int main()
{
// Declare program variables.
int feet;
double inches, meters;


// TODO: Call function to get feet input.
// An int value is returned - assign to the correct variable
feet = getFeet();

// TODO: Call function to get inches input.
// A double value is returned - assign to the correct variable
inches = getInches();

// TODO: Call function to convert feet and inches to meters.
// Pass the two arguments to the function
// Assign the returned value to the correct variable
meters = convertToMeters(feet,inches);

// TODO: Call function to print the results.
// Pass the correct three arguments to the function
// Nothing is returned, so do NOT provide a variable assignment
displayResults(feet,inches,meters);

cout << "End program - ";
return 0;
}
// ********************************************
// TODO: Define getFeet()
// Refer to the function prototype above for parameter and return type info
//
// Repeat
// - Prompt the user to enter feet
// - Read the user's keyboard response
// Until a value of 0 or more is entered
//
// Note: You will need to declare a local variable inside the function to hold
// the user's entry for feet (be sure to use correct data type)
int getFeet()
{
    int feet;
    while(true)
    {
        cout << "Enter feet: ";
        cin >> feet;

        if(feet >= 0)
            break;
    }

    return feet;
}

// TODO: Define getInches()
// Refer to the function prototype above for parameter and return type info
//
// Repeat
// - Prompt the user to enter inches
// - Read the user's keyboard response
// Until a value of 0 or more and less than 12 is entered
//
// Note: You will need to declare a local variable inside the function to hold
// the user's entry for inches (be sure to use correct data type)
double getInches()
{
   double inches;
   while(true)
   {
       cout << "Enter inches: ";
       cin >> inches;

       if(inches >= 0 && inches < 12)
           break;
   }

   return inches;
}


// TODO: Define convertToMeters()
// Refer to the function prototype above for parameter and return type info
//
// - Declare a new local variable called fractionalFeet that is type double
// - Type cast feet to be a double and assign to the fractionalFeet variable
// - On the same line where you are assigning the converted feet value to
// - fractionalFeet, also convert inches to feet (inches / 12) and add to the feet
// - Finally, return the result of multiplying fractionalFeet by FEET_TO_METERS_FACTOR

double convertToMeters(int feet,double inches)
{
   return FEET_TO_METERS_FACTOR*(inches/12) + FEET_TO_METERS_FACTOR*feet;
}


// TOTO: Define displayResults()
// Output statement to format results as shown
// using values passed to parameters:
// e.g.
//
// 10'-6.25" = 3.20757 meter(s).
//
// Use string literals along with value parameters:
// "'"
// "" = "
// " meter(s)."

void displayResults(int feet, double inches, double meters)
{
   cout << feet << "'-"<<inches<< "" = " << meters << " meter(s) ";
}


/* Sample Output
Sample Run #1: <this line not in output>
===================== <this line not in output>
Enter feet: 5
Enter inches: 10.25
5'-10.25" = 1.78481 meter(s).
End program - Press any key to continue . . .
Sample Run #2: <this line not in output>
===================== <this line not in output>
Enter feet: -5
Enter feet: 3
Enter inches: -5
Enter inches: 15.75
Enter inches: 5.75
3'-5.75" = 1.06072 meter(s).
End program - Press any key to continue . . .
*/