[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

check my work - spot my errors

Posted on 1997-12-10
1
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
messiah earned 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

649 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