Hi Henry Rich.
I found your book on J for C programmers extremely enlightening.
While reading it, I found that the following C program has several errors
:
#include <stdio.h>

#define MAXACCT 500

// Program to process journal and account files, printing

// start/end/avg balance.  Parameters are # days in current

// month, filename of Accounts file, filename of Journal file

void acctprocess(int daysinmo, char * acctfn, char *jourfn)

{

     FILE fid;

     int nacct, acctx;

     float acctno, openbal, xactnday, xactnamt

     struct {

           float ano;            // account number

           float openbal;    // opening balance

           float prevday;    // day number of last activity

           float currbal;      // balance after last activity

           float weightbal; // weighted balance: sum of closing balances

     } acct[MAXACCT];

// Read initial balances; set day to start-of-month, sum of balances to 0

     fid = fopen(acctfn);

     for(nacct = 0;2 == fscanf(fid,"%f%f",acctno,openbal) {

           acct[nacct].ano = acctno;

           acct[nacct].openbal = openbal;

           acct[nacct].prevday = 1;

           acct[nacct].currbal = openbal;

           acct[nacct].weightbal = 0;

           ++nacct;

     }

     fclose(acctfn);

     // Process the journal: for each record, look up the account

     // structure; add closing-balance values for any days that

     // ended before this journal record; update the balance

     fid = fopen(jourfn);

     while(3 == fscanf(fid,"%f%f%f",acctno,xactnday,xactnamt) {

           for(acctx = 0;acct[acctx].ano != acctno;++acctx);

           acct[nacct].weightbal +=

                 acct[nacct].currbal * (xactnday - acct[nacct].prevday);

           acct[nacct].currbal += xactnamt;

           acct[nacct].prevday = xactnday;

     }



     // Go through the accounts.  Close the month by adding

     // closing-balance values applicable to the final balance;

     // produce output record

     for(acctx = 0;acctx < nacct;++acctx) {

           acct[nacct].weightbal +=

                 acct[nacct].currbal * (daysinmo - acct[nacct].prevday);

           printf("Account %d: Opening %d, closing %d, avg %d\n",

                 acct[acctx].ano, acct[acctx].openbal, acct[acctx].currbal,

                  acct[acctx].weightbal/daysinmo);

     }

     fclose(fid);

}



On another subject, I kindly ask your permission to translate your book into Spanish.
Regards.
Ricardo M. Forno.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to