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

Please basic simple C language, so printf\'s and scanf\'s not cout or cin. Thank

ID: 3867632 • Letter: P

Question

Please basic simple C language, so printf's and scanf's not cout or cin. Thank you so much!

The input file tripletsin.txt consists of a series of lines, each of which contains three numbers (a triplet). For example the input file might contain the following:

3.2 -4.1 8.6

5.6 1.2 0.3

Write a function called triplets which reads these lines one at a time and writes out the numbers ordered from smallest to largest to an output file called tripletsout.txt. Your function must return the number of triplets (i.e. lines) read. Also, your function must not make use of any functions other than fscanf and fprintf. That is, you must devise the required if-else structure to accomplish the required ordering yourself. Write a main function to test your triplets function. The main must open and close the input and output files (this must not be done in the function), call the function, and then print out the number of triplets read. For example, if triplesin.txt contains the lines indicated above, then triplesout.txt should contain

-4.100000 3.200000 8.600000

0.3000000 1.200000 5.600000

and the main should report

Number of triplets read = 2

Explanation / Answer

#include<stdio.h>

int triplets(FILE *fin, FILE *fout){

   float a,b,c;
   float temp;
   int count = 0;
   int i,j;

   while (fscanf(fin, "%f %f %f", &a, &b, &c) != EOF){
         count++;
         if (a > b) {
             if (b > c){
                fprintf(fout, "%f %f %f ", a, b, c);
             else
                fprintf(fout, "%f %f %f ", a, c, b);
         }
         else {
             if (a > c){
                fprintf(fout, "%f %f %f ", b, a, c);
             else
                fprintf(fout, "%f %f %f ", b, c, a);

         }
        
       
   }
   return count;
}

int main(){


   FILE *fin,*fout;
   int count;

   fin = fopen("tripletsin.txt", "r");
   fout = fopen("tripletsout.txt", "w");
   if (fin != NULL){
      count = triplets(fin,fout);
      printf("Number of triplets read = %d ",count);
      fclose(fin);
      fclose(fout);
   }
   else {
       printf("Error opening file ");
   }
  
   return 0;
}