50033. Accounts

I'm a slow walker, but I never walk backwards.

Problem Description

Write as program to manage a set of accounts within a binary file. An account has the following information.

account.h

1
2
3
4
5
6
7
8
9
10
11
#ifndef _ACCOUNT_H
#define _ACCOUNT_H
 
typedef struct account {
    int balance;
    int accountNum;
    int zipcode;
    int age;
} Account;
 
#endif

First you need to read the name of the file from the standard input. After you read the account information from the file, you need to write a summary report.

  • A sorted listing according to increasing accountNum. All accountNum are different.
  • A summary according to the preprocessor flag SORTBY
    • If the SORTBY flag is ZIPCODE, then you need to print the sum of balance of all accounts from the same zipcode, sorted by increasing zip code.
    • If the SORTBY flag is AGE, then you need to print the sum of balance of all accounts from the same age, sorted by increasing age.

You may assume that the constants AGE and ZIPCODE are 1 and 2 respectively.

Subtasks

  • 5pt. There is only one record in the file, and the SORTBY flag is ZIPCODE.
  • 10pt. There are no more than 3 records in the file, all zip codes are different, and the SORTBY flag is ZIPCODE.
  • 15pt. There are no more than 10 records in the file, some zip codes may be the same, and the SORTBY flag is ZIPCODE.
  • 20pt. There are no more than 1000 records in the file, and the SORTBY flag is AGE.
  • 50pt. There are no more than 100000 records in the file.

Hints

If we sort the array according to a field, it becomes much easier to sum up all balance from that field.

Sample Input

p10075-sample.bin

file download

Sample Output (if a #define SORTBY ZIPCODE is defined)

account, age, zipcode, balance
0, 24, 100, 6000
2, 24, 101, 6200
3, 25, 100, 6500
5, 24, 100, 6300
7, 26, 101, 6000
zipcode, sum_balance
100, 18800
101, 12200

Sample Output (if a #define SORTBY AGE is defined)

account, age, zipcode, balance
0, 24, 100, 6000
2, 24, 101, 6200
3, 25, 100, 6500
5, 24, 100, 6300
7, 26, 101, 6000
age, sum_balance
24, 18500
25, 6500
26, 6000

Compile

$ gcc -std=c99 -O2 a.c -DSORTBY=ZIPCODE -DAGE=1 -DZIPCODE=2 -o testzip.out
$ gcc -std=c99 -O2 a.c -DSORTBY=AGE -DAGE=1 -DZIPCODE=2 -o testage.out

part of main.c

#define AGE 1
#define ZIPCODE 2

Discussion