-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_of_three.c
79 lines (65 loc) · 1.72 KB
/
min_of_three.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
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// open file in read mode
FILE* inFile = fopen("min_of_three.txt", "r");
// check to make sure file was successfully opened
if (inFile == NULL)
{
printf("Unable to open file.\n");
return 1;
}
// store the first number, which will be used in the rest of the program
int num_pairs = 0;
fscanf(inFile, "%d", &num_pairs);
// create two arrays, one to hold number pairs and the second to hold
// answers
int* pairsArray;
pairsArray = malloc (num_pairs * 3 * sizeof(int));
int* answersArray;
answersArray = malloc (num_pairs * sizeof(int));
// read in pairs from the file
int i = 0;
for (i = 0; i < (num_pairs * 3); i = i + 3)
{
fscanf(inFile, "%d %d %d", &pairsArray[i], &pairsArray[i + 1], &pairsArray[i + 2]);
}
// find minimum among pairs
int j = 0;
int k = 0;
for (j = 0, k = 0; j < (num_pairs * 3); j = j + 3, k++)
{
int min = pairsArray[j];
if (pairsArray[j] > pairsArray[j + 1])
{
if (pairsArray[j + 1] > pairsArray[j + 2])
{
min = pairsArray[j + 2];
}
else
{
min = pairsArray[j + 1];
}
}
else if (pairsArray[j] > pairsArray[j + 2])
{
min = pairsArray[j + 2];
}
answersArray[k] = min;
}
// print out our answers
int ll = 0;
for (ll = 0; ll < num_pairs; ll++)
{
if (ll == (num_pairs - 1))
{
printf("%d\n", answersArray[ll]);
}
else
{
printf("%d ", answersArray[ll]);
}
}
return 0;
}