Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CORRECT THIS C PROGRAM FOR ME PLEASE. Suppose that at a particular steel mill, s

ID: 3927656 • Letter: C

Question

CORRECT THIS C PROGRAM FOR ME PLEASE.

Suppose that at a particular steel mill, steel bars are grouped into classes according to their yield tensile strength as shown in the following table (note that this is not a standard classification):

Class Tensile strength (MPa)

-------------------------------------------------

A 1000.0 <= strength

B 800.0 <= strength < 1000.0

C 600.0 <= strength < 800.0

D 400.0 <= strength < 600.0

E strength < 400.0

Write a program which reads a yield tensile strength from the file steelclassin.txt, calls the function class to determine the corresponding class and then the main program writes the strength and corresponding class to a file steelclassout.txt. This continues until the end of the input file is reached. Your output file should look like:

Tensile Strength    Class
----------------------------
649.4 C
827.3       B
989.7       B
453.6       D
278.3       E

THIS IS MY OUTPUT:

Tensile Strength    Class
----------------------------
827.3       B
989.7       B
453.6       D
278.3       E

IT won't print the first number and I don't know why.

Here's my code:

#include<stdio.h>

char class(float strength);

int main(void){
  
   float strength;
   char answer;
   FILE * fin = fopen("steelclassin.txt", "r");
   FILE * fout = fopen("steelclassout.txt", "w");
  
   fscanf(fin, "%f", &strength);
   fprintf(fout, " Tensile Strength    Class ");
   fprintf(fout, "---------------------------- ");
  
   while(fscanf(fin, "%f", &strength)!=EOF){
      
       answer = class(strength);
       fprintf(fout, "%.1f       %c ", strength, answer);
      
   }
  
   fclose(fin);
   fclose(fout);
   system("notepad.exe steelclassout.txt");
   return 0;
}

char class(float strength){
  
   char answer;
  
   if(strength >= 1000.0){
      
       answer = 'A';
      
   }else if(strength >= 800.0){
      
       answer = 'B';
      
   }else if(strength >= 600.0){
      
       answer = 'C';
      
   }else if(strength >= 400.0){
      
       answer = 'D';
      
   }else{
      
       answer = 'E';
   }
  
   return answer;
  
}

Explanation / Answer


#include<stdio.h>
char class(float strength);
int main(void){
  
float strength;
char answer;
FILE * fin = fopen("steelclassin.txt", "r");
FILE * fout = fopen("steelclassout.txt", "w");
  
//
//fscanf(fin, "%f", &strength);
fprintf(fout, " Tensile Strength Class ");
fprintf(fout, "---------------------------- ");
  
//We are reading values from the file.
while(fscanf(fin, "%f", &strength)!=EOF){
  
answer = class(strength);
fprintf(fout, "%.1f %c ", strength, answer);
  
}
  
fclose(fin);
fclose(fout);
system("notepad.exe steelclassout.txt");
return 0;
}
char class(float strength){
  
char answer;
  
if(strength >= 1000.0){
  
answer = 'A';
  
}else if(strength >= 800.0){
  
answer = 'B';
  
}else if(strength >= 600.0){
  
answer = 'C';
  
}else if(strength >= 400.0){
  
answer = 'D';
  
}else{
  
answer = 'E';
}
  
return answer;
  
}