Thanks I got it all working now. Turns out the main problem was how I was saving the file.
But now I have run into another problem. When I read all of the values into my array I am somehow reading an extra zero. I think it is at the beginning when I store the numbers but im not sure. It adds a zero to my array whenever the file does not even contain a zero.
Any help would again be great!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FLUSH while (fgetc(fp) != '\n')
#define MAX_SIZE_NAME 100
#define MAX_SIZE_FILE 100
int main(void)
{
// local declarations in main
float data[MAX_SIZE_FILE];
char fileName[MAX_SIZE_NAME];
int fileLength = 0;
float h;
FILE* fp;
int result;
//for sorting
int current;
int walker;
float temp;
//for printing table
int numPrinted = 0;
int j;
//for average
int k;
float sum = 0;
float average;
// first print my name
printf("\nNAME: Nathan Vazquez\n\n");
printf("Enter File Name: ");
fgets(fileName, sizeof(fileName), stdin);
strtok(fileName, "\n");
//open file
fp = fopen(fileName, "r");
if (fp == NULL)
{
printf("Error: Could not open file %s for read.", fileName);
return EXIT_FAILURE;
} //if
while ((result = fscanf(fp, "%f", &h)) != EOF && (fileLength < 100))
{
if (result != 1)
{
FLUSH;
} //if
data[fileLength++] = h;
} // while
//done opening and storing file
//sort data in file
for(current = 0; current < fileLength; current++)
{
//inner loop: Bubble up one element each pass
for (walker = fileLength; walker > current; walker--)
if (data[walker] < data[walker - 1])
{
temp = data[walker];
data[walker] = data[walker - 1];
data[walker - 1] = temp;
} //closes if
} //closes outer for loop
//print out data
printf("\n\n");
for (j = 0; j < fileLength; j++)
{
numPrinted++;
printf(" %8.2f ", data[j]);
if (numPrinted >= 5)
{
printf("\n");
numPrinted = 0;
} //closes if
} // closes for
printf("\n\n"); // provides the one line gap between data table and results
//prints out the number of elements in the file
printf("There were %d numbers found in file %s\n", fileLength, fileName);
// Smallest number found
printf("The smallest number found was: \t%8.2f\n", data[0]);
//Largest number found
printf("The largest number found was: \t%8.2f\n", data[fileLength]);
for(k = 0; k < fileLength; k++)
{
sum = sum + data[k];
} //for
average = sum / fileLength;
printf("The average of the values is: \t%8.2f\n", average);
fclose(fp);
return EXIT_SUCCESS;
}