COMP1721 - Higher Computing 1B

Computing 1B - Week 06 Tutorial Solutions

  1. Code to find the name of the customer with the largest balance in a single account.

    enum {
    	MAX_ACCOUNTS = 5,
    	N_CUSTOMERS = 1000
    };
    
    struct customer {
    	char   name[20];
    	int    numberOfAccounts;
    	double balance[MAX_ACCOUNTS];
    };
    
    struct customer customers[N_CUSTOMERS];
    ...
    int    i, richest;
    double maxbalance;
    ...
    richest = 0; /* first is richest seen so far ... */
    for (i = 1; i < N_CUSTOMERS; ++i) {
    	for (j = 0; j < customers[i].numberOfAccounts; ++ j) {
    		if (customers[i].balance[j] > maxBalance) {
    			richest = i;
    			maxBalance = customers[i].balance[j];
    		}
    	}
    }
    printf("Richest customer is %s\n", customers[richest].name);
    


Andrew Taylor (andrewt@cse.unsw.edu.au)
Higher Computing 1B, Computer Science & Engineering, UNSW