Skip to content

luc089.c

Problem Statement

Update 'CUSTOMER.DAT' balance using 'TRANSACTIONS.DAT' (Deposit/Withdrawal). Ensure balance doesn't fall below Rs. 100 on withdrawal.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)
Difficulty Advanced (index: 6 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Array
  • Pointers
  • Iteration
  • Sorting (possible)
  • Recursion

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct customer {
    int accno;
    char name[30];
    float balance;
};

struct trans {
    int accno;
    char trans_type;
    float amount;
};

void create_files();
void update_customer(struct customer *c, int n, struct trans t);

int main()
{
    FILE *fc, *ft;
    struct customer cust[100];
    struct trans t;
    int i, n_cust = 0;

    create_files(); // Generate dummy data

    // 1. Load all customers into memory
    fc = fopen("CUSTOMER.DAT", "rb");
    if (fc == NULL) exit(1);

    while (fread(&cust[n_cust], sizeof(struct customer), 1, fc) == 1)
    {
        n_cust++;
    }
    fclose(fc);

    // 2. Process transactions sequentially
    ft = fopen("TRANSACTIONS.DAT", "rb");
    if (ft == NULL) exit(2);

    printf("Processing Transactions...\n");
    while (fread(&t, sizeof(struct trans), 1, ft) == 1)
    {
        update_customer(cust, n_cust, t);
    }
    fclose(ft);

    // 3. Write updated data back to CUSTOMER.DAT
    fc = fopen("CUSTOMER.DAT", "wb");
    fwrite(cust, sizeof(struct customer), n_cust, fc);
    fclose(fc);

    printf("Update Complete. New Balances:\n");
    for(i=0; i<n_cust; i++)
        printf("%d %s: %.2f\n", cust[i].accno, cust[i].name, cust[i].balance);

    return 0;
}

void update_customer(struct customer *c, int n, struct trans t)
{
    int i;
    for (i = 0; i < n; i++)
    {
        if (c[i].accno == t.accno)
        {
            if (t.trans_type == 'D')
            {
                c[i].balance += t.amount;
                printf("Acc %d: Deposited %.2f\n", t.accno, t.amount);
            }
            else if (t.trans_type == 'W')
            {
                if ((c[i].balance - t.amount) >= 100)
                {
                    c[i].balance -= t.amount;
                    printf("Acc %d: Withdrew %.2f\n", t.accno, t.amount);
                }
                else
                {
                    printf("Acc %d: Withdrawal denied (Min bal constraint)\n", t.accno);
                }
            }
            return;
        }
    }
    printf("Transaction Error: Acc %d not found\n", t.accno);
}

void create_files()
{
    struct customer c[] = {{101, "A", 500}, {102, "B", 1000}, {103, "C", 200}};
    struct trans t[] = {{101, 'D', 200}, {102, 'W', 500}, {103, 'W', 150}}; // 103 fail
    FILE *f1 = fopen("CUSTOMER.DAT", "wb");
    FILE *f2 = fopen("TRANSACTIONS.DAT", "wb");
    fwrite(c, sizeof(struct customer), 3, f1);
    fwrite(t, sizeof(struct trans), 3, f2);
    fclose(f1); fclose(f2);
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc089.c):

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    struct customer {
        int accno;
        char name[30];
        float balance;
    };

    struct trans {
        int accno;
        char trans_type;
        float amount;
    };

    void create_files();
    void update_customer(struct customer *c, int n, struct trans t);

    int main()
    {
        FILE *fc, *ft;
        struct customer cust[100];
        struct trans t;
        int i, n_cust = 0;

        create_files(); // Generate dummy data

        // 1. Load all customers into memory
        fc = fopen("CUSTOMER.DAT", "rb");
        if (fc == NULL) exit(1);

        while (fread(&cust[n_cust], sizeof(struct customer), 1, fc) == 1)
        {
            n_cust++;
        }
        fclose(fc);

        // 2. Process transactions sequentially
        ft = fopen("TRANSACTIONS.DAT", "rb");
        if (ft == NULL) exit(2);

        printf("Processing Transactions...\n");
        while (fread(&t, sizeof(struct trans), 1, ft) == 1)
        {
            update_customer(cust, n_cust, t);
        }
        fclose(ft);

        // 3. Write updated data back to CUSTOMER.DAT
        fc = fopen("CUSTOMER.DAT", "wb");
        fwrite(cust, sizeof(struct customer), n_cust, fc);
        fclose(fc);

        printf("Update Complete. New Balances:\n");
        for(i=0; i<n_cust; i++)
            printf("%d %s: %.2f\n", cust[i].accno, cust[i].name, cust[i].balance);

        return 0;
    }

    void update_customer(struct customer *c, int n, struct trans t)
    {
        int i;
        for (i = 0; i < n; i++)
        {
            if (c[i].accno == t.accno)
            {
                if (t.trans_type == 'D')
                {
                    c[i].balance += t.amount;
                    printf("Acc %d: Deposited %.2f\n", t.accno, t.amount);
                }
                else if (t.trans_type == 'W')
                {
                    if ((c[i].balance - t.amount) >= 100)
                    {
                        c[i].ba
    ... (truncated for brevity)