-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertTemp.c
82 lines (70 loc) · 1.77 KB
/
convertTemp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
/* Finding the equation of a line when you know two points
* Far = ( 9 / 5)Cel + 32
* Cel = (Far - 32) * 5 / 9
*/
int main(void)
{
// open file
FILE* inFile = fopen("convertTemp.txt", "r");
// read and store first digit
int elements;
fscanf(inFile, "%d", &elements);
// create an int array of size elements
int* origTemps;
origTemps = malloc(elements * sizeof(int));
// read elements and story in array
int ii;
for (ii = 0; ii < elements; ii++)
{
fscanf(inFile, "%d", &origTemps[ii]);
}
// create an int array to hold our answers
int* answers;
answers = malloc(elements * sizeof(int));
// convert from farenheit to celcius and store in answers array
int jj;
for (jj = 0; jj < elements; jj++)
{
float converted = ( (float) origTemps[jj] - 32) * 5 / 9;
int int_convert = (int) converted;
if ( converted > 0 )
{
if ( (fabsf(converted) - abs(int_convert)) >= 0.5)
{
answers[jj] = abs(int_convert) + 1;
}
else
{
answers[jj] = abs(int_convert);
}
}
else
{
if ( (fabsf(converted) - abs(int_convert)) >= 0.5)
{
answers[jj] = (abs(int_convert) + 1) * -1;
}
else
{
answers[jj] = (abs(int_convert)) * -1;
}
}
}
// print out answer
int kk;
for (kk = 0; kk < elements; kk++)
{
if (kk == (elements - 1))
{
printf("%d\n", answers[kk]);
}
else
{
printf("%d ", answers[kk]);
}
}
return 0;
}