Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Stuck on a practice problem in C Write a program to read a file of numbers into

ID: 3764213 • Letter: S

Question

Stuck on a practice problem 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 "COP 2220: " 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 .

11. Include comments in your code to explain what the code does. Make sure to have comments on the program and for each function.

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.

Explanation / Answer

#include <stdio.h> #include <stdlib.h>
// Used to discard the rest of a line in the file
// Read and discard characters until a new line is reached.
#define FLUSH while (fgetc(fpInput) != ' ')
#define FILENAME_MAX 4096

// Function Declarations
float filePrompt(float numbers[]);
void selectionSort(float list[], float last);

// void printData( int main(void)
{
//Local Declarations FILE* fpfileName;
char nameRead;
char testFile[FILENAME_MAX];
// Statements
printf(" COP 2220-50449 ");
printf("Enter File Name: ");
if(!(fpfileName = fopen("testFile[]", "r")))
printf("Error opening file "),
exit(100);
while (fscanf(fpfileName, "%c", &nameRead) != EOF)
{
char testFile[nameRead];
nameRead++;
}
float filePrompt(float numbers[]);
return 0;
}
{
// Local Declarations
FILE* fpInput;
int sum = 0;
int avg = 0;
int count = 0;
int line_num = 0;
int input_num = 0;
int rc;
float dataIN;
float numbers[];
FILE* numRead;
{
printf(" COP 2220-50449 ");
printf("Enter File Name: ");
if(!(fpInput = fopen(fpfileName, "r")))
printf("Error opening file "),
exit(100);
while (count < 96 && fscanf(fpInput, "%f", &dataIN) != EOF)
{
line_num++;
if( rc != 1 )
{
printf("Non-numeric value on line %d, discarding the rest of the line. ", line_num);
FLUSH;
continue;
}
numbers[count] = dataIN;
sum += count;
count++;
avg = (sum / count);
else
return numbers[];
}
void selectionSort (float list[], float last)
{
float smallest;
float tempData;
for (float current = 0; current < last; current++)
{
smallest = current;
for (int walk = current + 1; walk <= last; walk++)
if (list[walk] < list[smallest])
smallest = walk;
tempData = list[current]
list[current] = list[smallest]
list[smallest] = tempData;
}
return; }