Question

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

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\n");
   fprintf(fout, "----------------------------\n");
  
   while(fscanf(fin, "%f", &strength)!=EOF){
      
       answer = class(strength);
       fprintf(fout, "%.1f       %c\n", 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;
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#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\n");
fprintf(fout, "----------------------------\n");
  
//We are reading values from the file.
while(fscanf(fin, "%f", &strength)!=EOF){
  
answer = class(strength);
fprintf(fout, "%.1f %c\n", 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;
  
}

Add a comment
Know the answer?
Add Answer to:
CORRECT THIS C PROGRAM FOR ME PLEASE. Suppose that at a particular steel mill, steel bars...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • Please write the complete code in C. Write a C function that prints the minimum spanning...

    Please write the complete code in C. Write a C function that prints the minimum spanning tree of a graph. At the end, print the weight of the spanning tree. A suggested report format is shown in the following example. Source Vertex To Vertex Weight A B 2 A C 4 B D 3 D E 1 Total weight of spanning tree: 10 Your main program will read a graph from DataIn file to an adjacency table before calling the...

  • For the following task, I have written code in C and need help in determining the...

    For the following task, I have written code in C and need help in determining the cause(s) of a segmentation fault which occurs when run. **It prints the message on line 47 "printf("Reading the input file and writing data to output file simultaneously..."); then results in a segmentation fault (core dumped) I am using mobaXterm v11.0 (GNU nano 2.0.9) CSV (comma-separated values) is a popular file format to store tabular kind of data. Each record is in a separate line...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • C++ Can someone please help me with this problem- commenting each line of code so I...

    C++ Can someone please help me with this problem- commenting each line of code so I can understand how to solve this problem using the C++ programming language? I really need help understanding how to create a file for the program to read. Do I create the file in Visual basic or create a text file? I have the code, just need to know how to create the file for it to read. #include<fstream> #include<iostream> using namespace std; int main()...

  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

  • Design and implement a C version of the color match program. As a starting point, use...

    Design and implement a C version of the color match program. As a starting point, use the file HW2-1-shell.c. The program should employ a reasonable algorithm that compares all pairings of colors in the palette exactly once. A color should not be compared to itself. Nor should two colors be compared to each other more than once. Your C program should print out the total component color difference for the closest pair using the print string provided in the shell...

  • PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks...

    PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks from your mystocks.txt file. You can use this information for the simulator. 2.)The stock price simulator will return the current price of the stock. The current prices is the prices of the requested ticker + a random number. 3.)We will assume that the stock price remains constant after the price check till your trade gets executed. Therefore, a buy or a sell order placed...

  • hey, this is a filei/o homework. um please show me how to do this (im using...

    hey, this is a filei/o homework. um please show me how to do this (im using ONLY arraylist for the first part so please continue on that) i have put my work please fix some mistakes and continue on it. the file is named “tools.txt” its a text document file that i savedin the netbeansprojects file. um pleas euse netbeans and show me the output afterwards, make it simple and continue on what i have worked on whilw fixing some...

  • // READ BEFORE YOU START: // You are given a partially completed program that creates a...

    // READ BEFORE YOU START: // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, gender, class, standard, and roll_number. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT