Question

Write a program that will take input from a file of numbers of type double and...

Write a program that will take input from a file of numbers of type double and output the average of the numbers in the file to the screen.

Output the file name and average.

Allow the user to process multiple files in one run.

Part A

use an array to hold the values read from the file

modify your average function so that it receives an array as input parameter, averages values in an array and returns the average

Part B

it sorts the array (Use the textbook function sort() , but you will need to change it to work with type double)

it prints the sorted array values in the output function along with the file name and the average

Use the following test data to test your program (You created files for assignment five and can reuse those):

File name

Values

class1.txt

95.6 78.3 88.8 46.7 98.5 67.1 84.0 70.3 82.4 73.6

class2.txt

95.6 78.3 88.8 46.7 98.5 67.1 84.0

class3.txt

95.6 78.3 88.8 46.7 98.5

class4.txt

70.0 80.0 90.0

Turn in copies of your program and the output from test run.

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


#include <stdio.h>
#include <stdlib.h>
double average(double *array,int size){ // function to calculate average
double sum=0;
int i=0;
for(i=0;i<size;i++)
     sum+=array[i];
return sum/size;
}
void sort(double *array,int size) // i am using bubble sort , you can use of your choice
{
     int i,j;
     double temp;
     for(i=0;i<size-1;i++){
          for(j=0;j<size-i-1;j++){
               if(array[j]>array[j+1]){
                    temp=array[j+1];
                    array[j+1]=array[j];
                    array[j]=temp;
               }
    }
     }
}
int main()
{
     FILE *fptr; // file pointer
     char file[100];
     double num[1000],number; // num is an array to store numbers from file
      int cnt=0,i,j; // cnt denotes number of elements read from file
      for(j=1;j<5;j++){
           printf("Enter file name : "); // prompting user to input file name
           scanf("%s",&file);
           fptr=fopen(file,"r"); // opening file in read mode
                if(fptr==NULL) // checking if file is correctly opened
               {
                    printf("File does not exist.");
                    continue;
               }
          while(fscanf(fptr,"%lf",&number)!=EOF) // reading from file and storing in the array until end of file is reached
         {
               num[cnt++]=number;
          }
          sort(num,cnt); // sorting the elements
          printf("File %s \nSorted order ",file);
          for(i=0;i<cnt;i++)
               printf("%.2f ",num[i]); // printing sorted elements
          printf("\nAverage %.2f\n\n",average(num,cnt)); // printing average
          fclose(fptr);
          cnt=0;
     }
     return 0;
}

Enter file name class1.txt File class1.txt Sorted order 46.70 67.10 70.30 73.60 78.30 82.40 84.00 88.80 95.60 98.50 Average 7

Add a comment
Know the answer?
Add Answer to:
Write a program that will take input from a file of numbers of type double and...
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 that takes its input from a file of numbers of type double and...

    Write a program that takes its input from a file of numbers of type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of type double separated by blanks and/or line breaks. If this is being done as a class assignment, obtain the file name from your instructor C++

  • 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...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( );...

    Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

  • 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...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Stuck on this computer science assignment Write a program that demonstrates binary searching through an array...

    Stuck on this computer science assignment Write a program that demonstrates binary searching through an array of strings and finding specific values in an corresponding parallel double array. In all cases your output should exactly match the provided solution.o. Provided files: Assignment.cpp - starter assignment with function prototypes companies.txt - file for program to read. earnings.txt - file for program to read. Input1.txt Input2.txt - Test these inputs out manually, or use stream redirection Input3.txt - These inputs are based...

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