#include /* Michael A. Redmond */ /* PA State Police fine calculation program */ double calcMPH (double miles, double minutes) { const int minPerHour = 60; double mph; mph = (miles / minutes) * minPerHour; return mph; } double calcFine (double mph, int speedLimit) { double overLimit; if (mph <= speedLimit) { return 0; } overLimit = mph - speedLimit; if (overLimit <= 5.0001) { return 20; } if (overLimit <= 10.0001) { return (20 + (2 * overLimit)); } if (overLimit <= 20.0001) { return (20 + (4 * overLimit)); } else { return (50 + (5 * overLimit)); } } void printBill(double miles,double minutes,double MPH,int speedLimit,double Fine) { printf("\n"); printf("===========================< CUT HERE >===============================\n"); printf(" Bill\n"); printf("\n"); printf(" You traveled %6.1f miles in %6.1f minutes \n",miles,minutes); printf(" - an average speed of %5.1f miles per hour\n",MPH); printf(" This compares to a speed limit of %2d \n",speedLimit); printf(" The fine of %7.2f has been charged to your account \n",Fine); printf("\n"); printf("===========================< CUT HERE >===============================\n"); printf("\n"); } int main() { int numCars; int cnt; double minutes; double miles; double currMPH; double currFine; int speedLimit; double totalMPH = 0.0; int numSpeeding = 0; double totalFines = 0.0; double percSpeeding; double aveFinePerSpeeding; double aveFinePerCar; double aveMPH; printf("How many cars will be logged in this batch? "); scanf("%d",&numCars); printf("\n"); while (numCars <= 0) { printf("Please enter a positive number of cars"); scanf("%d",&numCars); } /* loop through user specified number of cars */ for (cnt = 0;cnt < numCars; cnt++) { printf("Car %2d \n",(cnt + 1)); printf("Time on Turnpike (in minutes): "); scanf("%lf",&minutes); while (minutes <= 0) { printf("\nPlease enter a positive number of minutes"); scanf("%lf",&minutes); } printf("Miles on Turnpike: "); scanf("%lf",&miles); while (miles <= 0) { printf("\nPlease enter a positive number of miles"); scanf("%lf",&miles); } printf("Speed Limit on Turnpike: "); scanf("%d",&speedLimit); while ((speedLimit < 35) || (speedLimit > 65)) { printf("\nPlease enter a speed limit between 35 and 65"); scanf("%d",&speedLimit); } printf("\n"); currMPH = calcMPH(miles,minutes); totalMPH += currMPH; printf("\nSpeed: %5.1f \n",currMPH); currFine = calcFine(currMPH,speedLimit); if (currFine > 0.0001) { printBill(miles,minutes,currMPH,speedLimit,currFine); numSpeeding++; totalFines += currFine; } } // end for loop percSpeeding = (numSpeeding / (1.0 * numCars)) * 100.0; if (numSpeeding > 0) { aveFinePerSpeeding = totalFines / numSpeeding; } else { aveFinePerSpeeding = 0; } aveFinePerCar = totalFines / numCars; aveMPH = totalMPH / numCars; // final report for batch of cars printf("\n"); printf("\n\n Final Report\n"); printf(" ------------\n"); printf("\n"); printf(" In a batch of %2d cars %2d were speeding \n",numCars,numSpeeding); printf("\n"); printf(" This represents %6.2f percent of cars \n",percSpeeding); printf("\n"); printf(" The total fines charged were %8.2f \n",totalFines); printf(" This represents an average per speeding car of %6.2f \n",aveFinePerSpeeding); printf(" OR an average for all cars of %6.2f \n",aveFinePerCar); printf("\n The average speed of all cars was %6.2f miles per hour \n",aveMPH); printf("\n"); printf("Goodbye \n"); return 0; }