Directions Complete the following homework assignment using the description give
ID: 3757511 • Letter: D
Question
Directions
Complete the following homework assignment using the description given in each section.
Purpose
• Perform basic I/O operations using scanf and printf functions
• Use conditional statements to perform error check.
• Perform simple mathematical calculations.
Description
City of Columbia calculates the monthly Electricity utility bill based on the number of units consumed. It has divided the users into 3 categories, Residential, Commercial and Industrial. Based on the type of connection the rates vary. The bill depends on the number of units consumed, which is read in kWh (kilowatt-hours). The total bill consists of two parts. Part 1 is the Energy Charge, the table below shows the slab rates for consumption for each type of connection. Part 2 is the Connection Charge, the City of Columbia charges a fixed amount every month of $25 for Residential connection, $90 for Commercial connection and $850 if it is an Industrial connection.
Example: If Shakespeare’s Pizza, which comes under Commercial Connection, consumed 700kWh during the last month, based on the table below their rate is 14c / kWh.
Energy Charge= units consumed*(rate/100)
Energy Charge= 700*.14= $98
Total Bill = Energy Charge + Connection Charge
Total Bill= 98+90= $188
Develop a program in C that will input units consumed for the previous month and will calculate and display the bill for that particular connection as shown in the sample output below. To begin, read a connection type from the user: 1 for Residential, 2 for Commercial and 3 for Industrial. Assume that the user can enter an invalid input multiple times, so, prompt an appropriate error message until a valid connection is chosen. This error check is shown in the sample output below. Next, read units consumed for that connection, again, perform an error check. A unit is invalid if it is negative (less than zero). Here, again assume that the user can provide an invalid input for unit’s multiple times. Using the connection type, units consumed and the rate from the table below calculate the total bill for Electricity for the user (using the formula above). Make sure the program loops and asks if the user wants to continue to calculate another Bill. If the user hits 1 the calculator should start again for a new bill, if the user hits 0 the program should terminate by showing the proper output (It should show how many times the bill was calculated and what is the grand total of all the bills). Implement the bonus after this. Your results should match the sample output below. Use formatting to print the dollar amount correct to 2 decimal places only
lower bound
Functions to implement:
NOTE: Use of global variables will result in a 30-point deduction.
1. void displayMenu();
Prints the display options
2. int errorCheck(int option);
Error checking inputs for displayMenu(). Returns a 0 if invalid, and 1 is the input is valid.
3. int errorCheckUnits(int units);
Error checking for valid unit inputs. If unit input is less than 0, return 0. If valid, return 1.
4. float getRate(int units, int option);
Takes in the user’s units and menu option. Determines the rate (in cents) per kWH by looking at the given number of units. Returns the rate.
5. int charge(int option);
Takes in the user’s menu option. Based off the user’s option, determine the Conneciton Charge. Return the Connection Charge.
6. int main (void)
The main function will be doing all of the function calls.
Bonus: Take a positive number (N) as an input (perform error checking), print the numbers and calculate the product of all numbers from 1 to N, also product of all even numbers and product of all odd numbers from 1 to N. (use for-loop)
Eg: Number is 4
1 2 3 4
Product of all numbers is 24
Product of even numbers = 8
Product of odd numbers = 3
Sample Output
Character in bold are inputs from the user
***** ELECTRICITY BILL CALCULATOR *****
1. Residential
2. Commercial
3. Industrial
Choose the type of connection: 0
Invalid Choice! Please enter a valid choice
1. Residential
2. Commercial
3. Industrial
Choose the type of connection: 4
Invalid Choice! Please enter a valid choice
1. Residential
2. Commercial
3. Industrial
Choose the type of connection: 1
Enter the number of units (in kWh):-111
Invalid input!
Enter the number of units (in kWh):-10
Invalid input!
Enter the number of units (in kWh):300
Total energy charge for the customer is: $ 30.00
Conneciton charge for this customer is: $ 25.00
Total bill due from this connection is: $ 55.00
Do you want to continue and calculate another bill? If Yes enter 1, else 0:1
1. Residential
2. Commercial
3. Industrial
Choose the type of connection: 0
Invalid Choice! Please enter a valid choice
1. Residential
2. Commercial
3. Industrial
Choose the type of connection: 0
Invalid Choice! Please enter a valid choice
1. Residential
2. Commercial
3. Industrial
Choose the type of connection: 2
Enter the number of units (in kWh):-99
Invalid input!
Enter the number of units (in kWh):-99
Invalid input!
Enter the number of units (in kWh):400
Total energy charge for the customer is: $ 56.00
Conneciton charge for this customer is: $ 90.00
Total bill due from this connection is: $ 146.00
Do you want to continue and calculate another bill? If Yes enter 1, else 0:1
1. Residential
2. Commercial
3. Industrial
Choose the type of connection: 0
Invalid Choice! Please enter a valid choice
1. Residential
2. Commercial
3. Industrial
Choose the type of connection: 3
Enter the number of units (in kWh):-1
Invalid input!
Enter the number of units (in kWh):-500
Invalid input!
Enter the number of units (in kWh):37
Total energy charge for the customer is: $ 13.51
Conneciton charge for this customer is: $ 850.00
Total bill due from this connection is: $ 863.51
Do you want to continue and calculate another bill? If Yes enter 1, else 0:1
1. Residential
2. Commercial
3. Industrial
Choose the type of connection: 1
Enter the number of units (in kWh):37
Total energy charge for the customer is: $ 2.78
Conneciton charge for this customer is: $ 25.00
Total bill due from this connection is: $ 27.78
Do you want to continue and calculate another bill? If Yes enter 1, else 0:0
You calculated the bill 4 times and the total amount from all of the bills due is $1092.29
*** BONUS Section ***
Enter a number: 5
The numbers are: 1 2 3 4 5
The product of all numbers from 1 to 5 is 120
The product of all even numbers is 8
The product of all odd numbers is 15
Guidelines for Grading
60 Points Possible (+5 bonus)
10 points – Displaying the menu, performing the error check and reading the connection type correctly.
20 points – Handling 3 cases (connection) and the different rate slabs for units within each cases correctly.
5 points – Error checking for units.
10 points – Calculating the Energy Charge and Total Bill correctly.
10 points – Program loops accordingly and prints final total.
5 points – Displaying all the outputs as shown in the sample output above, writing comments in the code where it is necessary, using proper headers (name and pawprint), and general coding style.
lower bound
upper bound rate(in cents)per kwh 0 200 7.50 0 300 10.50 0 500 36.50 >200 700 10 >300 1000 14 >500 2000 40 >700 1250 13.50 >1000 2000 17.50 >2000 3000 45.50 >1250 above 15 >2000 above 20 >3000 above 50Explanation / Answer
#include <stdio.h>
//These are the function prototypes
void displayMenu1(void);
void displayMenu2(void);
int errorCheck(int);
int errorCheckUnits(int);
float getRate(int, int);
int charge(int);
int bonus(int);
void displayMenu1(void) //Displays the menu for the connection type
{
printf (" 1. Residential 2. Commercial 3. Industrial Please choose connection type: ");
}
void displayMenu2(void) //Displays the menu for the units
{
printf (" Please enter the number of units of electricity consumed (in kWh): ");
}
int errorCheck (int option) //Error checks the menu choice
{
if (option < 1 || option > 3)
{
printf (" ERROR! Please select a valid connection type! ");
return 0;
}
else return 1;
}
int errorCheckUnits (int units) //Error checks the units
{
if (units <= 0)
{
printf (" ERROR! Please enter a positive value for units consumed! ");
return 0;
}
else return 1;
}
float getRate (int option, int units)
{
float rate;
if (option == 1)
{
if (units > 0 && units <= 300) rate = 0.075;
if (units > 300 && units <= 750) rate = 0.100;
if (units > 750 && units <= 1500) rate = 0.135;
if (units > 1500) rate = 0.150;
}
if (option == 2)
{
if (units > 0 && units <= 300) rate = 0.105;
if (units > 300 && units <= 750) rate = 0.140;
if (units > 750 && units <= 1500) rate = 0.175;
if (units > 1500) rate = 0.200;
}
if (option == 3)
{
if (units > 0 && units <= 300) rate = 0.365;
if (units > 300 && units <= 750) rate = 0.400;
if (units > 750 && units <= 1500) rate = 0.455;
if (units > 1500) rate = 0.500;
}
return rate;
}
int charge (int option)
{
int flatFee;
if (option == 1) flatFee = 25;
if (option == 2) flatFee = 90;
if (option == 3) flatFee = 850;
return flatFee;
}
int bonus (int num1)
{
int num2, sum = 0, sumEven = 0, sumOdd = 0;
printf (" The numbers you wish to add are ");
for (num2=1; num2 <= num1; ++num2)
{
sum = sum + num2;
printf (" %d ", num2);
if (num2 % 2 == 0)
{
sumEven = sumEven + num2;
}
else
{
sumOdd = sumOdd + num2;
}
}
printf (" The sum of all numbers 0 to %d is %d. ", num1, sum);
printf ("Sum of even numbers: %d. ", sumEven);
printf ("Sum of odd numbers: %d. ", sumOdd);
return 0;
}
int main (void)
{
printf (" *****ELECTRICITY BILL CALCULATOR***** ");
int option, units, check = 0, flatFee, loop = 1;
float rate, rate2, subtotal, total;
while (loop == 1)
{
while (check == 0)
{
displayMenu1();
scanf("%d", &option);
check = errorCheck (option);
}
check = 0;
while (check == 0)
{
displayMenu2();
scanf("%d", &units);
check = errorCheckUnits (units);
}
check = 0;
rate = getRate (option, units);
rate2 = rate * units;
printf (" Your rate is $%.2f.", rate2);
flatFee = charge (option);
printf (" Your flat fee is $%d.", flatFee);
subtotal = rate2 + flatFee;
total = subtotal + total;
printf (" Your subtotal is $%.2f.", subtotal);
printf (" Do you want to calculate again? 1 for yes, 0 for no: ");
scanf ("%d", &loop);
}
printf (" Your total bill due is %.2f. ", total);
printf (" *****BONUS*****");
printf(" Please enter a positive number: ");
int num1;
scanf("%d", &num1);
while (num1 <= 0)
{
printf (" ERROR! Please enter a positive number! ");
printf ("Please enter a positive number: ");
scanf("%d", &num1);
}
bonus(num1);
printf(" ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.