Below is my program that I am currently working on for C not C++, when I compile
ID: 3651598 • Letter: B
Question
Below is my program that I am currently working on for C not C++, when I compile it it compiles normally and everything works except the amount always shows 0 when I compile it using gcc -Wall it tells me that on line 44 'total' may be uninitialized. How do I go about correcting this? (Line 44 is under my first case, case R r, where I have printf(Amount due....... )) thanks so much for your help!#include <stdio.h>
double Res(double rate);
double Com(double rate);
double Ind(double rate1,double rate2);
int main(void)
{
double total;
int kwh,
peak,
opeak,
num;
char code;
printf("Enter the account number > ");
scanf("%d", &num);
printf("Enter the code R-Residential C-Commercial I-Industrial ");
scanf(" %c", &code);
switch(code)
{
case 'R':
case 'r':
printf("Enter the kilo-watt hours >");
scanf("%d", &kwh);
printf("Residential use ");
printf("Account Number %d ",num);
printf("kilowatt-hours %d ",kwh);
printf("Amount due $ %.2f ",total);
break;
case 'C':
case 'c':
printf("Enter the kilowatt hours > ");
scanf("%d", &kwh);
printf("Commercial Use ");
printf("Account Number %d ",num);
printf("kilowatt-hours %d ",kwh);
printf("Amount due $ %.2f ",total);
break;
case 'I':
case 'i':
printf("Enter peak kilowatt-hours > ");
scanf("%d", &peak);
printf("Enter off-peak kilowatt-hours > ");
scanf("%d", &opeak);
printf("Industrial Use ");
printf("Account Number %d ",num);
printf("Peak kilowatt hours %d ",peak);
printf("Off-Peak kilowatt hours %d ",opeak);
printf("Amount due $ %.2f ",total);
break;
default:
printf("Invalid code type ");
}
return(0);
}
double Res(double rate)
{
return(6.00*(0.052/rate));
}
double Com(double rate)
{
double total;
if(rate<=1000)
total = 60.00;
else
total = 60 + (rate-1000)*0.045;
return(total);
}
double Ind(double rate1,double rate2)
{
double peak;
if(rate1<=1000)
peak = 76.00;
else
peak = 76.00 + (rate1-1000)*0.065;
double opeak;
if(rate2<=1000)
opeak = 40.00;
else
opeak = 40.00 + (rate2-1000)*0.028;
return(peak+opeak);
}
Explanation / Answer
please rate - thanks
you never called the functions, so total never received a value
#include <stdio.h>
double Res(double rate);
double Com(double rate);
double Ind(double rate1,double rate2);
int main(void)
{
double total; //
int kwh,
peak,
opeak,
num;
char code;
printf("Enter the account number > ");
scanf("%d", &num);
printf("Enter the code R-Residential C-Commercial I-Industrial ");
scanf(" %c", &code);
switch(code)
{
case 'R':
case 'r':
printf("Enter the kilo-watt hours >");
scanf("%d", &kwh);
printf("Residential use ");
printf("Account Number %d ",num);
printf("kilowatt-hours %d ",kwh);
total=Res(kwh);
printf("Amount due $ %.2f ",total);
break;
case 'C':
case 'c':
printf("Enter the kilowatt hours > ");
scanf("%d", &kwh);
printf("Commercial Use ");
printf("Account Number %d ",num);
printf("kilowatt-hours %d ",kwh);
total=Com(kwh);
printf("Amount due $ %.2f ",total);
break;
case 'I':
case 'i':
printf("Enter peak kilowatt-hours > ");
scanf("%d", &peak);
printf("Enter off-peak kilowatt-hours > ");
scanf("%d", &opeak);
printf("Industrial Use ");
printf("Account Number %d ",num);
printf("Peak kilowatt hours %d ",peak);
printf("Off-Peak kilowatt hours %d ",opeak);
total=Ind(peak,opeak);
printf("Amount due $ %.2f ",total);
break;
default:
printf("Invalid code type ");
}
return(0);
}
double Res(double rate)
{
return(6.00*(0.052/rate));
}
double Com(double rate)
{
double total;
if(rate<=1000)
total = 60.00;
else
total = 60 + (rate-1000)*0.045;
return(total);
}
double Ind(double rate1,double rate2)
{
double peak;
if(rate1<=1000)
peak = 76.00;
else
peak = 76.00 + (rate1-1000)*0.065;
double opeak;
if(rate2<=1000)
opeak = 40.00;
else
opeak = 40.00 + (rate2-1000)*0.028;
return(peak+opeak);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.