about summary refs log tree commit diff
path: root/usth/ICT2.1/labwork/1/Ex2.c
diff options
context:
space:
mode:
Diffstat (limited to 'usth/ICT2.1/labwork/1/Ex2.c')
-rw-r--r--usth/ICT2.1/labwork/1/Ex2.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/usth/ICT2.1/labwork/1/Ex2.c b/usth/ICT2.1/labwork/1/Ex2.c
new file mode 100644
index 0000000..f783946
--- /dev/null
+++ b/usth/ICT2.1/labwork/1/Ex2.c
@@ -0,0 +1,28 @@
+/*
+ * Read an array of n int from stdin and print its sum to stdout
+ * This is free and unencumbered software released into the public domain.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int sum(int n, int *a)
+{
+	int s = 0;
+	while (n--)
+		s += a[n];
+	return s;
+}
+
+int main()
+{
+	int n;
+	scanf("%d", &n);
+
+	int *a = malloc(n * sizeof(int));
+	for (int i = 0; i < n; ++i)
+		scanf("%d", a + i);
+
+	printf("%d\n", sum(n, a));
+	return 0;
+}