Hello, If you could help me using C programming, that would be awesome! Write a
ID: 3630008 • Letter: H
Question
Hello,
If you could help me using C programming, that would be awesome!
Write a program that takes the length and width of a rectangular yard and the length
and width of a rectangular house situated in the yard. Your program should compute the time required to cut the grass at the rate of one square meter per second. The lengths and widths are expressed in meters and are integers. Your program should interact with the user in exactly this manner:
Enter length and width of the yard in meters => 100 95
Enter length and width of the house in meters => 25 22
Cutting 8950 square meters.
Total time: 149 minutes and 10 seconds
Explanation / Answer
#include <stdio.h>
int main (int argc, const char * argv[]) {
int houseWidth, houseLength, yardWidth, yardLength, squareMetersToBeMowed;
int minutes;
float timeToMowF, seconds;
printf("Enter length and width of the yard in meters => ");
scanf(" %d %d", &yardWidth, &yardLength);
printf("Enter length and width of the house in meters => ");
scanf(" %d %d", &houseWidth, &houseLength);
squareMetersToBeMowed = (yardWidth*yardLength) - (houseWidth*houseLength);
printf("Cutting %d square meters. ", squareMetersToBeMowed);
minutes = squareMetersToBeMowed/60;
timeToMowF = (float)(squareMetersToBeMowed)/(60);
seconds = (timeToMowF-minutes)*60;
printf("Total time: %d minutes and %d seconds", minutes, (int) (seconds+.5));
return 0;
}
Just had to divide the total number of square yards to be mowed by 60 to get the minutes. Then had to get the fraction from the number of square meters for the yard/square meters for house and then multiply that fraction by 60 to get the seconds. When printing the seconds I cast it to an integer and add .5 to it. That seems like a decent way to ensure the number being cast to an integer will be rounded appropriately. There are better ways to do so. But that was my solution here. Hope it helps!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.