here is the contents of my data file:
6 67.00 F 11.00 C
7 73.00 F 13.00 C
8 82.00 F 17.00 C
9 79.00 F 10.00 C
10 79.00 F 12.00 C
11 79.00 F 9.00 C
12 72.00 F 14.00 C
13 52.00 F 6.00 C
14 45.00 F 0.00 C
15 60.00 F 4.00 C
26 26.11 C 39.00 F
27 22.22 C 52.00 F
28 11.11 C 32.00 F
29 7.22 C 25.00 F
30 15.56 C 43.00 F
1 60.00 F 15.00 C
2 55.00 F 10.00 C
3 71.00 F 14.00 C
4 63.00 F 12.00 C
5 42.00 F -5.00 C
21 19.44 C 45.00 F
22 22.78 C 50.00 F
23 27.78 C 42.00 F
24 26.11 C 49.00 F
25 26.11 C 59.00 F
16 15.56 C 35.00 F
17 12.78 C 25.00 F
18 21.67 C 60.00 F
19 17.22 C 57.00 F
20 5.56 C 30.00 F
first i had to convert all of the Celcius measurements to Farenheight which i completed easily
now i am trying to sort this list of data in ascending order using the numbers 1-30 from the first column of the data file above
here is my code...
Code:
int main( void )
{
int x;
int day[30];
float htemp;
float ltemp;
char hmeasure;
char lmeasure;
FILE *infile;
infile = fopen(INFILE, "r" );
FILE *outfile;
outfile = fopen(OUTFILE, "w" );
while( fscanf(infile, "%i %f %c %f %c", &day[x], &htemp, &hmeasure, <emp, &lmeasure) == 5 )
{
switch(hmeasure)
{
case 'F':
htemp = htemp;
break;
case 'C':
htemp = ((htemp*1.8)+32);
hmeasure = 'F';
break;
}
switch(lmeasure)
{
case 'F':
ltemp = ltemp;
break;
case 'C':
ltemp = ((ltemp*1.8)+32);
lmeasure = 'F';
break;
}
void sort(int day[], int n);
fprintf(outfile, "%i %4.2f %c %4.2f %c\n", day, htemp, hmeasure, ltemp, lmeasure);
}
fclose (infile);
fclose (outfile);
return 0;
}
void sort(int day[], int n)
{
int k, j, m;
double hold;
for (k=0; k<=n-2; k++)
{
m=k;
for (j=k+1; j<=n-1; j++)
{
if (day[j] < day[m])
m = j;
}
hold = day[m];
day[m] = day[k];
day[k] = hold;
}
return;
and here is the result of my code:
7218 67.00 F 51.80 F
7218 73.00 F 55.40 F
7218 82.00 F 62.60 F
7218 79.00 F 50.00 F
7218 79.00 F 53.60 F
7218 79.00 F 48.20 F
7218 72.00 F 57.20 F
7218 52.00 F 42.80 F
7218 45.00 F 32.00 F
7218 60.00 F 39.20 F
7218 79.00 F 39.00 F
7218 72.00 F 52.00 F
7218 52.00 F 32.00 F
7218 45.00 F 25.00 F
7218 60.01 F 43.00 F
7218 60.00 F 59.00 F
7218 55.00 F 50.00 F
7218 71.00 F 57.20 F
7218 63.00 F 53.60 F
7218 42.00 F 23.00 F
7218 66.99 F 45.00 F
7218 73.00 F 50.00 F
7218 82.00 F 42.00 F
7218 79.00 F 49.00 F
7218 79.00 F 59.00 F
7218 60.01 F 35.00 F
7218 55.00 F 25.00 F
7218 71.01 F 60.00 F
7218 63.00 F 57.00 F
7218 42.01 F 30.00 F
as you can see, instead of sorting, it's just changing all the numbers in the first colum to "7218" and is not sorting the list
i dont understand what i am doing wrong, can anyone help?
Thanks in advance,
Isaiah