about summary refs log tree commit diff
path: root/usth/ICT2.1/labwork/1
diff options
context:
space:
mode:
Diffstat (limited to 'usth/ICT2.1/labwork/1')
-rw-r--r--usth/ICT2.1/labwork/1/Bonus.c25
-rw-r--r--usth/ICT2.1/labwork/1/Ex1.cc28
-rw-r--r--usth/ICT2.1/labwork/1/Ex2.c28
-rw-r--r--usth/ICT2.1/labwork/1/Ex3.c33
-rw-r--r--usth/ICT2.1/labwork/1/Ex4.c46
-rw-r--r--usth/ICT2.1/labwork/1/README.md38
-rw-r--r--usth/ICT2.1/labwork/1/TT1.pdfbin0 -> 379881 bytes
7 files changed, 198 insertions, 0 deletions
diff --git a/usth/ICT2.1/labwork/1/Bonus.c b/usth/ICT2.1/labwork/1/Bonus.c
new file mode 100644
index 0000000..e98bee5
--- /dev/null
+++ b/usth/ICT2.1/labwork/1/Bonus.c
@@ -0,0 +1,25 @@
+/*
+ * Read an integer from stdin and print its prime factors to stdout
+ * This is free and unencumbered software released into the public domain.
+ */
+
+#include <stdio.h>
+
+int main()
+{
+	unsigned n, m = 0;
+
+	scanf("%u", &n);
+
+	for (unsigned i = 2; i * i <= n; ++i)
+		while (n % i == 0) {
+			printf(m++ ? " %u" : "%u", i);
+			n /= i;
+		}
+	if (n != 1)
+		printf(m++ ? " %u" : "%u", n);
+	if (m)
+		putchar(10);
+
+	return 0;
+}
diff --git a/usth/ICT2.1/labwork/1/Ex1.cc b/usth/ICT2.1/labwork/1/Ex1.cc
new file mode 100644
index 0000000..da20f22
--- /dev/null
+++ b/usth/ICT2.1/labwork/1/Ex1.cc
@@ -0,0 +1,28 @@
+// Read two integers from stdin and print their sum and product
+// This is free and unencumbered software released into the public domain.
+
+#include <iostream>
+
+using namespace std;
+
+int
+add (int& a, int& b)
+{
+  return a + b;
+}
+
+int
+mul (int& a, int& b)
+{
+  return a * b;
+}
+
+int
+main ()
+{
+  int a;
+  int b;
+
+  cin >> a >> b;
+  cout << add (a, b) << endl << mul (a, b) << endl;
+}
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;
+}
diff --git a/usth/ICT2.1/labwork/1/Ex3.c b/usth/ICT2.1/labwork/1/Ex3.c
new file mode 100644
index 0000000..f20bb54
--- /dev/null
+++ b/usth/ICT2.1/labwork/1/Ex3.c
@@ -0,0 +1,33 @@
+/*
+ * Read coordinates of 2 2D-points from stdin and print their distance to stdout
+ * This is free and unencumbered software released into the public domain.
+ */
+
+#include <math.h>
+#include <stdio.h>
+
+/*
+ * The overhead caused by re-evaluating x in this case is much
+ * to change this into a function call.
+ */
+#define SQR(x) ((x) * (x))
+
+/* Conventionally C names are in lowercase */
+struct point {
+	double x, y;
+};
+
+double distance(struct point u, struct point v)
+{
+	return sqrt(SQR(u.x - v.x) + SQR(u.y - v.y));
+}
+
+int main()
+{
+	struct point m, n;
+
+	scanf("%lf %lf %lf %lf", &m.x, &m.y, &n.x, &n.y);
+	printf("%g\n", distance(m, n));
+
+	return 0;
+}
diff --git a/usth/ICT2.1/labwork/1/Ex4.c b/usth/ICT2.1/labwork/1/Ex4.c
new file mode 100644
index 0000000..000f73a
--- /dev/null
+++ b/usth/ICT2.1/labwork/1/Ex4.c
@@ -0,0 +1,46 @@
+/*
+ * Print the area of the rectangle constructed from a pair of 2D points
+ * and check if a point it within this rectangle
+ * This is free and unencumbered software released into the public domain.
+ */
+
+#include <math.h>
+#include <stdio.h>
+
+/* Writing a header for just a simple struct would be overkill. */
+struct point {
+	double x, y;
+};
+
+struct rect {
+	struct point u, v;
+};
+
+double area(struct rect r)
+{
+	return fabs((r.u.x - r.v.x) * (r.u.y - r.v.y));
+}
+
+int isin(struct point p, struct rect r)
+{
+	return ((p.x - r.u.x) * (p.x - r.v.x) < 0
+	        && (p.y - r.u.y) * (p.y - r.v.y) < 0);
+}
+
+int main()
+{
+	struct point m, n;
+	/* Don't bother logging errors, since it would pollute the pipe. */
+	do {
+		scanf("%lf %lf %lf %lf", &m.x, &m.y, &n.x, &n.y);
+	} while (m.x == n.x || m.y == n.y);
+
+	struct rect r = {m, n};
+	printf("%g\n", area(r));
+
+	struct point p;
+	scanf("%lf %lf", &p.x, &p.y);
+	printf("%d\n", isin(p, r));
+
+	return 0;
+}
diff --git a/usth/ICT2.1/labwork/1/README.md b/usth/ICT2.1/labwork/1/README.md
new file mode 100644
index 0000000..2d8236b
--- /dev/null
+++ b/usth/ICT2.1/labwork/1/README.md
@@ -0,0 +1,38 @@
+# Algorithms and Data Structures: Tutorial 1
+
+This package contains the following files:
+
+* Ex1.cc
+* Ex2.c
+* Ex3.c
+* Ex4.c
+* Bonus.c
+
+These programs operate on standard I/O directly.  Except for the solution
+of the first exercise which is written in C++, all solutions are in C.
+The source files are as licensed under the Unlicense which states
+
+> This is free and unencumbered software released into the public domain.
+>
+> Anyone is free to copy, modify, publish, use, compile, sell, or
+> distribute this software, either in source code form or as a compiled
+> binary, for any purpose, commercial or non-commercial, and by any
+> means.
+>
+> In jurisdictions that recognize copyright laws, the author or authors
+> of this software dedicate any and all copyright interest in the
+> software to the public domain. We make this dedication for the benefit
+> of the public at large and to the detriment of our heirs and
+> successors. We intend this dedication to be an overt act of
+> relinquishment in perpetuity of all present and future rights to this
+> software under copyright law.
+>
+> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+> IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+> OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+> ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+> OTHER DEALINGS IN THE SOFTWARE.
+>
+> For more information, please refer to <http://unlicense.org/>
diff --git a/usth/ICT2.1/labwork/1/TT1.pdf b/usth/ICT2.1/labwork/1/TT1.pdf
new file mode 100644
index 0000000..3140b19
--- /dev/null
+++ b/usth/ICT2.1/labwork/1/TT1.pdf
Binary files differ