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

I need to modify my code, but am having trouble with having it compile correctly

ID: 3625246 • Letter: I

Question

I need to modify my code, but am having trouble with having it compile correctly. Here are the requirements I must follow. Any help would be so appreciated!
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.

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.)

For example: I 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, I could put the comment before the bank record into a function by sending the closing balance into the function.


#include <stdio.h>

void main (void)
{

/* Variable Declarations */
/* --------------------- */

int number_of_deposits, number_of_withdrawals, i;
float amount_to_deposit[50], amount_to_withdraw[50];
float starting_balance, current_balance;
char name[30];




printf("Please enter your first name: ");
gets(name); /* or use scanf("%s", name); */
fflush(stdin);

printf(" Hello %s ", name);

/* Query the user for the starting balance (starting_balance >= 0) */
/* ------------------------------------------------------------ */

do
{
printf ("Now enter your current balance in dollars and cents: ");
scanf ("%f", &starting_balance);
fflush(stdin);

if (starting_balance < 0)
printf("Error: Starting Balance must be at least zero! ");

} while (starting_balance < 0);

/* Prompt for the number of withdrawals */
/* Ensure that user did not enter more than 50 withdrawals. */
/* -------------------------------------------------------- */

do
{
printf (" Enter the number of withdrawals: ");
scanf ("%i", &number_of_withdrawals);
fflush(stdin);

if (number_of_withdrawals > 50)
printf ("*** Too many withdrawals. *** ");
if (number_of_withdrawals < 0)
printf ("*** Negative number of withdrawals not allowed. *** ");

} while (number_of_withdrawals > 50 || number_of_withdrawals < 0);


/* Prompt for the number of deposits. */
/* Ensure that user did not enter more than 50 deposits. */
/* ----------------------------------------------------- */

do
{
printf (" Enter the number of deposits: ");
scanf ("%i", &number_of_deposits);
fflush(stdin);

if (number_of_deposits > 50)
printf ("*** Too many deposits. *** ");
if (number_of_deposits < 0)
printf ("*** Negative number of deposits not allowed. *** ");

} while ( number_of_deposits > 50 || number_of_deposits < 0);



/* Retain starting balance for bank record. */
/* ---------------------------------------- */

current_balance = starting_balance;

/* Prompt for all deposit amounts, keeping track of current balance. */
/* ---------------------------------------------------------------- */

printf (" "); /* spacing */
for (i = 0; i < number_of_deposits; i++ )
{
do
{
printf ("Enter the amount of deposit #%i: ", i+1 );
scanf ("%f", &amount_to_deposit[i]);
fflush(stdin);

if (amount_to_deposit[i] <= 0)
printf ("*** Deposit amount must be positive! Please re-enter *** ");

} while (amount_to_deposit[i] <= 0);

current_balance = current_balance + amount_to_deposit[i];

} /* end for loop */


/* Prompt for all withdrawal amounts, keeping track of current balance. */
/* ------------------------------------------------------------------- */


printf (" "); /* spacing */
for (i = 0; i < number_of_withdrawals; i++ )
{
/* Enter amounts, and be sure amount does not exceed current balance. */
/* ------------------------------------------------------------------ */

do
{
printf ("Enter the amount of withdrawal #%i: ", i+1 );
scanf ("%f", &amount_to_withdraw[i]);
fflush(stdin);

if (amount_to_withdraw[i] > current_balance)
printf ("*** Withdrawal amount exceeds current balance. *** ");
else
if (amount_to_withdraw[i] <= 0)
printf ("*** Withdrawal amount must be greater than zero! *** ");

} while (amount_to_withdraw[i] > current_balance || amount_to_withdraw[i] <= 0);

current_balance = current_balance - amount_to_withdraw[i];

/* If Balance goes to zero, break the loop, no more withdrawals allowed, reset the
withdrawal count so the bank record prints properly! */

if(current_balance == 0)
{
printf (" *** Balance is now zero. No more withdrawals allowed! *** ");
number_of_withdrawals = i + 1;
break;
} /* end-if */

} /* end for loop.*/
printf (" "); /* spacing */


/* Output closing balance and appropriate closing message. */
/* ------------------------------------------------------- */

printf ("*** The closing balance %s is $%.2f *** ", name, current_balance);

if (current_balance >= 50000.00 )
printf ("*** %s time to invest some money! ***", name);
else
if (current_balance >= 15000.00 )
printf ("*** %s maybe you should consider a CD. ***", name);
else
if (current_balance >= 1000.00 )
printf ("*** %s keep up the good work. ***", name);
else
printf ("*** %s your balance is very low. ***", name);


/* Output bank record: Starting balance, deposits and withdrawals. */
/* ------------------------------------------------------------- */


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

for (i = 0; i < number_of_deposits; i++ )
printf ("Deposit #%2i: %15.2f ", i+1, amount_to_deposit[i]);

if (number_of_deposits > 0)
printf (" "); /* spacing */

for (i = 0; i < number_of_withdrawals; ++i )
printf ("Withdrawal #%2i: %15.2f ", i+1, amount_to_withdraw[i]);

printf (" Ending Balance: $%15.2f ", current_balance);

getchar();

} /* end main. */

Explanation / Answer

please rate - thanks

5 nice functions, 2 of which are used twice

#include <stdio.h>
void bankRecord(int,float[],int,float[],float,float);
void balance(float,char[]);
float get_starting_info(char[]);
int get_number(char[]);
void do_transactions(float[],char[],int);
int main (void)
{

/* Variable Declarations */
/* --------------------- */

int number_of_deposits, number_of_withdrawals, i;
float amount_to_deposit[50], amount_to_withdraw[50];
float starting_balance, current_balance;
char name[30];
starting_balance=get_starting_info(name);

/* Prompt for the number of withdrawals */
/* Ensure that user did not enter more than 50 withdrawals. */
/* -------------------------------------------------------- */
number_of_withdrawals=get_number("withdrawals");

/* Prompt for the number of deposits. */
/* Ensure that user did not enter more than 50 deposits. */
/* ----------------------------------------------------- */
number_of_deposits=get_number("deposits");

/* Retain starting balance for bank record. */
/* ---------------------------------------- */

current_balance = starting_balance;

/* Prompt for all deposit amounts, keeping track of current balance. */
/* ---------------------------------------------------------------- */

printf (" "); /* spacing */
for (i = 0; i < number_of_deposits; i++ )
{do_transactions(amount_to_deposit,"deposit",i);
current_balance = current_balance + amount_to_deposit[i];

} /* end for loop */


/* Prompt for all withdrawal amounts, keeping track of current balance. */
/* ------------------------------------------------------------------- */


printf (" "); /* spacing */
for (i = 0; i < number_of_withdrawals; i++ )
{
/* Enter amounts, and be sure amount does not exceed current balance. */
/* ------------------------------------------------------------------ */
do_transactions(amount_to_withdraw,"withdrawal",i);
current_balance = current_balance - amount_to_withdraw[i];

/* If Balance goes to zero, break the loop, no more withdrawals allowed, reset the
withdrawal count so the bank record prints properly! */

if(current_balance == 0)
{
printf (" *** Balance is now zero. No more withdrawals allowed! *** ");
number_of_withdrawals = i + 1;
break;
} /* end-if */

} /* end for loop.*/
printf (" "); /* spacing */

balance(current_balance,name);

bankRecord(number_of_deposits,amount_to_deposit,number_of_withdrawals,
              amount_to_withdraw,starting_balance,current_balance);


getchar();

} /* end main. */
void do_transactions(float amount[],char mess[],int i)
{
do
{
printf ("Enter the amount of %s #%i: ",mess, i+1 );
scanf ("%f", &amount[i]);
fflush(stdin);

if (amount[i] <= 0)
printf ("*** %s amount must be positive! Please re-enter *** ",mess);

} while (amount[i] <= 0);
}
int get_number(char mess[])
{int number;
do
{
printf (" Enter the number of %s: ",mess);
scanf ("%i", &number);
fflush(stdin);

if (number > 50)
printf ("*** Too many %s. *** ",mess);
if (number < 0)
printf ("*** Negative number of %s not allowed. *** ",mess);

} while (number > 50 || number < 0);
return number;
}
float get_starting_info(char name[])
{float starting_balance;
printf("Please enter your first name: ");
gets(name); /* or use scanf("%s", name); */
fflush(stdin);

printf(" Hello %s ", name);

/* Query the user for the starting balance (starting_balance >= 0) */
/* ------------------------------------------------------------ */

do
{
printf ("Now enter your current balance in dollars and cents: ");
scanf ("%f", &starting_balance);
fflush(stdin);

if (starting_balance < 0)
printf("Error: Starting Balance must be at least zero! ");

} while (starting_balance < 0);

return starting_balance;   
}

void balance(float current_balance,char name[])
{/* Output closing balance and appropriate closing message. */
/* ------------------------------------------------------- */

printf ("*** The closing balance %s is $%.2f *** ", name, current_balance);

if (current_balance >= 50000.00 )
printf ("*** %s time to invest some money! ***", name);
else
if (current_balance >= 15000.00 )
printf ("*** %s maybe you should consider a CD. ***", name);
else
if (current_balance >= 1000.00 )
printf ("*** %s keep up the good work. ***", name);
else
printf ("*** %s your balance is very low. ***", name);
}
void bankRecord(int number_of_deposits,float amount_to_deposit[],
               int number_of_withdrawals,float amount_to_withdraw[],
               float starting_balance,float current_balance)
{/* Output bank record: Starting balance, deposits and withdrawals. */
/* ------------------------------------------------------------- */
int i;

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

for (i = 0; i < number_of_deposits; i++ )
printf ("Deposit #%2i: %15.2f ", i+1, amount_to_deposit[i]);

if (number_of_deposits > 0)
printf (" "); /* spacing */

for (i = 0; i < number_of_withdrawals; ++i )
printf ("Withdrawal #%2i: %15.2f ", i+1, amount_to_withdraw[i]);

printf (" Ending Balance: $%15.2f ", current_balance);
}