Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Back in the days of \'long distance,\" phone calls beyond your local community w

ID: 3670283 • Letter: B

Question

Back in the days of 'long distance," phone calls beyond your local community were billed by the minute, with different rates depending on day of the week and the time of the day that the call was made. (The phone lines were less in use on weekends and at night, when businesses were not using them as extensively.) This program computes a person's phone bill based on this type of calling. Write a C program that computes the cost of a long-distance call, total cost of all the calls made and cost of the most expensive call. The cost of the call is determined according to the following rate schedule: Any call started between 8:00 a.m. (800 hours) and 6:00 p.m. (1800 hours) during the weekdays is billed at the rate of $0.20 per minute. Any call started after 6:00 p.m. (1800 hours) until 11:00 p.m. (2300 hours) during the weekdays is billed at the rate of $ 0.1S per minute. Any call made during the weekend (Saturday and Sunday) and after 11:00 p.m. and before 8:00 a.m. every day is billed at the rate of $0.10 per minute. The input will consist of the day ('d' for weekday or 'e' for weekend), the time call started, and the length of the call in minutes. The time is to be input in 24-hour notation. The day of the week will be read as a char. The time and the length will be integer values. You do not have to worry about a call going from one rate to another. For example if the call started at 745 and the length of the call is 30 minutes, your entire call will be calculated at the rate of the time call started. Use "define to declare the rates as constants. Use loops and if statements in the main of your program You are not required to use functions, but you can if you want. Remember to initialize the sum and the most expensive call to appropriate values. Sample output of the program Enter the day weekday or weekend d Enter the time the call started 900 Enter the length of the call 20 weekday 20 minute call started at 900 hrs will cost S 4.00 Do you have another y/n y Enter the day weekday or weekend 0 Enter the time the call started 100 Enter the length of the call 20 weekend 20 minute call started at 100 hrs will cost S 2.00

Explanation / Answer

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

void main()
{
   int             i, j, k, calls, startTime = 0, len;
   char            day[10];
   float           cost[10], maxCost = 0.0, minCost = 0.0, length;

   printf(" Enter the number of calls, no more than 10: ");
   scanf("%d", &calls);

   for (j = 1; j <= calls; j++) {
       printf(" Enter the start of call %d and the length ", j);
       printf("in minutes: ");
       scanf("%d %f", &startTime, &length);
       printf("Now enter the day of the week the call was made: ");
       scanf("%s", day);

       /* Make sure it's all lowercase */

       len = strlen(day);
       for (i = 0; i < len; i++) {
           day[i] = tolower(day[i]);
       }

       /* switch() to the day and perform the calculations */

       switch (day[0]) {
       case 's':   /* day[0] == s, must be weekend */
           cost[j] = (.20 * length) - ((.20 * length) * .15);
           break;

       case 'm':
       case 't':
       case 'w':
       case 'f':
           if (startTime >= 800 && startTime <= 1700)   /* full price */
               cost[j] = .20 * length;
           if (startTime > 1700 && startTime < 2300)   /* 10% off */
               cost[j] = (.20 * length) - ((.20 * length) * .10);
           if (startTime >= 2300 || startTime < 800)   /* 15% off */
               cost[j] = (.20 * length) - ((.20 * length) * .15);
           break;

       default:
           printf(" Invalid input. ");
           break;
       }       /* end switch */

       if (cost[j] > maxCost)
           maxCost = cost[j];
       if (j == 1)
           minCost = maxCost;
       else if (cost[j] <= minCost)
           minCost = cost[j];

       printf(" Cost for call %d is %.2f. ", j, cost[j]);
   }           /* end for */
   printf("The most expensive call cost %.2f. ", maxCost);
   printf("The least expensive call cost %.2f. ", minCost);
}


output

Enter the number of calls, no more than 10: 2                                                                                                               
                                                                                                                                                            
Enter the start of call 1 and the length in minutes: 1 20                                                                                                   
Now enter the day of the week the call was made: monday                                                                                                     
                                                                                                                                                            
Cost for call 1 is 3.40.                                                                                                                                    
                                                                                                                                                            
Enter the start of call 2 and the length in minutes: 900                                                                                                    
20                                                                                                                                                          
Now enter the day of the week the call was made: wednesday                                                                                                  
                                                                                                                                                            
Cost for call 2 is 4.00.                                                                                                                                    
The most expensive call cost 4.00.                                                                                                                          
The least expensive call cost 3.40.