Question

2. Read in an unknown number of real values from a file called “data.txt”. Calculate the...

2. Read in an unknown number of real values from a file called “data.txt”. Calculate the average of the real numbers. Output the average of the real numbers to a file called “output.txt”. Some skeleton code is provided for you below.

#include

  

int main (void)

{

                        FILE *infile = NULL;

                        FILE *outfile = NULL;

                        /* Fill in the code to open a file. Make sure you check that the file was open successfully. */

                        while (!feof (infile))

                        {

                                    /* Read in the real values. */

                                    /* Keep track of the number of values read and the sum of the values. */

                        }

                        /* Output average to a file named "output.txt". */

                        /* Close your files. */

                        return 0;

}

Write code in C

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

#include <stdio.h>
#include<stdlib.h>
int main()
{
   double i,sum,avg;
   int count=0;

   FILE *infile = NULL;
   FILE *outfile = NULL;


   infile = fopen("data.txt", "r"); /* openig file in read mode */

   /* checking that the file was open successfully or not */
   if(infile==NULL)
   {
       printf("Error in opening data.txt file\n");
       exit(1);
   }

   // read 1st number
   fscanf (infile, "%lf", &i);

   sum = 0.0;

   // continuously read all number
   while (!feof (infile))
    {
        count++; /* count total number of real value are present*/

        /* sum of the values. */
        sum += i ;

         fscanf (infile, "%lf", &i);
        
        
    }

    avg = sum/count; /* calculate average */


    outfile = fopen("output.txt", "w"); /* opening output file in write mode */

    /* checking that the file was open successfully or not */
   if(outfile==NULL)
   {
       printf("Error in opening output.txt file\n");
       exit(2);
   }


   fprintf(outfile,"%lf ",avg); /*wrire average to output file*/

   fclose(infile); /* closing input file */
   fclose(outfile); /* closing output file */
    
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
2. Read in an unknown number of real values from a file called “data.txt”. Calculate the...
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
  • The input file should be called 'data.txt' and should be created according to the highlighted instructions...

    The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain (in order): the weight (a number greater than zero and less than or equal to 1), the number, n, of lowest numbers to drop, and the...

  • How do I complete my code using an external file? External file: data.txt //the number 6...

    How do I complete my code using an external file? External file: data.txt //the number 6 is in the external file Incomplete code: #include <iostream> #include <fstream> using namespace std; class example {    int data[1]; public:    example();    void read(ifstream &); }; example::example() {    ifstream infile;    infile.open("data.txt");    } void example::read(ifstream &infile) {    infile >> data[1];    cout << data[1] << endl;    } int main() {    example example, read; //system("pause");    return 0;...

  • Write a program to read the contents of data.txt file, and determine the smallest value (min),...

    Write a program to read the contents of data.txt file, and determine the smallest value (min), largest value (max), and the average value (ave). The minimum, maximum, and average values must be calculated in the function calc(). Remember to declare the stdlib.h library, so that you can use the atof function, which will convert the number in the text file into float number. Also, remember to check if there is an error opening the file or not. If there is...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • For this assignment, you are to write a program that does the following: • read exam scores from a file name data.txt into an array, and • after reading all the scores, output the following to the mon...

    For this assignment, you are to write a program that does the following: • read exam scores from a file name data.txt into an array, and • after reading all the scores, output the following to the monitor: the average score and the total number of scores. In addition, the program must use the following functions: //precondition: fileName is name of the file to open, inFile is the input file 2 of 2 //postcondition: inFile is opened. If inFile cannot...

  • I am told to create a fillArray method that will read integers from the file "data.txt"...

    I am told to create a fillArray method that will read integers from the file "data.txt" and store them in the array. I'm told to assume that the file has no more than 100 items in it. This is what I have so far: public void fillArray() { int curVal;    Scanner input = null; try { input = new Scanner(new File("data.txt")); // set the current number of items in the array to zero while (input.hasNextInt()) { curVal = input.nextInt();...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for...

    Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for writing and reading from console #include <fstream> //this one to deal with files, read and write to text files using namespace std; int main() { //first thing we need to read the data in the text file //let's assume we have the reading in a text file called data.txt //so, we need a stream input variable to hold this data ifstream infile; //now we...

  • I need help with the following code... Instructions: This lab builds on the skills from Lab...

    I need help with the following code... Instructions: This lab builds on the skills from Lab 2c, in which you read data values from a file and kept a count of the number of invalid values. In this lab, your program will be using the same code to read each data entry from the file, but you will also save each value in an array named allMags after each is read in. When all values in the file have been...

  • create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in...

    create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain...

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