(Write in C) Write a program to read a file of numbers into an array, sort them
ID: 3763551 • Letter: #
Question
(Write in C)
Write a program to read a file of numbers into an array, sort them and then print them out along with a few statistics. 1. The program should print "Assignment 7 " with a blank line before and after.
2. Then it should prompt the user for the input file name of the file containing the data to sort.
3. If the program can't open the file or if no file name is entered (^d is entered on Linux or ^z-Enter on Windows for EOF), it should print an error message and exit.
4. The file will contain numbers that could be positive or negative integers or floating point numbers five numbers to a line.
5. The program should only handle the first 100 valid numbers in the file.
6. The program should read the numbers from the file into a single dimensional array and then sort them.
7. If the program encounters a non-numeric value in the file, it flush that input line in the file, print an error message and go to the next line. It should not use any numbers from that line.
8. The program should print the sorted list of numbers, 6 numbers to a line in columns 12 characters wide with 1 digits to the right of the decimal point.
9. After printing the sorted list, it should print:
o There were lines found in file .
o There were valid numbers found in file .
o The smallest number found was:
o The average of the values is:
o The largest number found was
: o Zero occurred times
The average should be calculated as a double.
10. If there are no valid lines in the file, the program should only print the following line
o There were 0 lines found in file .
o There were 0 valid numbers found in file .
13. Your program should include at least four functions called from main (in addition to main) that you will define.
14. Your program should not use global variables except an optional global debug flag.
Hi, so I have to do this assignment, but I am completely lost, I have no idea where to start. If you guys could show me some examples or give me some tips or help me out, I would really appriciate it!!
Explanation / Answer
#include <stdio.h>
#include <math.h>
#include<conio.h>
int main(int argc, char*argv[])
{
double average, number = 0, min = 0, max = 0, sum = 0, N, std_dev, sum_sqs = 0.0;
if (argc <= 1)
{
fprintf(stderr, "Usage: %s file ", argv[0]);
return 1;
}
FILE *pFile = fopen(argv[1], "r");
if (pFile == 0)
{
fprintf(stderr, "%s: error failed to open file %s ", argv[0], argv[1]);
return 1;
}
if (fscanf(pFile, "%lf", &N) == 1)
{
for (int j = 0; j < N; j++)
{
if (fscanf(pFile, "%lF", &number) != 1)
{
fprintf(stderr, "%s: error failed to read number ", argv[0]);
return 1;
}
if (number < min || j == 0)
min = number;
if (number > max || j == 0)
max = number;
sum += number;
sum_sqs += (number*number);
}
}
fclose(pFile);
average = sum/N;
std_dev = sqrt((sum_sqs/N)-(average*average));
printf("Smallest: %7.2lf ", min);
printf("Largest: %7.2lf ", max);
printf("Average: %7.2lf ", average);
printf("Standard deviation: %7.3lf ", std_dev);
return(0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.