Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

C++ code not producing correct mathematical responses.

+14 votes
asked Feb 17 by Antionette Calles (220 points)
Code is found below. The sums that I am getting incorrect should add up to the following:

You can mow 198 sq. ft. per min

Time to mow the lawn is 37.8788 minutes.

#include <iostream>
using namespace std;

// Constants for conversion
const double FEET_PER_MILE = 5280;
const double INCHES_PER_FOOT = 12;
const double MINUTES_PER_HOUR = 60;

// Function to convert miles per hour to feet per minute
double mph_to_fpm(double speed) {
    return speed * FEET_PER_MILE / MINUTES_PER_HOUR;
}

// Function to calculate area in square feet
double calculate_area(double length, double width) {
    return length * width;
}

// Function to calculate time in minutes to mow the lawn
double calculate_time(double area_to_mow, double mow_rate) {
    return area_to_mow / mow_rate;
}

int main() {
    double length_lawn, width_lawn, length_house, width_house, walking_speed;

    // Prompt user for input
    cout << "Mowing the lawn by Antionette Calles\n\n";
    cout << "Enter five numbers separated by blanks\n";
    cout << "They represent the length and width of lawn in feet,\n";
    cout << "the length and width of the house in feet,\n";
    cout << "and the walking speed in mph\n";
    cin >> length_lawn >> width_lawn >> length_house >> width_house >> walking_speed;

    // Echo input
    cout << "\nThe lawn is " << length_lawn << " by " << width_lawn << " feet" << endl;
    cout << "The house is " << length_house << " by " << width_house << " feet" << endl;
    cout << "Your walking speed is " << walking_speed << " mph" << endl;

    // Calculate areas
    double area_lawn = calculate_area(length_lawn, width_lawn);
    double area_house = calculate_area(length_house, width_house);
    double area_to_mow = area_lawn - area_house;

    // Print area to mow
    cout << "\nThe area to mow is " << area_to_mow << " square feet" << endl;

    // Calculate mow rate
    double mow_rate = mph_to_fpm(walking_speed) * (width_lawn / INCHES_PER_FOOT);

    // Print mow rate
    cout << "You can mow " << mow_rate << " sq. ft. per min" << endl;

    // Calculate time to mow
    double time_to_mow = calculate_time(area_to_mow, mow_rate);

    // Print time to mow
    cout << "Time to mow the lawn is " << time_to_mow << " minutes" << endl;

    return 0;
}

1 Answer

0 votes
answered Feb 28 by Peter Minarik (86,240 points)

I believe this is the right thing to do is

double mow_rate = mph_to_fpm(walking_speed) * (width_lawn * INCHES_PER_FOOT);

Let me know if there are any further issues. It also helps if you share what you expect the correct behaviour would be (I'm always confused with imperial measurements XD).

Good luck!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...