Question

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

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

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

#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\n", a, b, c);
             else
                fprintf(fout, "%f %f %f\n", a, c, b);
         }
         else {
             if (a > c){
                fprintf(fout, "%f %f %f\n", b, a, c);
             else
                fprintf(fout, "%f %f %f\n", 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\n",count);
      fclose(fin);
      fclose(fout);
   }
   else {
       printf("Error opening file\n");
   }
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Please basic simple C language, so printf's and scanf's not cout or cin. Thank you so...
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
  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a...

    Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...

  • Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the...

    Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file inFile exists when mostFrequent is called; mostFrequent must create outFile. The input file contains only lower case letters and white space. The function mostFrequent identifies the letter(s) that appear most frequently on each line of inFile and writes them to a corresponding line of...

  • Using C programming...

    Write a text file contains a square matrix of integer numbers. The file contains an integer number indicating the identical number of rows and columns followed by the data itself (that number cannot be larger than 100). For example, a file containing a 3x3 matrix would contain 3 followed by 9 other integer numbers. The program will call one function to determine if all the numbers on the main diagonal are the same.The function is called checkdiag (int checkdiag (int matrix[][100], int...

  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

  • in c++ please HW09: Read/Write File Ints Now that we've had a taste of what file...

    in c++ please HW09: Read/Write File Ints Now that we've had a taste of what file I/O is all about, here's your chance to try it out on your own! You're to write a program that will prompt the user if he/she would like to read ints from a file (and have them displayed to stdout) or write ints to a file for safekeeping. If the user wishes to save a set of numbers, then the file nums.txt is opened...

  • IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...

    IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.

  • UNIX is all about manipulating files and input/output streams fluidly, so it is important to get a strong grasp of how...

    UNIX is all about manipulating files and input/output streams fluidly, so it is important to get a strong grasp of how this fundamentally works at the system call level to understand higher-level system programming concepts. Every program automatically has three file descriptors opened by the shell standard input standard output standard error 1 2 One can use read and write other open file. Normally, standard input and output on the terminal are line-buffered, so, for example, the specified number of...

  • Must use a function to compute the day. All cin and cout statements must appear in...

    Must use a function to compute the day. All cin and cout statements must appear in main(). Has to be in c++. lab 10 Problem write a program that prints the day number of the year, given the date in the form month-day year. For example, if the input is 1-1-2006, the day number is 1; if the input is 12 the day number is 359. The program should check for a leap year. A year is a leap year...

  • Please use C language with basic terms. Please fast, thank you so much :) Write a...

    Please use C language with basic terms. Please fast, thank you so much :) Write a function declared as "int categorize(char *str)" which accepts a string as an input parameter and categorize the string. The function should return the category number as decribed below: Category 1: If the string contains only capital letters (e.g. ABCDEF), the function should return 1. Category 2: If the string contains only small letters (e.g. abcdef), the function should return 2. Category 3: If the...

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