/* Assignment 8: Pay for Music System */ /* Author: Jennifer G. Lucas jglucas02@yahoo.com */ /* Date: 03/22/01 */ /* Writing Functions (and previous material) */ #include /* function (header) to get valid data from user */ /* set parameters */ double GetValid(double min, double max){ /* declare variables */ double number; /* get info from user */ printf("Please Enter A Number Between %4.2f and %4.2f:", min, max); scanf("%lf", &number); /* make sure user enters a correct number */ while ((number < min) || (number > max)){ printf("Please Enter A Number Between %4.2f and %4.2f:", min, max); scanf("%lf", &number); } /* return the number */ return number; } /* function (header) to calculate the cost for current month */ /* set parameters */ double MonthlyCost(double downloads, double minutes){ /* declare variables */ const double baseCost = 10; const double minCost = 0.01; double cost; /* calculate the monthly cost if downloads are greater than or equal to five */ if (downloads >= 5){ cost = 15 + 2*(downloads - 5) + baseCost + (minCost * minutes); } /* calculate monthly cost if downloads are less than five */ else { cost = (downloads * 3) + baseCost + (minCost * minutes); } /* return the cost */ return cost; } int main (){ /* declaration of variables */ int count; double averageCost; double total; int numberCust; double songs; double connectMins; double totalSongs; double totalMins; double totalCost; double songAvg; double minAvg; double costAvg; double lastAvg; /* display Welcome Greeting */ printf("Welcome to the Pay For Music System.\n"); /* get the number of people to be processed */ printf("How many people will be logged in this batch?"); scanf("%d", &numberCust); /* initialization of totals */ totalSongs = 0; totalMins = 0; totalCost = 0; /* get valid downloads and valid minutes from each person */ for (count = 1; count <= numberCust; count++){ /* display persons in order */ printf("\nPerson %d", count); /* get valid number of downloads */ printf("\nNumber of Downloads This Month "); songs = GetValid(0, 1000); /* calculate total number of songs */ totalSongs = totalSongs + songs; /* get valid number of minutes */ printf("\nNumber of Minutes Connected "); connectMins = GetValid(0, 40000); /* calculate total number of minutes */ totalMins = totalMins + connectMins; /* display the monthly cost */ total = MonthlyCost(songs, connectMins); printf("\nCost: %5.2f", total); /* calculate total cost */ totalCost = totalCost + total; /* display average cost per song */ averageCost = total/songs; printf("\nAve Cost Per Song: %5.2f", averageCost); /* end of "for" loop */ } /* calculate final averages for songs, minutes, monthly cost, and cost per song */ songAvg = totalSongs/numberCust; minAvg = totalMins/numberCust; costAvg = totalCost/numberCust; lastAvg = costAvg/songAvg; /* display final results */ printf("\n Final Report \n"); printf(" ------------ \n"); printf(" Customers Songs Minutes Cost\n"); printf(" ========= ===== ======= ====\n"); printf("Total %d %6.2f %6.2f %6.2f\n", numberCust, totalSongs, totalMins, totalCost); printf("Average %6.2f %6.2f %6.2f\n", songAvg, minAvg, costAvg); printf("\nAverage Cost Per Song: %6.2f\n", lastAvg); printf("\nGoodbye\n"); return 0; /* end of program */ }