summaryrefslogtreecommitdiff
path: root/findMaxOfFloats.c
diff options
context:
space:
mode:
Diffstat (limited to 'findMaxOfFloats.c')
-rw-r--r--findMaxOfFloats.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/findMaxOfFloats.c b/findMaxOfFloats.c
new file mode 100644
index 0000000..afa6d77
--- /dev/null
+++ b/findMaxOfFloats.c
@@ -0,0 +1,63 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <float.h>
4#include <string.h>
5#include <errno.h>
6#include <unistd.h>
7
8void main(int argc, char* argv[])
9{
10 float *array = NULL;
11 int arrayLength = 0;
12 int arrayUsage = 0;
13
14 float max = FLT_MIN;
15 float current;
16
17 FILE *fp;
18
19 for (int i = 1; i < argc; i++)
20 {
21 fp = fopen(argv[i], "r");
22 if (fp == NULL)
23 {
24 fprintf(stderr, "Could not open `%s´: %s\n", argv[i], strerror(errno));
25 exit(EXIT_FAILURE);
26 }
27
28 for (int res; res = fscanf(fp, "%f\n", &current); )
29 {
30 if (res == EOF || res != 1)
31 break;
32
33 if (arrayUsage >= arrayLength)
34 {
35 if (arrayLength <= 1)
36 arrayLength = 1;
37
38 // fprintf(stderr, "Reallocating array %d → %d\n", arrayLength, arrayLength * 2);
39 arrayLength *= 2;
40 array = realloc(array, sizeof(float) * arrayLength);
41 }
42
43 array[arrayUsage] = current;
44 arrayUsage++;
45 }
46
47 if (ferror(fp))
48 {
49 fprintf(stderr, "Error while reading from file `%s´: %s\n", argv[i], strerror(errno));
50 exit(EXIT_FAILURE);
51 }
52
53 fclose(fp);
54 }
55
56 for (int i = 0; i < arrayUsage; i++)
57 {
58 if (array[i] > max)
59 max = array[i];
60 }
61
62 printf("%.2f\n", max);
63}