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

I am having trouble with my code. Here is the direction: his program is similar

ID: 3633979 • Letter: I

Question

I am having trouble with my code.
Here is the direction:

his program is similar to programming assignment #4: Data error checking for all inputs as in #4. Your program is to behave in the exact same manner as assignment #4, however how you structure your code is a different story. Now we will use functions.

Recall Assignment #4:

Write a C program that allows the user to make some simple banking transactions. The program should first prompt the user to enter the current balance of his/her bank account (in dollars and cents). The program should then prompt the user to enter the number of deposits to make, and then the number of withdrawals to make. For this assignment, let's set a maximum of 50 deposits and 50 withdrawals. (etc.)

Here is the change for program #5: There should be at least three non-trivial functions in the program! (by that I mean a function that does more than just print a line of text). For example, one function may be to find out how many deposits will be entered, , or the opening balance, or the message before the bank record. etc.

Hints: Your code from assignment #4 does not need to be modified too much to solve this problem. The algorithm is the same. Outputting the contents of the arrays in "record form" is pretty straightforward, and should be done in a loop. (This could be a good place for a function.) Take concepts from my answer to problem #4, but please do not copy mine verbatim.

For example: You can use void as the function type and use an argument(s) (e.g. you can have void print_deposits_out ( float deposits [ ], int number_of_deposits) as a prototype.) And you could use a similar one for the withdrawals. Or, you could put the comment before the bank record into a function by sending the closing balance into the function. Furthermore, if you use more than three functions, I will grant you some minor errors that I would normally take off for(-2 or -3 points) for each function used (to a maximum of 100 for a grade). Now that's an incentive!

Good luck!




#include<stdio.h>

void balance(float, char []);
float get_current_info(char[]);
int get_number(char[]);


main(void)
{
/* Declare the variable */
/* ------------------------------------------ */

int num_deposit = 0, i, n;
int num_withdrawal = 0;
float start_balance;
float end_balance;
float deposit_total = 0;
float withdrawals_total = 0;
float deposits[50];
float withdrawals[50];
char fst_name[60];

start_balance=get_current_info(fst_name[60]);



printf(" Enter the number of withdrawals: ");
scanf("%d", &num_withdrawal);
fflush(stdin);

while(num_withdrawal < 0)
{
printf("Error: Number of withdrawals must be at least zero, please re-enter. "); /* If the number of withdrawal is a negative number, errorr message will display*/
printf("Enter the number of withdrawals: ");
scanf("%d", &num_withdrawal);
fflush(stdin);

if(num_withdrawal > 50)
{
printf("*** Error: Number of withdrawals must less than fifty. *** ");
printf("Enter the number of withdrawals: ");
scanf("%d", &num_withdrawal);
fflush(stdin);
}
}

printf(" Enter the number of deposits: ");
scanf("%d", &num_deposit);
fflush(stdin);

while(num_deposit < 0)
{
printf("*** Error: Number of deposits must be at least zero, please re-enter. *** "); /* If the number of deposit is a negative number, errorr message will display*/
printf("Enter the number of deposits: ");
scanf("%d", &num_deposit);
fflush(stdin);

if(num_deposit > 50)
{
printf("*** Error: Number of deposit must be is less than fifty. *** ");
printf("Enter the number of deposits: ");
scanf("%d", &num_deposit);
fflush(stdin);
}
}

/* Loop the transations */
/* ------------------------------------ */


printf(" ");
for(i = 0; i < num_deposit; i++)
{
printf("Enter the amount of deposit #%d: ", i + 1);
scanf("%f", &deposits[i]);
fflush(stdin);

while(deposits[i] < 0)
{
printf("*** Deposit amount must be at least zero, please re-enter. *** "); /* If the deposit is less than zero, re-enter the amount */
printf("Enter the amount of deposit #%d: ", i + 1);
scanf("%f", &deposits[i]);
fflush(stdin);

}

start_balance = start_balance + deposits[i]; /* accumulation */

} /*end loop */

printf(" ");
for(i = 0; i < num_withdrawal; i++)
{
printf("Enter the amount of withdrawal #%d: ", i + 1);
scanf("%f", &withdrawals[i]);
fflush(stdin);

while(withdrawals[i] < 0)
{
printf("*** Withdrawal amount must be at least zero, please re-enter. *** "); /* If the withdrawal is less than zero, re-enter the amount */
printf("Enter the amount of withdrawal #%d: ", i + 1);
scanf("%f", &withdrawals[i]);
fflush(stdin);

}

if(withdrawals[i] > start_balance)
{
printf("*** Withdrawal amount exceeds current balance. *** "); /* Display message if the withdrawals is greater than the deposit */

}


else
if(withdrawals[i] <= 0)
{
printf("*** Withdrawal amount must be larger than zero! *** ");
printf("Enter the amount of withdrawal #%d: ", i + 1);
scanf("%f", &withdrawals[i]);
fflush(stdin);
}

start_balance = start_balance - withdrawals[i];
if(start_balance == 0)
{
printf(" *** The Balance is zero. Withdrawals not allowed! *** ");
withdrawals_total = i + 1;
break;
} /* end-if*/

} /* end loop */



end_balance = start_balance + deposit_total; /* calculate the entire the total */

/* Introduction to the program */
/* --------------------------------------------- */

printf("Welcome to the Sears Banking System ");
printf("Please enter your first name: ");
gets(fst_name);
fflush(stdin);

printf(" Hello %s. ", fst_name);
printf("Now enter your current balance in dollars and cents: ");
scanf("%f", &start_balance);
fflush(stdin);

/* Error message: Beginning balance*/
/* ------------------------------------------- */
while(start_balance < 0)
{
printf("Error: Beginning balance must be at least zero, please re-enter! "); /* If the balance is less than zero, enter the amount again. */
printf("Now enter current balance in dollars and cents: ");
scanf("%f", &start_balance);
fflush(stdin);
}
printf(" The closing balance %s is $%.2f ",fst_name,end_balance);

if(end_balance >= 50000)
{
printf("*** %s it is time to invest some money*** ",fst_name);
}

else if(end_balance >= 15000)
{
printf("*** %s maybe you should consider a CD. *** ",fst_name);
}

else if(end_balance >= 1000)
{
printf("*** %s keep up the good work. *** ",fst_name);
}

else if(end_balance >= 0)
{
printf("*** %s your balance getting low. *** ",fst_name);
}


printf(" *** Bank Record *** ");
printf ("Starting Balance:$%13.2f ", start_balance);

for(i = 0; i < num_deposit; i++)
{
printf("Deposit #%d: %12.2f ", i + 1,deposits[i]);
}

printf(" ");
for(i = 0; i < num_withdrawal; i++)
{
printf("Withdrawal #%d: %12.2f ", i + 1,withdrawals[i]);
}

printf(" ");
printf("Ending balance: $%12.2f ",end_balance);

getchar ();

} /* end main */

Explanation / Answer

please rate - thanks

your logic is untouched


#include<stdio.h>
float getdeposits(float[],int,float);
float getwithdrawals(float[],int*,float);
float getBalance(char []);
int getInfo(char[],int,int);
void print(char [],float,float,int,int,float[],float[]);
int main(void)
{
         /* Declare the variable */
         /* ------------------------------------------ */
  
   int   num_deposit = 0, i, n;
   int   num_withdrawal = 0;
   float   start_balance;
   float   end_balance;
   float   deposit_total = 0;
   float   withdrawals_total = 0;
   float   deposits[50];
   float   withdrawals[50];
   char    fst_name[60];
   /* Introduction to the program */
   /* --------------------------------------------- */
  
   printf("Welcome to the Sears Banking System ");
   printf("Please enter your first name: ");
   gets(fst_name);
   fflush(stdin);
  
   printf(" Hello %s. ", fst_name);
   start_balance=getBalance("Now enter your current balance in dollars and cents: ");
   num_withdrawal=getInfo(" Enter the number of withdrawals: ",0,50);
   num_deposit=getInfo("Enter the number of deposits: ",0,50);

   /* Loop the transations */
   /* ------------------------------------ */
   end_balance=getdeposits(deposits,num_deposit,start_balance);
   end_balance=getwithdrawals(withdrawals,&num_withdrawal,end_balance);
   print(fst_name,end_balance,start_balance,num_deposit,num_withdrawal,deposits,withdrawals);

   getchar ();
  
} /* end main */
void print(char fst_name[],float end_balance,float start_balance,
           int num_deposit,int num_withdrawal,float deposits[],
           float withdrawals[])
{int i;
printf(" The closing balance %s is $%.2f ",fst_name,end_balance);
  
   if(end_balance >= 50000)
   {
    printf("*** %s it is time to invest some money*** ",fst_name);
   }
  
   else if(end_balance >= 15000)
   {
    printf("*** %s maybe you should consider a CD. *** ",fst_name);
   }
  
   else if(end_balance >= 1000)
   {
    printf("*** %s keep up the good work. *** ",fst_name);
   }
  
   else if(end_balance >= 0)
   {
    printf("*** %s your balance getting low. *** ",fst_name);
   }
  
   printf(" *** Bank Record *** ");
   printf ("Starting Balance:$%13.2f ", start_balance);
  
   for(i = 0; i < num_deposit; i++)
   {
    printf("Deposit #%d:      %12.2f ", i + 1,deposits[i]);
   }
  
   printf(" ");
   for(i = 0; i < num_withdrawal; i++)
   {
    printf("Withdrawal #%d:   %12.2f ", i + 1,withdrawals[i]);
   }
  
   printf(" ");
   printf("Ending balance: $%12.2f ",end_balance);
}
float getwithdrawals(float withdrawals[],int* num_withdrawals,float start_balance)
{ int i;
   printf(" ");
   for(i = 0; i < *num_withdrawals; i++)
   {
    printf("Enter the amount of withdrawal #%d: ", i + 1);
    scanf("%f", &withdrawals[i]);
    fflush(stdin);
   
    while(withdrawals[i] < 0)
    {
     printf("*** Withdrawal amount must be at least zero, please re-enter. *** "); /* If the withdrawal is less than zero, re-enter the amount */
     printf("Enter the amount of withdrawal #%d: ", i + 1);
     scanf("%f", &withdrawals[i]);
     fflush(stdin);
    
    }
   
    if(withdrawals[i] > start_balance)
    {
     printf("*** Withdrawal amount exceeds current balance. *** "); /* Display message if the withdrawals is greater than the deposit */
    
    }
   
   
    else
      if(withdrawals[i] <= 0)
      {
       printf("*** Withdrawal amount must be larger than zero! *** ");
       printf("Enter the amount of withdrawal #%d: ", i + 1);
       scanf("%f", &withdrawals[i]);
       fflush(stdin);
      }
     
      start_balance = start_balance - withdrawals[i];
      if(start_balance == 0)
   {
    printf(" *** The Balance is zero. Withdrawals not allowed! *** ");
    *num_withdrawals= i + 1;    
    break;
   } /* end-if*/
     
   } /* end loop */
     
return start_balance;    
      }
float getdeposits(float deposits[],int num_deposit,float start_balance)
{int i;
   printf(" ");
   for(i = 0; i < num_deposit; i++)
   {
    printf("Enter the amount of deposit #%d: ", i + 1);
    scanf("%f", &deposits[i]);
    fflush(stdin);
   
    while(deposits[i] < 0)
    {
     printf("*** Deposit amount must be at least zero, please re-enter. *** "); /* If the deposit is less than zero, re-enter the amount */
     printf("Enter the amount of deposit #%d: ", i + 1);
     scanf("%f", &deposits[i]);
     fflush(stdin);
    
    }
   
    start_balance = start_balance + deposits[i]; /* accumulation */
   
   } /*end loop */
   return start_balance;
      }
int getInfo(char message[],int min,int max)
{int n;

   printf(" %s",message);
   scanf("%d", &n);
   fflush(stdin);
  
   while(n < min)
   {
    printf("Error:must be at least %d, please re-enter. ",min); /* If the number of withdrawal is a negative number, errorr message will display*/
    printf("%s",message);
    scanf("%d", &n);
    fflush(stdin);
   
    if(n > max)
    {
     printf("*** Error: must less than %d. *** ",max);
     printf("%s",message);
    scanf("%d", &n);
     fflush(stdin);
    }
   }
return n;
}
float getBalance(char message[])
{float n;
printf("%s",message);
   scanf("%f", &n);
   fflush(stdin);
  
   /* Error message: */
   /* ------------------------------------------- */
   while(n < 0)
   {
    printf("Error: must be at least 0 please re-enter! "); /* If the balance is less than zero, enter the amount again. */
    printf("%s",message);
   scanf("%f", &n);
    fflush(stdin);
   }
   return n;
}