Balancing Accounts – Solution

I hope you had a wee bit of a struggle with your solution to this month’s Exercise. It involves a lot of decisions and proper steps in the right order to hold off on debits so that the bank account never dips below zero.

The pathetically easy way to devise a solution is to tally all the deposits and withdrawals, then add them to get the final balance. Here you go:

2021_04-Exercise-a.c

#include <stdio.h>

int main()
{
    float deposit[20] = {
        10.0, -5.0, -10.0, 5.0, 15.0, -10.0, 5.0, -10.0, 5.0, -15.0,
        -5.0, 10.0, 15.0, -15.0, 5.0, 15.0, -5.0, 10.0, -10.0, -5.0
    };
    float a,debit;

    a = debit = 0.0;
    for(int x=0; x<20; x++)
    {
        if( deposit[x] < 0.0 )
            debit += deposit[x];
        else
            a += deposit[x];
    }

    /* show results */
    printf("A total of $%.2f in deposits made\n",a);
    printf("A total of %.2f in withdrawals made\n",debit);
    printf("Final balance: $%.2f\n",a+debit);

    return(0);
}

The for loop at Line 12 tallies values in the deposit[] array, accumulating positive values in variable a and negative values in variable debit. At Line 21, a series of printf() functions output the results, boring and ho-hum:

A total of $95.00 in deposits made
A total of -90.00 in withdrawals made
Final balance: $5.00

I would prefer that you instead approach the problem one array element at a time, which is what I did for my real solution:

2021_04-Exercise-b.c

#include <stdio.h>

int main()
{
    float deposit[20] = {
        10.0, -5.0, -10.0, 5.0, 15.0, -10.0, 5.0, -10.0, 5.0, -15.0,
        -5.0, 10.0, 15.0, -15.0, 5.0, 15.0, -5.0, 10.0, -10.0, -5.0
    };
    float a,debit;

    a = debit = 0.0;
    for(int x=0; x<20; x++)
    {
        printf("Bal: $%.2f\t",a);
    
        if( deposit[x] > 0.0 )
        {
            printf("%+.2f ",deposit[x]);
            a += deposit[x];
            /* check to see if debits can be taken */
            if( a+debit>0.0 && debit!=0.0 )
            {
                printf("add debit total %.2f ",debit);
                a += debit;
                debit = 0.0;
            }
            printf("= $%4.2f",a);
        }
        else
        {
            if( a+deposit[x] > 0 )
            {
                printf("%+.2f ",deposit[x]);
                a += deposit[x];
                printf("= $%4.2f",a);
            }
            else
            {
                debit += deposit[x];
                printf("%.2f added to debits",deposit[x]);
            }
        }
        putchar('\n');
    }

    return(0);
}

Within the for loop, an if-else decision separates the positive and negative elements in the deposit[] array.

At Line 16, if the value of deposit[x] is positive, it’s added the cumulative balance in variable a. Then at Line 21, a test is made to determine if the current value of debit can be subtracted ( a+=debit>0.0 ). If so, the total is reduced at Line 24, a += debit. At Line 25, the value of debit is zeroed, allowing for further negative values to accumulate.

The else part of the decision at Line 29 handles withdrawals, or negative elements in the deposit[] array. At Line 31, if the withdrawal can be subtracted, it is. Otherwise, at Line 39, the value of the withdrawal is added to the cumulative debit total, debit += deposit[x].

Here is the output:

Bal: $0.00	+10.00 = $10.00
Bal: $10.00	-5.00 = $5.00
Bal: $5.00	-10.00 added to debits
Bal: $5.00	+5.00 = $10.00
Bal: $10.00	+15.00 add debit total -10.00 = $15.00
Bal: $15.00	-10.00 = $5.00
Bal: $5.00	+5.00 = $10.00
Bal: $10.00	-10.00 added to debits
Bal: $10.00	+5.00 add debit total -10.00 = $5.00
Bal: $5.00	-15.00 added to debits
Bal: $5.00	-5.00 added to debits
Bal: $5.00	+10.00 = $15.00
Bal: $15.00	+15.00 add debit total -20.00 = $10.00
Bal: $10.00	-15.00 added to debits
Bal: $10.00	+5.00 = $15.00
Bal: $15.00	+15.00 add debit total -15.00 = $15.00
Bal: $15.00	-5.00 = $10.00
Bal: $10.00	+10.00 = $20.00
Bal: $20.00	-10.00 = $10.00
Bal: $10.00	-5.00 = $5.00

I hope you found this Exercise challenging. I decided to amp up the difficulty a tad, though if you opted for the easier solution (presented first), I don’t blame you. It takes some deft action to determine how negative values are held off until the balance can accept them. I hope your solution was effective and clever.

Leave a Reply