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:5 | ISBN:9780132846813 | Edition: 5

Question

Write a program that asks for the user’s height, weight, and age, and then computes clothing sizes according to the following formulas.

• Hat size = weight in pounds divided by height in inches and all that multiplied by 2.9.

• Jacket size (chest in inches) = height times weight divided by 288 and then adjusted by adding one-eighth of an inch for each 10 years over age 30. (Note that the adjustment only takes place after a full 10 years. So, there is no adjustment for ages 30 through 39, but one-eighth of an inch is added for age 40.)

• Waist in inches = weight divided by 5.7 and then adjusted by adding one-tenth of an inch for each 2 years over age 28. (Note that the adjustment only takes place after a full 2 years. So, there is no adjustment for age 29, but one-tenth of an inch is added for age 30.)

Use functions for each calculation. Your program should allow the user to repeat this calculation as often as he or she wishes.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program code:

#include <iostream>
using namespace std;

// Function to calculate the hat size
double calculateHatSize(double weight, double height) {
    return (weight / height) * 2.9;
}

// Function to calculate the jacket size
double calculateJacketSize(double height, double weight, int age) {
    double jacketSize = (height * weight) / 288;
    int ageDifference = age - 30;
    if (ageDifference > 0) {
        int adjustment = ageDifference / 10;
        jacketSize += (adjustment * 0.125);
    }
    return jacketSize;
}

// Function to calculate the waist size
double calculateWaistSize(double weight, int age) {
    double waistSize = weight / 5.7;
    int ageDifference = age - 28;
    if (ageDifference > 0) {
        int adjustment = ageDifference / 2;
        waistSize += (adjustment * 0.1);
    }
    return waistSize;
}

int main() {
    char choice;
    do {
        double height, weight;
        int age;

        // Input the user's height, weight, and age
        cout << "Enter your height in inches: ";
        cin >> height;
        cout << "Enter your weight in pounds: ";
        cin >> weight;
        cout << "Enter your age: ";
        cin >> age;

        // Calculate the clothing sizes
        double hatSize = calculateHatSize(weight, height);
        double jacketSize = calculateJacketSize(height, weight, age);
        double waistSize = calculateWaistSize(weight, age);

        // Output the calculated sizes
        cout << "Your calculated sizes are:\n";
        cout << "Hat size: " << hatSize << endl;
        cout << "Jacket size (chest in inches): " << jacketSize << endl;
        cout << "Waist size in inches: " << waistSize << endl;

        // Ask if the user wants to repeat the calculation
        cout << "Do you want to calculate again? (y/n): ";
        cin >> choice;
    } while (choice == 'y' || choice == 'Y');

    cout << "Program ended. Goodbye!\n";
    return 0;
}

Executed Output:

Enter your height in inches: 69
Enter your weight in pounds: 194
Enter your age: 40
Your calculated sizes are:
Hat size: 8.15362
Jacket size (chest in inches): 46.6042
Waist size in inches: 34.6351
Do you want to calculate again? (y/n): n 
Program ended. Goodbye!

 

0 0

Discussions

Post the discussion to improve the above solution.