I was wondering if anyone could help me convert the program below from an if/els
ID: 3806877 • Letter: I
Question
I was wondering if anyone could help me convert the program below from an if/else statement to a DO-WHILE Loop.
#include <stdio.h>
#include <stdlib.h>
int main()
{
double MIN_BAL = 400.00; // minimum balance
double MONTHLY_FEE = 10.00; // base monthly fee
double LOW_BAL_FEE = 15.00; // low balance fee
double FEE_LEVEL1 = 0.10; // fee: 1-19 checks
double FEE_LEVEL2 = 0.08; // fee: 20-39 checks
double FEE_LEVEL3 = 0.06; // fee: 40-59 checks
double FEE_LEVEL4 = 0.04; // fee: 60 or more checks
int CHECK_LEVEL1 = 19; // 1-19 checks
int CHECK_LEVEL2 = 39; // 20-39 checks
int CHECK_LEVEL3 = 59; // 40-59 checks
double begin_balance;
int checks;
printf("Author's Name ");
printf("C.S.1428.25? ");
printf("Lab Section: L? ");
printf("--/--/-- ");
printf("Enter the following information about your checking account. ");
printf(" Beginning balance: $");
scanf("%lf", &begin_balance);
if (begin_balance < 0)
{
printf(" You entered a negative beginning balance. ");
printf(" Your account is currently overdrawn! ");
}
printf(" Number of checks written: ");
scanf("%d", &checks);
int isNegativeChecks = checks < 0;
if (isNegativeChecks)
{
printf(" Number of checks must be zero or more. ");
printf(" Try again. ");
}
printf(" The bank's service fees have been written to prog4_25?out.txt. ");
int isLowBalance = begin_balance < MIN_BAL;
double checkFees;
if (checks >= 0)
{
if (checks <= CHECK_LEVEL1)
{
checkFees = checks*FEE_LEVEL1;
}
else if(checks <= CHECK_LEVEL2)
{
checkFees = CHECK_LEVEL1*FEE_LEVEL1 + (checks-CHECK_LEVEL1)*FEE_LEVEL1;
}
else if(checks <= CHECK_LEVEL3)
{
checkFees = CHECK_LEVEL1*FEE_LEVEL1 +
(CHECK_LEVEL2-CHECK_LEVEL1)*FEE_LEVEL1 +
(checks - CHECK_LEVEL2)*FEE_LEVEL2;
}
else
{
checkFees = CHECK_LEVEL1*FEE_LEVEL1 +
(CHECK_LEVEL2-CHECK_LEVEL1)*FEE_LEVEL1 +
(CHECK_LEVEL3 - CHECK_LEVEL2)*FEE_LEVEL2 +
(checks - CHECK_LEVEL3)*FEE_LEVEL4;
}
}
double totalFees = 0;
FILE *fh = fopen("prog4_25?out.tx", "w");
fprintf(fh, "Author's Name ");
fprintf(fh, "C.S.1428.25? ");
fprintf(fh, "Lab Section: L? ");
fprintf(fh, "--/--/-- ");
if (isLowBalance && !isNegativeChecks)
{
fprintf(fh, "Low balance fee applied: %f ", LOW_BAL_FEE);
totalFees += LOW_BAL_FEE;
}
if (!isNegativeChecks)
{
fprintf(fh, "Number of checks written: %d ", checks);
fprintf(fh, "Check fee applied: %f ", checkFees);
totalFees += checkFees;
}
fprintf(fh, "Monthly fee applied: %f ", MONTHLY_FEE);
totalFees += MONTHLY_FEE;
fprintf(fh, " Total banking fees: %f ", totalFees);
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main()
{
double MIN_BAL = 400.00; // minimum balance
double MONTHLY_FEE = 10.00; // base monthly fee
double LOW_BAL_FEE = 15.00; // low balance fee
double FEE_LEVEL1 = 0.10; // fee: 1-19 checks
double FEE_LEVEL2 = 0.08; // fee: 20-39 checks
double FEE_LEVEL3 = 0.06; // fee: 40-59 checks
double FEE_LEVEL4 = 0.04; // fee: 60 or more checks
int CHECK_LEVEL1 = 19; // 1-19 checks
int CHECK_LEVEL2 = 39; // 20-39 checks
int CHECK_LEVEL3 = 59; // 40-59 checks
double begin_balance;
int checks;
printf("Author's Name ");
printf("C.S.1428.25? ");
printf("Lab Section: L? ");
printf("--/--/-- ");
printf("Enter the following information about your checking account. ");
//adding do while here,,,
//to read a correct balance
do
{
printf(" Beginning balance: $");
scanf("%lf", &begin_balance);
if (begin_balance < 0)
{
printf(" You entered a negative beginning balance. ");
printf(" Your account is currently overdrawn! ");
}
}
while(begin_balance<0);
//added do-while loop
//to correctly read the number of checks
int isNegativeChecks;
do
{
printf(" Number of checks written: ");
scanf("%d", &checks);
isNegativeChecks = checks < 0;
if (isNegativeChecks)
{
printf(" Number of checks must be zero or more. ");
printf(" Try again. ");
}
}while(checks<0);
printf(" The bank's service fees have been written to prog4_25?out.txt. ");
int isLowBalance = begin_balance < MIN_BAL;
double checkFees;
if (checks >= 0)
{
if (checks <= CHECK_LEVEL1)
{
checkFees = checks*FEE_LEVEL1;
}
else if(checks <= CHECK_LEVEL2)
{
checkFees = CHECK_LEVEL1*FEE_LEVEL1 + (checks-CHECK_LEVEL1)*FEE_LEVEL1;
}
else if(checks <= CHECK_LEVEL3)
{
checkFees = CHECK_LEVEL1*FEE_LEVEL1 +
(CHECK_LEVEL2-CHECK_LEVEL1)*FEE_LEVEL1 +
(checks - CHECK_LEVEL2)*FEE_LEVEL2;
}
else
{
checkFees = CHECK_LEVEL1*FEE_LEVEL1 +
(CHECK_LEVEL2-CHECK_LEVEL1)*FEE_LEVEL1 +
(CHECK_LEVEL3 - CHECK_LEVEL2)*FEE_LEVEL2 +
(checks - CHECK_LEVEL3)*FEE_LEVEL4;
}
}
double totalFees = 0;
FILE *fh = fopen("prog4_25?out.tx", "w");
fprintf(fh, "Author's Name ");
fprintf(fh, "C.S.1428.25? ");
fprintf(fh, "Lab Section: L? ");
fprintf(fh, "--/--/-- ");
if (isLowBalance && !isNegativeChecks)
{
fprintf(fh, "Low balance fee applied: %f ", LOW_BAL_FEE);
totalFees += LOW_BAL_FEE;
}
if (!isNegativeChecks)
{
fprintf(fh, "Number of checks written: %d ", checks);
fprintf(fh, "Check fee applied: %f ", checkFees);
totalFees += checkFees;
}
fprintf(fh, "Monthly fee applied: %f ", MONTHLY_FEE);
totalFees += MONTHLY_FEE;
fprintf(fh, " Total banking fees: %f ", totalFees);
return 0;
}
output:-
Author's Name
C.S.1428.25?
Lab Section: L?
--/--/--
Enter the following information about your checking account.
Beginning balance: $-10
You entered a negative beginning balance.
Your account is currently overdrawn!
Beginning balance: $300
Number of checks written: -1
Number of checks must be zero or more.
Try again.
Number of checks written: 0
The bank's service fees have been written to prog4_25?out.txt.
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.