Here is my first example of a program with functions (see attachment). Try it ou
ID: 441054 • Letter: H
Question
Here is my first example of a program with functions (see attachment). Try it out.
1. What does it do?
2. What functions does it have?
3. How do the functions exchange data in order to solve the problem?
// Example 1:
// ---------
#include <iostream>
#include <string>
using namespace std;
// function prototypes
double imperialToMetric (int feet, int inches); // converts from feet and inches to cm
int readNonNegInt(string prompt); // reads and validates non-negative integer value
int main ()
{
int feet, inches; // length in feet and inches
double cm; // same length in centimeters
// read and validate feet
feet = readNonNegInt("Enter value for feet: ");
// read and validate inches
inches = readNonNegInt("Enter value for inches: ");
// convert feet and inches to cm
cm = imperialToMetric(feet, inches); // call function, passing arguments
// display result
cout << "This length is " << cm << " centimeters" << endl;
// delay program termination
cout << "Enter any key to finish" << endl;
cin.ignore(2);
return 0;
} // end main
Explanation / Answer
1. It reads in valid feet and inches before converting them to cm. The result is printed out to console. 2. imperialToMetric and readNonNegInt 3. imperialToMetric takes two integers passed by value and returns a double. readNonNegInt takes in one string passed by value and returns an integer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.