-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_of_two.c
68 lines (54 loc) · 1.41 KB
/
min_of_two.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
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// open file in read mode
FILE* inFile = fopen("min_of_two.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 * 2 * 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 * 2); i = i + 2)
{
fscanf(inFile, "%d %d", &pairsArray[i], &pairsArray[i + 1]);
}
// sum each pair
int j = 0;
int k = 0;
for (j = 0, k = 0; j < (num_pairs * 2); j = j + 2, k++)
{
int min = pairsArray[j];
if (pairsArray[j] > pairsArray[j + 1])
{
min = pairsArray[j + 1];
}
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;
}