SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Function Basics
Exercise:
Programming Projects
Question:10 | ISBN:9780132846813 | Edition: 5

Question

One way to estimate the height of a child is to use the following formula, which uses the height of the parents:

Hmale_child = ((Hmother 13 / 12) + Hfather) / 2

Hfemale_child = ((Hfather 12 / 13) + Hmother) / 2

ll heights are in inches. Write a function that takes as input parameters the gender of the child, height of the mother in inches, and height of the father in inches, and outputs the estimated height of the child in inches. Embed your function in a program that allows you to test the function over and over again until telling the program to exit. The user should be able to input the heights in feet and inches, and the program should output the estimated height of the child in feet and inches. Use the integer data type to store the heights.



TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header section
#include <iostream>
using namespace std;

// Function to estimate the height of a child
int estimateChildHeight(char gender, int motherHeight, int fatherHeight) {
    int childHeight;
    
    // Calculate the estimated height based on the gender of the child
    if (gender == 'M') {
        childHeight = ((motherHeight * 13 / 12) + fatherHeight) / 2;
    } else if (gender == 'F') {
        childHeight = ((fatherHeight * 12 / 13) + motherHeight) / 2;
    } else {
        // Invalid gender input
        cout << "Invalid gender input. Please enter 'M' for male or 'F' for female." << endl;
        return 0;
    }
    
    return childHeight;
}

// Function to convert inches to feet and inches
void convertToFeetAndInches(int inches, int& feet, int& remainingInches) {
    feet = inches / 12;
    remainingInches = inches % 12;
}

int main() {
    char gender;
    int motherHeightFeet, motherHeightInches;
    int fatherHeightFeet, fatherHeightInches;
    
    while (true) {
        // Get the inputs from the user
        cout << "Enter the gender of the child (M-MALE/F-FEMALE/E-EXIT): ";
        cin >> gender;
        
        // Check if the user wants to exit
        if (gender == 'E') {
            cout << "Exiting the program..." << endl;
            break;
        }
        
        cout << "Enter the height of the mother (feet inches): ";
        cin >> motherHeightFeet >> motherHeightInches;
        
        cout << "Enter the height of the father (feet inches): ";
        cin >> fatherHeightFeet >> fatherHeightInches;
        
        // Convert heights to inches
        int motherHeight = motherHeightFeet * 12 + motherHeightInches;
        int fatherHeight = fatherHeightFeet * 12 + fatherHeightInches;
        
        // Estimate the height of the child
        int childHeight = estimateChildHeight(gender, motherHeight, fatherHeight);
        
        // Convert child height to feet and inches
        int childHeightFeet, childHeightInches;
        convertToFeetAndInches(childHeight, childHeightFeet, childHeightInches);
        
        // Display the estimated height of the child
        cout << "Estimated height of the child: " << childHeightFeet << " feet " << childHeightInches << " inches" << endl;
        cout << "----------------------------------------------" << endl;
    }
    
    return 0;
}

OUTPUT OF THE PROGRAM CODE:

Enter the gender of the child (M-MALE/F-FEMALE/E-EXIT): M
Enter the height of the mother (feet inches): 5 1
Enter the height of the father (feet inches): 6 1
Estimated height of the child: 5 feet 9 inches
----------------------------------------------
Enter the gender of the child (M-MALE/F-FEMALE/E-EXIT): F
Enter the height of the mother (feet inches): 5 0
Enter the height of the father (feet inches): 6 0
Estimated height of the child: 5 feet 3 inches
----------------------------------------------
Enter the gender of the child (M-MALE/F-FEMALE/E-EXIT): E
Exiting the program...

 

0 0

Discussions

Post the discussion to improve the above solution.