I'm new to programming in c and I'm trying to write a program that reads floating point numbers from a user entered file then sorts them and prints them out. I'm not sure where I've messed up in the code. When I run the program and type in the test file, all that happens is it prints a bunch of zero's and some other random numbers not in the test file. Where have I messed up in the code?
Code:
#include <stdio.h>
#include <stdlib.h>
#define ARY_SIZE 100
#define FFLUSH while(fgetc(fp) != '\n')
int main()
{
printf("\n\nCOP 2220 Project 4: Kurt Gallagher\n\n");
printf("Enter File Name: ");
float dataArray [ARY_SIZE], val;
int i, rc, temp, numPrinted, swap;
char fname[100];
FILE* fp;
rc = scanf("%s", fname);
fp = fopen(fname, "r");
if (fp != NULL)
{
char line[BUFSIZ];
while (fgets(line, sizeof line, fp) != NULL)
{
char *start = line;
float field;
int n;
while (sscanf(start, "%f", &field, &n) == 1)
{
printf("%f", field);
start += n;
}
}
}
for(i = 0; i < ARY_SIZE; i++)
{
for (swap = 0; swap > ARY_SIZE; swap--)
if (dataArray[swap] < dataArray[swap - 1])
{
temp = dataArray[swap];
dataArray[swap] = dataArray[swap - 1];
dataArray[swap - 1] = temp;
}
}
numPrinted = 0;
for(i = 0; i < ARY_SIZE; i++)
{
printf("%f", dataArray[i]);
if (numPrinted < 5)
numPrinted++;
else
{
printf("\n");
numPrinted = 0;
}
}
return 0;
}