What is wrong with my program?

My program is running fine until the very end where I need the time. My professor wants the time to be in hours and minutes (ex: 4 hours and 15 minutes). When my program gets to the time part I get really big numbers and negative numbers... Please help!


#include<stdio.h>
#include<math.h>

int
main(void)

{
double lawnwidth, lawnlength, lawnarea, housewidth, houselength,
housearea, drivewidth, drivelength, drivearea, circled, circlearea,
PI, totalarea, rad, timeoutput, hours, minutes;
int time;

PI = 3.14159265;

printf("Enter the width of the lawn. \n");
scanf_s("%lf", &lawnwidth);

printf("Enter the length of the lawn. \n");
scanf_s("%lf", &lawnlength);

printf("Enter the width of the house. \n");
scanf_s("%lf", &housewidth);

printf("Enter the length of the house. \n");
scanf_s("%lf", &houselength);

printf("Enter the width of the driveway. \n");
scanf_s("%lf", &drivewidth);

printf("Enter the length of the driveway. \n");
scanf_s("%lf", &drivelength);

printf("Enter the diameter of the circle. \n");
scanf_s("%lf", &circled);

lawnarea = lawnwidth * lawnlength;

housearea = housewidth * houselength;

drivearea = drivewidth * drivelength;

rad = circled/2;
circlearea = pow(rad,2) * PI;

totalarea = lawnarea - (housearea + drivearea + circlearea);
printf("The total lawn area needed cut is %.2f sqft. \n", totalarea);

timeoutput = totalarea/200;
time = timeoutput;
hours = timeoutput / 60;
minutes = time % 60;
printf("Total time to mow the lawn:\n Hours: %d\n Minutes: %d\n", hours, minutes);

return(0);
}
Last edited on
Sorry, I thought putting the code into [code] brackets would let me run the online compiler. Guess not. Could you post some example numbers that you've put in for the widths, lengths, etc.?
Last edited on
closed account (48T7M4Gy)
What you need to do is write down a few test samples. Work out the test results manually and compare to the values generated by your program. Show us the output.

minutes = time % 60; --- time is double, minutes is int, % is integer division
250, 150, 75, 40, 12, 50, 10. Those are my input numbers and my output I get for my total area is 33821.46, and then that should be divided by 200 to get my minutes (200 sqft covered each minute). My hour output and minutes output are end up being very large numbers like 79384340 and also sometimes negative numbers.
I'm not sure if this will change anything, but hours and minutes should both be integer values. The reason for this is that since you are giving the amount of minutes, there's no reason to have a non-whole number of hours. The reason minutes should be integer is that modular arithmetic always returns an integer value. So if timeoutput = 169.105 (using the area you've given), then hours should equal 2. If timeoutput = 169.105, time = 169, so minutes = 169%60 should give 49. The output should then say that the lawn took 2 hours and 49 minutes to mow.
closed account (48T7M4Gy)
Exactly hyperfine.

Try: hours = (int) timeoutput/60; Oops this depends on whether timeoutput is in hours or minutes

then work from there to get the minutes.
Last edited on
It worked! Thank You!!
Topic archived. No new replies allowed.