Homework Help Question & Answers

Question 9 [13 marks] A file called tanks.dat consists of the width, height and depth values for a number of water...


Question 9 [13 marks] A file called tanks.dat consists of the width, height and depth values for a number of water tanks. The
Question 9 [13 marks] A file called tanks.dat consists of the width, height and depth values for a number of water tanks. The values are all floating point and the units are metres. A typical file is shown below: 10 2.0 2.0 1.0 3.0 0.5 1.5 1.0 2.5 0.5 1.0 1.0 0.25 0.5 0.25 0.25 5.0 1.2 1.3 2.5 1.1 0.6 0.8 0.8 0.8 0.7 0.7 0.6 0.5 0.3 0.2 The first row of the file contains the number of triples of dimensions for each tank i.e. number of lines to read (in this case 10). Each of the rest of the lines contains the width, height and depth of a tank in that order. Your task is to create a C program to read the data in from the file and store it in suitable arrays. Assume the maximum number of triples that could be in the file is 100. Use appropriate error checking to deal with the file not existing. [4 marks for appropriate variables and constants, 4 marks for reading from the file, 5 marks for processing the data, total of 13 marks].
0 0
Next > < Previous
ReportAnswer #1

#include<stdio.h>
#include<stdlib.h>
int main(){

FILE *fp;
fp = fopen("tanks.dat","r");
if (!fp){
   printf("File is not present");
}
else{

   float width[100],height[100],depth[100];
   int nol;// no of lines in the file for the tripplet values
   float wid,he,dep;
   int i=0,j=0,k,result;
   fscanf(fp,"%d",&nol); // reads the first line in the file
   // this while loop is to iterate over the file and get the triplet values
   while(1){

       result = fscanf(fp,"%f %f %f\n",&wid,&he,&dep);
       if (result == EOF) //it checks for end of the file it will break when we came to end of file
           break;

            width[j] = wid; //store the values into respective arrays
        height[j] = he;
        depth[j] = dep;
        j++;

  
   }
   // for printing values
   printf("------------the values of widths are------ \n");
   for (k=0;k<nol;k++){
  
   printf("%f\n",width[k]);
   }

              printf("------------the values of heights are------ \n");
                      for (k=0;k<nol;k++){

                              printf("%f\n",height[k]);
                                  }

                  printf("------------the values of depths are------ \n");
                          for (k=0;k<nol;k++){

                                  printf("%f\n",depth[k]);
                                      }


}
return 0;
}

Add Homework Help Answer
Add Answer of:
Question 9 [13 marks] A file called tanks.dat consists of the width, height and depth values for a number of water...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE Expert Answers
WITHIN MINUTES
Related Questions