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 an error, then return a value of 1 from the function calc(), otherwise return 0 is everything is fine. Open a new text file and save it as data.txt. You must type the 10 numbers below into data.txt. Do NOT save the numbers into an array in the program. The numbers must be processed as the data.txt file is read line-by-line. 0.42 0.94 0.17 0.04 0.68 0.95 0.89 0.18 0.72 0.23 The expected output is Min = 0.040000 Max = 0.950000 Average = 0.522000
Program:
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
int calc(void)
{
FILE *fp;
char line[10];
float min;
float max;
float ave;
float number;
int count=0;
fp = fopen("data.txt", "r"); //opening file
if(fp)
{
if(fgets(line, 10, fp))
{
count++;
number = atof(line); //reading the number
max = number;
min = number;
ave += number;
}
while( fgets(line, 10, fp) )
{
count++;
number = atof(line); //reading the number
if( number > max )
max = number;
else if( number < min )
min = number;
ave += number; //getting sum
}
printf("Min = %f\n", min);
printf("Max = %f\n", max);
printf("Average = %f\n", ave/count);
fclose(fp);
return 0;
}
else
return 1;
}
int main()
{
calc();
return 0;
}
Execution and Output:
Write a program to read the contents of data.txt file, and determine the smallest value (min),...
Topics: list, file input/output (Python) You will write a program that allows the user to read grade data from a text file, view computed statistical values based on the data, and to save the computed statistics to a text file. You will use a list to store the data read in, and for computing the statistics. You must use functions. The data: The user has the option to load a data file. The data consists of integer values representing student...
You're to write a C++ program that analyzes the contents of an external file called data.txt. (You can create this file yourself using nano, that produces a simple ASCII text file.) The program you write will open data.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation and whitespace characters contained in the file so that the caller can report the results to stdout. Additionally, the function will return the total number of characters read to...
PYTHON write a program that will open a file named data.txt and read each line of the file one at a time. Each line should be printed to the screen along with a line number
(statistics.py) Write a program that reads data from the file provided, data.txt, into a list. Once the data are in a list, calculate and print the following: sum = 69.28 mean = 3.46 min = 0.19 max = 8.29 You may use the Python built-ins, min(), max(), and sum(). Format the numbers using '.2f'.
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...
In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; } while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...
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...
c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detected. (An example of such a file is shown below). Also, the program should copy from values[ 1 any value which has even sum of digits and its index (location) to two new arrays evenArr 1 and idxArrl I, respectively. In addition, the program must...
In C please! Thank you! Write a program that finds the smallest (min), largest (max), and average (mean) of N values entered by the user. Your program should begin by prompting the user for N, the number of values to be entered. Then the program should accept that number of values, determining the min, max, and mean. Then it should display those values. This is an extension of a previous lab. First, get it working for N values. Then, extend...
Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...