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 ,julia Lobur
Chapter:
Functions For All Subtasks
Exercise:
Programming Projects
Question:7 | ISBN:9780321531346 | Edition: 7

Question

Write a program like that of the previous exercise that converts from meters and centimeters into feet

and inches. Use functions for the subtasks.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header section
#include <iostream>
using namespace std;
// Function to convert meters and centimeters to feet
double convertToFeet(int meters, int centimeters) 
{
    double totalInches = (meters * 100 + centimeters) / 2.54; // Convert to total inches
    double feet = totalInches / 12; // Convert to feet
    return feet;
}

// Function to convert meters and centimeters to inches
double convertToInches(int meters, int centimeters) {
    double totalInches = (meters * 100 + centimeters) / 2.54; // Convert to total inches
    return totalInches;
}

// Function to convert feet to inches
double convertFeetToInches(double feet) {
    double inches = feet * 12;
    return inches;
}

// Function to display the conversion result
void displayResult(int meters, int centimeters, double feet, double inches) {
    cout << "The length " << meters << " meters and " << centimeters << " centimeters is equivalent to:\n";
    cout << "Feet: " << feet << " ft\n";
    cout << "Inches: " << inches << " in\n";
}

int main() {
    int meters, centimeters;
    
    // Get input from the user
    cout << "Enter the length in meters: ";
    cin >> meters;
    cout << "Enter the length in centimeters: ";
    cin >> centimeters;
    
    // Convert meters and centimeters to feet and inches
    double feet = convertToFeet(meters, centimeters);
    double inches = convertToInches(meters, centimeters);
    
    // Display the result
    displayResult(meters, centimeters, feet, inches);
    
    return 0;
}

OUTPUT OF THE PROGRAM CODE:

Enter the length in meters: 5
Enter the length in centimeters: 500 
The length 5 meters and 500 centimeters is equivalent to:
Feet: 32.8084 ft
Inches: 393.701 in

 

0 0

Discussions

Post the discussion to improve the above solution.