Question

In: Computer Science

Write a program to compute internet charges according to a rate schedule. The rate schedule is...

Write a program to compute internet charges according to a rate schedule.

The rate schedule is as follows:

$0.08 per GB for usage between 0 and 40 GB, inclusive

$0.07 per GB for usage between 41 GB and 70 GB, inclusive

$0.05 per GB for usage between 71 GB and 110 GB, inclusive

$0.04 per GB for usage greater than 110 GB

Learning Objectives

In this assignment, you will:

  • Use a selection control structure
  • Use a repetition control structure
  • Use functions with input arguments, output arguments, and return values
  • Display neatly formatted output to the screen

Requirements

Your code must use these four functions, using these names (in addition to main):

  1. getData
  2. computeCharges
  3. printAccountInfo
  4. printTotals

Requirements for the getData Function

Purpose:

This function prompts the user for an account number (integer) and a GB value (integer). It passes these values back to main via two output parameters.

Output Parameters:

  1. account number (as an integer)
  2. GB value (as an integer)

Algorithm:

Prompt and read. There must be only one prompt, and the user must enter the data on one line with a space between each value.

Return value:

None

Requirements for the computeCharges Function

Purpose:

This function computes the charges for one transaction. It uses one input parameter (GB value as an integer) and a return value (amount of the charge, which may include decimals).

Input Parameter:

  1. GB value (as an integer)

Algorithm:

Use a selection structure to determine the charge per GB, using the above rate schedule. Calculate the charges using multiplication.

Return value:

Transaction charges (may include decimals)

Requirements for the printAccountInfo Function

Purpose:

This function displays the information for one account transaction to the screen.

Input Parameters:

  1. account number
  2. GB value
  3. Transaction charges

Algorithm:

Print, using appropriate spacing and formatting. The transaction charges must display with two decimal places.

Return value:

None

Requirements for the printTotals Function

Purpose:

This function displays totals to the screen at the end of the program.

Input Parameters:

  1. Total number of accounts
  2. Sum of all GB values
  3. Sum of all transaction charges

Algorithm:

Print, using appropriate spacing and formatting. The sum of transaction charges must display with two decimal places.

Return value:

None

Requirements for main:

  1. Variable declarations in main should only include the variables needed within main. Do not declare all the variables needed in the program here – only the ones needed within main.
  2. The main function must use a conditional loop that prompts the user to enter a Y or an N to indicate if they wish to continue adding more input. Your code must be able to handle lowercase or uppercase: y, Y, n, or N.

Be sure the first prompt is for actual data, not the continuation response.

IMPORTANT NOTE ABOUT THIS LOOP – You will encounter the situation where there is a scanf for a number immediately before the scanf for the Y or N. We have seen this problem before…

  1. The loop body calls the appropriate functions with the appropriate arguments and return values, if necessary. Within the loop, you must keep track of the totals needed by the printTotals function.
  2. When the loop is complete, call the print_totals function with the appropriate arguments.
  3. All prompts and output must display exactly as demonstrated in the sample runs below. Pay attention to spacing and alignment!

Sample Run

Enter account number and GB used (one space between data): 12345 80
Account number: 12345        GB Used:     80        Charge:     4.00

Do you wish to continue? (y/n) y

Enter account number and GB used (one space between data): 98765 25
Account number: 98765        GB Used:     25        Charge:     2.00

Do you wish to continue? (y/n) Y

Enter account number and GB used (one space between data): 25413 120
Account number: 25413        GB Used:    120        Charge:     4.80

Do you wish to continue? (y/n) Y

Enter account number and GB used (one space between data): 42598 50
Account number: 42598        GB Used:     50        Charge:     3.50

Do you wish to continue? (y/n) n

Total accounts =        4
Total GB Used =       275
Total Charges =     14.30

Solutions

Expert Solution

In case of any queries,please comment. I would be very happy to assist all your queries.Please give a Thumps up if you like the answer.

Program

#include<stdio.h>
void getData(int *acct_num,int *gb);
void printAccountInfo(int *acct_num,int *gb,float *t_charge);
float computeCharges(int *gb);
void printTotals(int *num_accounts,int *total_gb,float *total_charge);

int main()
{
   int acct_num,gb,total_gb=0,num_accounts=0;
   float t_charge,total_charge=0;
   char ch;

   do
   {
   getData(&acct_num,&gb);
   total_gb=gb+total_gb;
   num_accounts++;
   t_charge=computeCharges(&gb);
   total_charge+=t_charge;
   printAccountInfo(&acct_num,&gb,&t_charge);
   printf ("Do you want to continue: y/n ");
   scanf (" %c", &ch);
   } while(ch == 'y' || ch == 'Y');
   printTotals(&num_accounts,&total_gb,&total_charge);
   return 0;
}

void getData(int *acct_num,int *gb)
{
   printf("Enter account number and GB used (one space between data): ");
   scanf("%d%d",acct_num,gb);
}

void printAccountInfo(int *acct_num,int *gb,float *t_charge)
{
   printf("Account number: %8d\tGB Used:%8d\tCharge:%8.2f\n",*acct_num,*gb,*t_charge);
}

float computeCharges(int *gb)
{
   float charge;
   if(*gb<=40 && *gb>=0)
       charge=*gb*.08;
   else if(*gb<=70 && *gb>=41)
       charge=*gb*.07;
   else if(*gb<=110 && *gb>=71)
       charge=*gb*.05;
   else if(*gb>110)
       charge=*gb*.04;
   return charge;
}

void printTotals(int *num_accounts,int *total_gb,float *total_charge)
{
   printf("Total accounts =%8d\n",*num_accounts);
   printf("Total GB Used =%8d\n",*total_gb);
   printf("Total Charges =%8.2f\n",*total_charge);


}

Output

Enter account number and GB used (one space between data): 12345 80
Account number: 12345   GB Used: 80   Charge: 4.00
Do you want to continue: y/n y
Enter account number and GB used (one space between data): 98765 25
Account number: 98765   GB Used: 25   Charge: 2.00
Do you want to continue: y/n Y
Enter account number and GB used (one space between data): 25413 120
Account number: 25413   GB Used: 120   Charge: 4.80
Do you want to continue: y/n Y
Enter account number and GB used (one space between data): 42598 50
Account number: 42598   GB Used: 50   Charge: 3.50
Do you want to continue: y/n n
Total accounts = 4
Total GB Used = 275
Total Charges = 14.30


Related Solutions

Write a program In C to compute internet charges according to a rate schedule. The rate...
Write a program In C to compute internet charges according to a rate schedule. The rate schedule is as follows: $0.08 per GB for usage between 0 and 40 GB, inclusive $0.07 per GB for usage between 41 GB and 70 GB, inclusive $0.05 per GB for usage between 71 GB and 110 GB, inclusive $0.04 per GB for usage greater than 110 GB Learning Objectives In this assignment, you will: Use a selection control structure Use a repetition control...
Subject is C++ Write a program to compute internet charges according to a rate schedule. The...
Subject is C++ Write a program to compute internet charges according to a rate schedule. The rate schedule is as follows: $0.08 per GB for usage between 0 and 40 GB, inclusive $0.07 per GB for usage between 41 GB and 70 GB, inclusive $0.05 per GB for usage between 71 GB and 110 GB, inclusive $0.04 per GB for usage greater than 110 GB code must use these four functions, using these names (in addition to main): getData computeCharges...
Program in C++ **********Write a program to compute the number of collisions required in a long...
Program in C++ **********Write a program to compute the number of collisions required in a long random sequence of insertions using linear probing, quadratic probing and double hashing. For simplicity, only integers will be hashed and the hash function h(x) = x % D where D is the size of the table (fixed size of 1001). The simulation should continue until the quadratic hashing fails.*********
write a Java program Write a program to assign a letter grade according to the following...
write a Java program Write a program to assign a letter grade according to the following scheme: A: total score >= 90 B: 80 <= total score < 90 C: 70 <= total score < 80 D: 60 <= total score < 70 F: total score < 60 Output - the student's total score (float, 2 decimals) and letter grade Testing - test your program with the following input data: test1 = 95; test2 = 80; final = 90; assignments...
Write a program to compute intersection of two sorted array of integers and compute the CPU...
Write a program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator. Test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers DONT FORGET CPU TIME FOR EACH ONE NO HASH SET
Branching Program write a program to do the following: A university needs to compute the tuition...
Branching Program write a program to do the following: A university needs to compute the tuition of its students. They will ask for the name, the number of units the student is taking, and their residency status. R=resident , N = non- resident. The tuition to be computed as follows: non-resident students pay $100 per unit; resident students pay $50 per unit if they take 12 or units or more and $75 per unit if they take less than 12...
write a java code Write a generic program to compute the sum of 30 terms of...
write a java code Write a generic program to compute the sum of 30 terms of the following series. (181 - 5)/31 + (186 + 9)/34 - (191 - 13)/37 + (196 + 17)/40 + . . . . . 1 5 11 Note: the 3rd, 6th, 9th term etc, has a negative sign in front of parenthesis. User Input: None Expected output: Term Ratio Sum 1 5.677 5.677 2 5.735 11.413 3 -4.811 6.602
in C++ Write a program to compute the current and the power dissipation in an AC...
in C++ Write a program to compute the current and the power dissipation in an AC circuit that has four resistors R1, R2, R3, and R4 in parallel. The voltage source is V. Test your solution with various voltage levels and resistor values. Execute and submit the program and the results in screen captures. Please note that the equivalent resistor is given by 1/Rtotal = 1/R1 + 1/R2 + 1/R3 + 1/R4 and the current I is given by I...
C program help 1. Write a program to compute the Mileage given by a vehicle. Mileage...
C program help 1. Write a program to compute the Mileage given by a vehicle. Mileage = (new_odometer – old_odometer)/(gallons_gas) // illustrating how ‘for’ loop works. 2. How to initialize an array of size 5 using an initializer list and to compute it’s sum How to initialize an array of size 5 with even numbers starting from 2 using ‘for’ loop and to compute it’s sum 3. Program to compute the car insurance premium for a person based on their...
C++ Overloaded Hospital Write a program that computes and displays the charges for a patient’s hospital...
C++ Overloaded Hospital Write a program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an in-patient or an out-patient. Please keep asking the user until the user enters the valid choice. Please refer to the test cases for more details. If the patient was an in-patient the following data should be entered: • The number of days spent in the hospital • The daily rate •...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT