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

In C coding language: You have a file containing a list of times and temperature

ID: 3828999 • Letter: I

Question

In C coding language:

You have a file containing a list of times and temperature readings. Unfortunately, the data was assembled from a set of devices and dumped into a file with all of the readings from device 1 followed by all of the readings from device 2 and finally all of the readings from device 3.

Here is an example of what the data file would look like.

8:20 23.5

9:40 24.6

10:30 25.7

11:00 26.0

11:45 25.4

9:00 23.8

9:50 24.8

10:50 25.8

11:50 25.3

10:00 24.8

10:40 25.8

11:20 26.1

12:00 25.2

Write a program that can read a file like this and sort it in order of ascending time. Your program should write the times and associated temperature readings back out to a file with one reading per line, sorted in order of increasing time. Your program should be able to cope with an input file of any length, and not just the example shown here.

Explanation / Answer

#include<stdio.h>
void sort(int *p, int size)
{
int i, j;
for (i = 0; i < size - 1; ++i)
{
for (j = 0; j < size - i - 1; ++j)
{
if (p[j] > p[j + 1])
{
int temp;
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}

void createtestfile()
{
FILE *f1;
f1 = fopen("sortedfile.txt", "w");
fprintf(f1, "6#this is comment ");
fprintf(f1, "3#this is comment ");
fprintf(f1, "7#this is comment ");
fprintf(f1, "2 ");
}

void readtestfile()
{
FILE *fp;
char buff[1024];
int value;

int number_of_lines;
fp = fopen("data.txt", "r");
do
{
fgets(buff, 1024, fp);
fscanf(fp, "%d", &value);
number_of_lines++;
buff[number_of_lines] = value;
} while (fp != EOF);

sort(buff, number_of_lines);

int i;
for (i = 1; i < number_of_lines; i++)
{
printf("value is %d", buff[i]);
}
}
fclose(f1)
int main()
{
createtestfile();
readtestfile();
return 0;
}