Solved

check my work - spot my errors

Posted on 1997-12-10
1
207 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)







 
0
Comment
Question by:fharris
1 Comment
 
LVL 1

Accepted Solution

by:
messiah earned 100 total points
ID: 1256578
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);
}

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now