Hi, I have an assignment to create a code that tracks the amount of trips that can be taken on a set of tires before they wear out. For now, my input is 8 miles and 14 inch radius for the wheels. The correct output is supposed to be 607 trips, yet I am receiving 557 trips. I've looked at the calculations, but I'm not sure of what I'm doing wrong. My output is:
How far is a one-way trip in miles from your house to the Slate Rock and Gravel Quarry? 8
What size tires would you like to purchase (12, 14, or 16-inch radius)? 14
The distance you entered for a round-trip, in inches, is 1013760
The circumference of the tire you selected is 87.9646
The amount of total trips you can make is 557 trips.
#include <iostream>
#include <cmath>
usingnamespace std;
constexprint inches_per_mile{63360};
constexprdouble rate_of_wear{0.0000005};
int miles_to_inches(int miles)
{
int inches;
inches = miles * inches_per_mile * 2;
return inches;
}
double radius_circumference(int radius)
{
double circumference;
circumference = 2 * M_PI * radius;
return circumference;
}
int num_trips(int circumference, int inches)
{
int numOfTrips, tireRadius, trips, numOfRotations;
double radiusWear, wearPerTrip;
numOfRotations = inches / circumference;
wearPerTrip = numOfRotations * rate_of_wear;
tireRadius = circumference / (2 * M_PI);
radiusWear = tireRadius * .25;
numOfTrips = radiusWear / wearPerTrip;
return numOfTrips;
}
int main ()
{
int miles, inches, tireRadius, totalTrips, trips;
double circumference;
cout << "How far is a one-way trip in miles from your house to the Slate Rock and Gravel Quarry? ";
cin >> miles;
cout << endl;
cout << "What size tires would you like to purchase (12, 14, or 16-inch radius)? ";
cin >> tireRadius;
cout << endl;
inches = miles_to_inches(miles);
circumference = radius_circumference(tireRadius);
totalTrips = num_trips(circumference, inches);
cout << "The distance you entered for a round-trip, in inches, is " << inches;
cout << endl;
cout << "The circumference of the tire you selected is " << circumference;
cout << endl;
cout << "The amount of total trips you can make is " << totalTrips << " trips. ";
cout << endl;
return 0;
}
most likely if its not what you expect, you have messed up mixing integers and doubles, and some piece of math has truncated a decimal that is messing up the result. should numOfRotations be a double? It is assigned a division result.
lets see... there are 5280 feet in a mile. there are 12 inches per foot. that is 63360, * 8 = 506880 inches in an 8 mile ride.
why does have *2 in inches per mile? round trip? If so, its right. I see what you did there, but its not well named (its not inches to mile conversion, its doing something else).
tire-radius could be corrupted from int/double. When I run it with your example input and print tire-radius in the function, at line 30, I get 13 instead of 14 due to floating point round off... !!