Week 4 ties into Week 5 Assignment so I am posting Week 4 an
Week 4 ties into Week 5 Assignment, so I am posting Week 4 and then the instructions to add onto Week 4 for Week5:
Week 4 Assignment Instructions:
Write a C program that allows the user to make some teller transactions.
The program should first prompt the user to enter the current balance of his/her account (in dollars and cents, not less than zero).
The program should then prompt the user to enter the number of withdrawals to make, and then the number of deposits to make.
For this assignment, let\'s set a maximum of 50 deposits and 50 withdrawals, you\'ll see why as you read on.
Using a loop, the program should then prompt the user to enter the amount of the first deposit (a positive amount to add to the account balance), the amount of the second deposit, the third, etc., until the number of deposits have been processed.
Using a second loop, the program should then prompt the user to enter the amount of the first withdrawal (a positive amount to subtract from the account balance, if possible), the amount of the second withdrawal, the third, etc. until the number of withdrawals have been processed.
Once all deposits and withdrawals have been made, the program should output the ending balance.
The dialog with the user should look like the following:
Welcome to the Computer Teller System
Enter your current balance in dollars and cents: 256.40
Enter the number of deposits (0 - 50): 3
Enter the number of withdrawals (0 - 50): 2
Enter the amount of deposit #1: 10.50
Enter the amount of deposit #2: 12.25
Enter the amount of deposit #3: 125.30
Enter the amount of withdrawal #1: 120.35
Enter the amount of withdrawal #2: 35.60
*** The closing balance is $248.50
*** The program should also output one of the following messages based on the closing balance. That is:
If the closing balance is greater than or equal to 50000.00, output: \"*** It is time to invest some money! ***\"
If the closing balance is between 15000.00 and 49999.99, output: \"*** Maybe you should consider a CD. ***\"
If the closing balance is between 1000.00 and 14999.99, output: \"*** Keep up the good work! ***\"
If the closing balance is between 0.00 and 999.99, output: \"*** Your balance is getting low! ***\"
So, in the above example, the last line of sample output would be: *** Your balance is getting low! ***
Regarding error checking on all user input, the following 5 error checks (on user input) should be included in your program:
1. Ensure that the starting (current) balance is a positive number. If not, the following error message should be displayed: *** Beginning balance must be at least zero, please re-enter.
2. Ensure that the number of deposits is between 0 and 50. If not, the following error message should be displayed: *** Invalid number of deposits, please re-enter.
3. Ensure that the number of withdrawals is between 0 and 50. If not, the following error message should be displayed: *** Invalid number of withdrawals, please re-enter.
4. Ensure that the deposit amount is equal to or greater than zero. If not, the following error message should be displayed: *** Deposit amount must be greater than zero, please re-enter.
5. Ensure that the withdrawal amount does not exceed the current balance (including the new deposits). If so, the following error message should be displayed: *** Withdrawal amount exceeds current balance.
So, for example, a sample run of the program with error checking might look like:
Enter current balance in dollars and cents: -56.40
*** Beginning balance must be at least zero, please re-enter.
Enter current balance in dollars and cents: 256.40
Enter the number of deposits (0 - 50): -3
*** Invalid number of deposits, please re-enter.
Enter the number of deposits: 3
Enter the number of withdrawals (0 - 50): 2
Enter the amount of deposit #1: 10.50
Enter the amount of deposit #2: -12.25
*** Deposit amount must be greater than zero, please re-enter.
Enter the amount of deposit #2: 12.25
Enter the amount of deposit #3: 125.30
Enter the amount of withdrawal #1: 5000.00
*** Withdrawal amount exceeds current balance, please re-enter.
Enter the amount of withdrawal #1: 120.35
Enter the amount of withdrawal #2: 35.60
*** The closing balance is $248.50 ***
*** Your balance is getting low! ***
Now, the reason we restrict the maximum number of deposits and withdrawals to 50 has to do with the array sizes. You want to be sure that the user does not want to enter more information than you can store in your arrays. In my example above, I made the array sizes \"50\". That means that I can store up to 50 deposits and 50 withdrawals. So, I want to test that the user does not plan to enter more than that. So, for example, if the user enters 100 as the number of deposits, the following should be displayed:
Enter the number of deposits (0 - 50): 100
*** Invalid number of deposits, please re-enter.
Lastly, you are to keep track of all of the deposits and all of the withdrawals so that you can print them out in \"record\" form. You do this by storing them in arrays. You will have one array to hold the deposit amounts entered by the user, and another array to hold the withdrawal amounts. You want to be sure that the size of the arrays are large enough to handle 50 deposits and 50 withdrawals. Perhaps:
float deposits[50], withdrawals[50];
Based on the above program run, the Teller Record should look like:
*** Teller Record *** Starting Balance: $ 256.40 Deposit #1: 10.50 Deposit #2: 12.25 Deposit #3: 125.30 Withdrawal #1: 120.35 Withdrawal #2: 35.60 Ending Balance: $ 248.50
Try to align the decimal points as closely as possible in your output.
Note: There are some bothersome complications with the entry and storage of some float numbers. Because of the way data is stored the float number 22.39 may be stored as 22.389999 and this may cause a problem with your final if statements and even with balance equaling zero. In other words, it is nearly impossible to test for equality with variables of type float. This is a well known anomaly in C. You may not encounter these problems, I just wanted to warn you of the potential problem.
Week 5 Assignment (Final Assignment) Instructions:
This program is similar to programming assignment #4.
That is, you will have all of the requirements as in Programming Assignment #4, including data error checking on all inputs.
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 Programming 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, not less than zero).
The program should then prompt the user to enter the number of withdrawals to make, and then the number of deposits 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. (Non-trivial means that the function does more than simply print a line of text. The functions should accept data through arguments and/or send data back to main via return)
Here are some examples of what you might want to use for your functions: Input functions:
1. Obtain opening (current) balance.
2. Obtain number the number of deposits.
3. Obtain number of withdrawals.
4. Obtain deposit amounts.
5. Obtain withdrawal amounts.
Output functions:
1. Display closing balance and associated message.
2. Display message based on closing balance.
3. Display bank record.
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.)
The most complicated of the above functions is the one which would display the bank record.
If you decide that you want to attempt this one, I will give you a head start by providing the function header as shown here: void (display_bank_record (float start_balance, float deposits [ ], int num_deposits, float withdrawals [ ], int num_withdrawals, float end_balance)
This is what I have for Assignment 4 but there are errors in the program, so any assistance would be greatly appreciated:
#include <stdio.h>
 main(void)
{
int num_deposit = 0, i, s;
 int num_withdrawal = 0;
 int c;
 float current_balance;
 float ending_balance;
 float total_deposit = 0;
 float deposit[50];
 float withdrawals[50];
printf(\"Welcome to the Computer Teller System\ \ \");
printf(\"Enter your current balance in dollars and cents: \");
scanf(\"%f\", ¤t_balance);
 
 while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
while(current_balance < 0)
{
       printf(\"***Beginning balance must be at least zero, please re-enter.***\");
   
   
        printf(\"Enter current balance in dollars and cents: \");
       scanf(\"%f\", ¤t_balance);
       
        while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
}
printf(\"\ Enter the number of deposits (0-50): \");
scanf(\"%d\", &num_deposit);
 
 while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
while(num_deposit < 0||num_deposit>50)
{
printf(\"***Invalid number of deposits, please re-enter.***\");
printf(\"Enter the number of deposits: \");
       scanf(\"%d\", &num_deposit);
       
        while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
}
printf(\"\ Enter the number of withdrawals (0-50): \");
scanf(\"%d\", &num_withdrawal);
 
 while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
while(num_withdrawal <0||num_withdrawal>50)
{
       printf(\"***Invalid number of withdrawals, please re-enter.***\");
 
        printf(\"Enter the number of withdrawals: \");
       scanf(\"%d\", &num_withdrawal);
       
        while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
}
for(i = 0; i < num_deposit; i++)
{
printf(\"\ Enter the amount of deposit #%d: \", i+1);
               scanf(\"%f\", &deposit[i]);
   
                while ( (c = getchar() != \'\ \') && c != EOF);
while(deposit[i]<0)
{
printf(\"/nDeposit amount must be greater than zero, please re-enter./n\");
printf(\"\ Enter the amount of deposit #%d: \", i+1);
       scanf(\"%f\", &deposit[i]);
       
        while ( (c = getchar() != \'\ \') && c != EOF);
}
total_deposit+=deposit[i];
fflush(stdin);
}
for(i = 0; i < num_withdrawal; i++)
{
printf(\"\ Enter the amount of withdrawal #%d: \", i+1);
               scanf(\"%f\", &withdrawals[i]);
       
                while ( (c = getchar() != \'\ \') && c != EOF);
while(withdrawals[i]<0)
{
printf(\"/nWithdrawal amount must be greater than zero, please re-enter.\ \");
printf(\"\ Enter the amount of withdrawal #%d: \", i+1);
       scanf(\"%f\", &withdrawals[i]);
       
        while ( (c = getchar() != \'\ \') && c != EOF);
}
if(withdrawals[i]>(current_balance+total_deposit))
{
printf(\"*** Withdrawal amount exceeds current balance. ***\ \");
}
else total_deposit -=withdrawals[i];
fflush(stdin);
}
ending_balance= current_balance+total_deposit;
printf(\"\ \");
printf(\"***The closing balance is :$%.2f\ ***\",ending_balance);
printf(\"\ \");
if(ending_balance>=50000)
printf(\"***It is time to invest some money!***\ \");
else if(ending_balance>=15000 && ending_balance<=49999.99)
printf(\"***Maybe you should consider a CD.***\ \");
else if(ending_balance>=1000 && ending_balance<=14999.99)
printf(\"***Keep up the good work!***\ \");
else if(ending_balance>=0 && ending_balance<=999.99)
printf(\"***Your balance is getting low!***\ \");
printf(\"***Teller Record***\ \");
printf(\"Starting Balance is $%.2f\ \ \",current_balance);
for(i = 0; i < num_deposit; i++)
{
printf(\"Deposit #%d : $%.2f\ \",i+1,deposit[i]);
}
printf(\"\ \");
for(i = 0; i < num_withdrawal; i++)
{
printf(\"Withdrawal #%d : $%.2f\ \",i+1,withdrawals[i]);
}
printf(\"\ \");
printf(\"Ending balance is :$%.2f\ \",ending_balance);
printf(\"\ \");
getch();
}
Solution
#include <stdio.h>
 main(void)
{
int num_deposit = 0, i, s;
 int num_withdrawal = 0;
 int c;
 float current_balance;
 float ending_balance;
 float total_deposit = 0;
 float deposit[50];
 float withdrawals[50];
printf(\"Welcome to the Computer Teller System\ \ \");
printf(\"Enter your current balance in dollars and cents: \");
scanf(\"%f\", ¤t_balance);
 
 while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
while(current_balance < 0)
{
       printf(\"***Beginning balance must be at least zero, please re-enter.***\");
   
   
        printf(\"Enter current balance in dollars and cents: \");
       scanf(\"%f\", ¤t_balance);
       
        while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
}
printf(\"\ Enter the number of deposits (0-50): \");
scanf(\"%d\", &num_deposit);
 
 while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
while(num_deposit < 0||num_deposit>50)
{
printf(\"***Invalid number of deposits, please re-enter.***\");
printf(\"Enter the number of deposits: \");
       scanf(\"%d\", &num_deposit);
       
        while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
}
printf(\"\ Enter the number of withdrawals (0-50): \");
scanf(\"%d\", &num_withdrawal);
 
 while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
while(num_withdrawal <0||num_withdrawal>50)
{
       printf(\"***Invalid number of withdrawals, please re-enter.***\");
 
        printf(\"Enter the number of withdrawals: \");
       scanf(\"%d\", &num_withdrawal);
       
        while ( (c = getchar() != \'\ \') && c != EOF);
fflush(stdin);
}
for(i = 0; i < num_deposit; i++)
{
printf(\"\ Enter the amount of deposit #%d: \", i+1);
               scanf(\"%f\", &deposit[i]);
   
                while ( (c = getchar() != \'\ \') && c != EOF);
while(deposit[i]<0)
{
printf(\"/nDeposit amount must be greater than zero, please re-enter./n\");
printf(\"\ Enter the amount of deposit #%d: \", i+1);
       scanf(\"%f\", &deposit[i]);
       
        while ( (c = getchar() != \'\ \') && c != EOF);
}
total_deposit+=deposit[i];
fflush(stdin);
}
for(i = 0; i < num_withdrawal; i++)
{
printf(\"\ Enter the amount of withdrawal #%d: \", i+1);
               scanf(\"%f\", &withdrawals[i]);
       
                while ( (c = getchar() != \'\ \') && c != EOF);
while(withdrawals[i]<0)
{
printf(\"/nWithdrawal amount must be greater than zero, please re-enter.\ \");
printf(\"\ Enter the amount of withdrawal #%d: \", i+1);
       scanf(\"%f\", &withdrawals[i]);
       
        while ( (c = getchar() != \'\ \') && c != EOF);
}
if(withdrawals[i]>(current_balance+total_deposit))
{
printf(\"*** Withdrawal amount exceeds current balance. ***\ \");
}
else total_deposit -=withdrawals[i];
fflush(stdin);
}
ending_balance= current_balance+total_deposit;
printf(\"\ \");
printf(\"***The closing balance is :$%.2f\ ***\",ending_balance);
printf(\"\ \");
if(ending_balance>=50000)
printf(\"***It is time to invest some money!***\ \");
else if(ending_balance>=15000 && ending_balance<=49999.99)
printf(\"***Maybe you should consider a CD.***\ \");
else if(ending_balance>=1000 && ending_balance<=14999.99)
printf(\"***Keep up the good work!***\ \");
else if(ending_balance>=0 && ending_balance<=999.99)
printf(\"***Your balance is getting low!***\ \");
printf(\"***Teller Record***\ \");
printf(\"Starting Balance is $%.2f\ \ \",current_balance);
for(i = 0; i < num_deposit; i++)
{
printf(\"Deposit #%d : $%.2f\ \",i+1,deposit[i]);
}
printf(\"\ \");
for(i = 0; i < num_withdrawal; i++)
{
printf(\"Withdrawal #%d : $%.2f\ \",i+1,withdrawals[i]);
}
printf(\"\ \");
printf(\"Ending balance is :$%.2f\ \",ending_balance);
printf(\"\ \");
getch();
}










