0

Driving cost - functions // need help with this question

Write a function DrivingCost with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to drive those miles. All items are of type double. If the function is called with 50 20.0 3.1599, the function returns 7.89975. Define that function in a program whose inputs are the car's miles/gallon and the gas dollars/gallon (both doubles). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your DrivingCost function three times. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: printf("%0.2lf", yourValue); Ex: If the input is: 20.0 3.1599 the output is: 1.58 7.90 63.20 Your program must define and call a function: double DrivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon) this is what I have done already : #include <stdio.h> double DrivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon) { double dollarCost=drivenMiles/milesPerGallon*dollarsPerGallon; return dollarCost; } int main() { double milesPerGallon=20.0, dollarsPerGallon=3.1599; double cost=DrivingCost(10,milesPerGallon,dollarsPerGallon); printf("%0.2lf ",cost); cost=DrivingCost(50,milesPerGallon,dollarsPerGallon); printf("%0.2lf ",cost); cost=DrivingCost(400,milesPerGallon,dollarsPerGallon); printf("%0.2lf\n",cost); return 0; }

5th Oct 2020, 5:44 PM
Manesh Divedia
Manesh Divedia - avatar
8 Answers
+ 1
LOL I GOT IT i had to type it in like ; scanf("%lf %lf", &milesPerGallon, &dollarsPerGallon);
5th Oct 2020, 6:12 PM
Manesh Divedia
Manesh Divedia - avatar
5th Oct 2020, 5:45 PM
Manesh Divedia
Manesh Divedia - avatar
0
Look at the second paragraph of your Description: "Define that function in a program whose inputs are the car's miles/gallon and the gas' dollar/gallon ..." Your code is required to ask for inputs for the car's MPG and gas' price, but you hardcoded those values. Try and see if reading inputs for those values help. The code is working as it should apart from this one thing.
5th Oct 2020, 5:58 PM
Ipang
0
so how would i change that?
5th Oct 2020, 6:01 PM
Manesh Divedia
Manesh Divedia - avatar
0
would i have to use scanf to ask the user to enter the values for these two variables. double milesPerGallon; double dollarsPerGallon;
5th Oct 2020, 6:01 PM
Manesh Divedia
Manesh Divedia - avatar
0
Try that, the imgur screenshot showed that your code was giving wrong outputs, but that was merely because your code doesn't read the values for those 2 variables ...
5th Oct 2020, 6:09 PM
Ipang
5th Oct 2020, 6:10 PM
Manesh Divedia
Manesh Divedia - avatar
0
LOL yeah man, that's what I'm talking about 👍
5th Oct 2020, 6:13 PM
Ipang