We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

check my work - spot my errors

fharris
fharris asked
on
Medium Priority
232 Views
Last Modified: 2010-04-02
would this create an array (called record) of structures reading info into each from the file cartran.dat and putting the stuff read in and some calculations from it into report1.dat.  Also, how would I read it in to the other file report2.e the reading and writing part of a function call and call it for both files - rep1 and rep2????

# include <stdio.h>
# include <math.h>
# define N 25

struct data {     char transcode[10]
                        char cartype[15]
                        int pp;
                        int dp;
            float yir;
                          int mtp;
           };
struct package  {
                struct data entry;
                float monthly payment;
                int amt_borr;
                float total_int;
                         };

           
/*calculates the monthlypayments required by the nature of the loan*/
/*is given x(purchaseprice of the car,z(downpayment made on the car)*/
/*y(the yearly interest rate), and m(the months to pay or duration of the payback period)*/

float carpayment(int x, int z, float y, int m);
double power(int x, int n);


int main()
{
FILE  *fpi, *fpo;
struct data entry;
struct package record[ N];


/*This checks that the files are open*/

if((fpi=fopen(“cartran.dat”, “r”))==NULL)
{printf(“cannot open cartran.dat for reading.\n”);
exit(1);}
if((fpo=fopen(“report1.dat”,”w”))==NULL)
{printf(“cannot open report1.dat for writing”))==NULL)
exit(1);}
if((fpo=fopen(“report2.dat”,”w”))==NULL)
{printf(“cannot open report2.dat for writing”))==NULL)

/*this prints out the headings for the data to be entered*/

fprintf(fpo,”transcode    cartype   pp   dp   amt_borr   yir   mtp   mp  total_int \n”);
fprintf(fpo,”============================================”);


/*This scans the input file and prints the desired info into the output file*/

for(i=0; i<NUMBER_OF_RECORDS;++i)
   while(fscanf(fpi,”%s%s%d%d%f%d”, record[i].entry.transcode,                               record[i].entry.cartype, record[i].entry.pp, record[i].entry.dp,  record[i].entry.yir,              record[i].entry.mtp)!=EOF)
         {
            record[i].monthlypayment=carpayment(record[i].entry.pp, record[i].entry.dp,                record[i].entry.yir, record[i].entry.mtp);
      record[i].amt_borr= record[i].entry.pp- record[i].entry.dp;
      record[i].total_int=(monthlypayment* record[i].entry.mtp)-( amt_borr);
            fprintf(fpo,”%s %s %d %d %d %6.2f %d %6.2f %6.2f”,       record[i].entry.transcode, record[i].entry.cartype, record[i].entry.pp,       record[i].entry.dp, record[i].amt_borr, record[i].entry.yir, record[i].entry.mtp,       record[i].monthlypayment, record[i].total_int);
         }


return (0);
}

/*precondition: recieves purchase price, downpayment, yearly interest rate and months to pay*/
/*postcondition: calculates and returns the monthly payments on the loan*/


car payment ( pp, dp, yir, mtp)   /*function that calculates the monthly payment required.*/      
   {                                               /*...including interest on loan*/
     int n, x, amt_borr;
     float mir, a, b, bracket, monthlypayment;
     
     mir=yir/12/100;
     n=mtp;
     x=mir+1;
     amt_borr=pp-dp;
     bracket=power(x,n)
 
     
    a=n*amt_borr;
    b=bracket/bracket-1;
    return monthlypayment;
  }

/*precondition: recieves two integers x and n*/
/*postcondition: returns x to the nth value*/

power(x,n)            /*recursive function that computes x to the n power*/
  { if(n==0)
        return 1;
     else
        return x* power(x, n-1)







 
Comment
Watch Question

Commented:
Here is a quickie version of your program
that's off the top of my head.. enjoy

// This should work, or give you a clue :)

#include <stdio.h>
#include <math.h>
#define  N 25

typedef struct
{
      char transcode[10];
      char cartype[15];
      int pp;
      int dp;
      float yir;
      int mtp;
} data;
typedef struct
{
      data entry;
      float monthlypayment;
      int amt_borr;
      float total_int;
} package;

float carpayment(int x, int z, float y, int m);
double power(int x, int n);

int main()
{
      FILE *fpi, *fpo, *fpe;
      data entry;
      package record[N];

      /* Save them to binary files.. it's a lot easier */
      fpi = fopen("cartran.dat", "rb");
      fpo = fopen("report1.dat", "wb");
      fpe = fopen("report2.dat", "wb");

      if(fpi == NULL || fpo == NULL || fpe == NULL)
      {
            printf("\nFile I/O Error\n");
            return -1;
      }

      for(int i=0; i < N; i++)
            while(!feof(fpi))
            {
                  fread(&record, sizeof(record), 1, fpi);
                  record[i].monthlypayment=carpayment(record[i].entry.pp, record[i].entry.dp, record[i].entry.yir, record[i].entry.mtp);
                  record[i].amt_borr= record[i].entry.pp- record[i].entry.dp;
                  record[i].total_int=(record[i].monthlypayment* record[i].entry.mtp)-(record[i].amt_borr);
                  fwrite(&record, sizeof(record), 1, fpe);
            }

fcloseall();
return (0);
}

float carpayment(pp,dp,yir,mtp)
{
 int n, x, amt_borr;
 float mir, a, b, bracket, monthlypayment;

 mir=yir/12/100;
 n=mtp;
 x=mir+1;
 amt_borr=pp-dp;
 bracket=power(x,n)

 a=n*record[i].amt_borr;
 b=bracket/bracket-1;
 return monthlypayment;
}

double power(x,n) /*recursive function that computes x to the n power*/
{
      if(n==0) return 1;
      else return x* power(x, n-1);
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.