aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Liodden <[email protected]>2017-09-21 11:45:39 +0200
committerErik Liodden <[email protected]>2017-09-21 11:45:39 +0200
commitd654d0885efdedc8c16b8cecd6008663466d7d9b (patch)
treef074a246d69751c133dd75d40c3b9be1d1efeb72
parent760593ae352b964ec7a0b51367548123cb24e155 (diff)
downloadalgdat-d654d0885efdedc8c16b8cecd6008663466d7d9b.tar.gz
add oving1 module
add oving1
-rw-r--r--src/oving1/module.mk1
-rw-r--r--src/oving1/oving1.c48
2 files changed, 49 insertions, 0 deletions
diff --git a/src/oving1/module.mk b/src/oving1/module.mk
new file mode 100644
index 0000000..c6a795d
--- /dev/null
+++ b/src/oving1/module.mk
@@ -0,0 +1 @@
+SRC += oving1/oving1.c
diff --git a/src/oving1/oving1.c b/src/oving1/oving1.c
new file mode 100644
index 0000000..7aa3ae9
--- /dev/null
+++ b/src/oving1/oving1.c
@@ -0,0 +1,48 @@
+/*
+ * oving1.c
+ * implements TDT4120 - AlgDat oving 1
+ *
+ * Erik Liodden
+ */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "list.h"
+#include "io.h"
+
+int oving1(void)
+{
+ int num, max;
+ char buf[BUF_SIZE];
+ struct llist *list = llist_init();
+
+ /*read from stdin to buffer */
+ while (fgets(buf, BUF_SIZE, stdin)) {
+ if (!(isnumber(*buf) || *buf == '-'))
+ continue;
+ num = atoi(buf);
+ llist_insert_int_beginning(list, num);
+ }
+
+
+ /* traverse the list */
+ struct llist_node *x = list->head;
+ if (x == NULL) {
+ fprintf(stderr, "empty list\n");
+ exit(EXIT_SUCCESS);
+ }
+ max = *(int *)x->data;
+ while(x != NULL) {
+ if (*(int *)x->data > max)
+ max = *(int *)x->data;
+ x = x->next;
+ }
+
+ printf("maximum:\t%d\n", max);
+
+ /* free memory allocated by the list */
+ llist_free_data_list(list);
+
+ return max;
+}