commit 424d3c5c430b33a7c44d23ebd87d9084efb844a6
parent 72fd76f10ce717d784f27a915ae7a7e9bbab646b
Author: Christos Margiolis <christos@margiolis.net>
Date: Sat, 12 Dec 2020 17:55:09 +0200
added data structures
Diffstat:
11 files changed, 378 insertions(+), 0 deletions(-)
diff --git a/c-data-structures/arrays_ex1.c b/c-data-structures/arrays_ex1.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+/* Make an array of 10 ints and assign random values in it */
+
+int
+main(int argc, char *argv[])
+{
+ int arr[10], i = 0;
+
+ srand(time(NULL));
+ for (; i < 10; i++) {
+ arr[i] = rand() % 101;
+ printf("arr[%d]: %d\n", i, arr[i]);
+ }
+
+ return 0;
+}
diff --git a/c-data-structures/arrays_ex2.c b/c-data-structures/arrays_ex2.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define ARRLEN(x) (sizeof(x) / sizeof(x[0]))
+
+/*
+ * Modify arrays_ex1.c to find max, maxloc, min, minloc,
+ * and the average value
+ */
+
+int
+main(int argc, char *argv[])
+{
+ float sum, avg;
+ int arr[10], i;
+ int max, min, maxloc, minloc;
+
+ srand(time(NULL));
+ sum = 0;
+ for (i = 0; i < ARRLEN(arr); i++) {
+ arr[i] = rand() % 101;
+ sum += arr[i];
+ printf("arr[%d]: %d\n", i, arr[i]);
+ }
+
+ max = min = arr[0];
+ maxloc = minloc = 0;
+ for (i = 0; i < ARRLEN(arr); i++) {
+ if (arr[i] < min) {
+ min = arr[i];
+ minloc = i;
+ }
+ if (arr[i] > max) {
+ max = arr[i];
+ maxloc = i;
+ }
+ }
+
+ avg = sum / (float)ARRLEN(arr);
+ printf("max: %d | maxloc: %d | min: %d | minloc: %d | avg: %.2f\n",
+ max, maxloc, min, minloc, avg);
+
+ return 0;
+}
diff --git a/c-data-structures/arrays_ex3.c b/c-data-structures/arrays_ex3.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ARRLEN(x) (sizeof(x) / sizeof(x[0]))
+
+/* Modify arrays_ex2.c to get the array's elements from stdin */
+
+int
+main(int argc, char *argv[])
+{
+ float sum, avg;
+ int arr[10], i;
+ int max, min, maxloc, minloc;
+
+ sum = 0;
+ for (i = 0; i < ARRLEN(arr); i++) {
+ do {
+ printf("arr[%d]: ", i);
+ scanf("%d", &arr[i]);
+ getchar();
+ } while (arr[i] < 0 || arr[i] > 100);
+
+ sum += arr[i];
+ }
+
+ max = min = arr[0];
+ maxloc = minloc = 0;
+ for (i = 0; i < ARRLEN(arr); i++) {
+ if (arr[i] < min) {
+ min = arr[i];
+ minloc = i;
+ }
+ if (arr[i] > max) {
+ max = arr[i];
+ maxloc = i;
+ }
+ }
+
+ avg = sum / (float)ARRLEN(arr);
+ printf("max: %d | maxloc: %d | min: %d | minloc: %d | avg: %.2f\n",
+ max, maxloc, min, minloc, avg);
+
+ return 0;
+}
diff --git a/c-data-structures/funcs_ex1.c b/c-data-structures/funcs_ex1.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+
+/*
+ * Write two swap functions. One using call by value
+ * and one using call by pointer
+ */
+
+static void
+swap_val(int x, int y)
+{
+ int tmp;
+
+ tmp = x;
+ x = y;
+ y = tmp;
+}
+
+static void
+swap_ptr(int *x, int *y)
+{
+ int tmp;
+
+ tmp = *x;
+ *x = *y;
+ *y = tmp;
+}
+
+int
+main(int argc, char *argv[])
+{
+ int x = 10, y = 20;
+
+ printf("x: %d | y: %d\n", x, y);
+ swap_val(x, y);
+ swap_ptr(&x, &y);
+ printf("x: %d | y: %d\n", x, y);
+
+ return 0;
+}
diff --git a/c-data-structures/funcs_ex2.c b/c-data-structures/funcs_ex2.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+
+/*
+ * Write a function that computes the basic operations.
+ * Take the first 2 arguments by value and the rest
+ * by pointer.
+ */
+
+static void
+calc(float x, float y, float *sum, float *diff, float *prod, float *ratio)
+{
+ *sum = x + y;
+ *diff = x - y;
+ *prod = x * y;
+ if (y != 0)
+ *ratio = x / y;
+}
+
+int
+main(int argc, char *argv[])
+{
+ float x = 20, y = 10;
+ float sum, diff, prod, ratio = 0;
+
+ calc(x, y, &sum, &diff, &prod, &ratio);
+ printf("x: %.2f | y: %.2f | sum: %.2f | diff: %.2f | prod: %.2f | ratio: %.2f\n",
+ x, y, sum, diff, prod, ratio);
+
+ return 0;
+}
diff --git a/c-data-structures/funcs_ex3.c b/c-data-structures/funcs_ex3.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <string.h>
+
+/*
+ * Read 2 strings. If they are the same replace all a's with
+ * b's, otherwise concatenate them using a third string.
+ */
+
+int
+main(int argc, char *argv[])
+{
+ char str1[64], str2[64], str3[64] = {0};
+ int i = 0;
+
+ printf("str1: ");
+ fgets(str1, 64, stdin);
+ printf("str2: ");
+ fgets(str2, 64, stdin);
+
+ if (!strcmp(str1, str2)) {
+ for (; i < strlen(str1); i++)
+ if (str1[i] == 'a')
+ str1[i] = 'b';
+ } else {
+ strcpy(str3, str1);
+ strcat(str3, str2);
+ }
+
+ printf("str1: %sstr2: %sstr3: %s\n", str1, str2, str3);
+
+ return 0;
+}
diff --git a/c-data-structures/memalloc_ex1.c b/c-data-structures/memalloc_ex1.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <string.h>
+
+/*
+ * Make a student structure, assign a pointer to it and modify
+ * it using the pointer
+ */
+
+struct Student {
+ char name[32];
+ float avg;
+};
+
+int
+main(int argc, char *argv[])
+{
+ struct Student stud = {"Foo Bar", 7.4f};
+ struct Student *ptr;
+
+ ptr = &stud;
+
+ printf("stud.name: %s | stud.avg: %.2f\n", stud.name, stud.avg);
+ strcpy(ptr->name, "Bar Baz");
+ printf("ptr->name: %s | ptr->avg: %.2f\n", ptr->name, ptr->avg);
+
+ return 0;
+}
diff --git a/c-data-structures/memalloc_ex2.c b/c-data-structures/memalloc_ex2.c
@@ -0,0 +1,51 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * Make N student strucutre, allocate memory for them and
+ * assign values to them from stdin.
+ */
+
+struct Student {
+ char name[32];
+ int id;
+ float grade;
+};
+
+int
+main(int argc, char *argv[])
+{
+ struct Student *stud;
+ int n, i;
+ float sum;
+
+ do {
+ printf("How many students? ");
+ scanf("%d", &n);
+ getchar();
+ } while (n <= 0);
+
+ if ((stud = malloc(n * sizeof(struct Student))) == NULL)
+ return 1;
+
+ sum = 0.0f;
+ for (i = 0; i < n; i++) {
+ printf("stud[%d].name: ", i);
+ fgets(stud[i].name, 32, stdin);
+
+ printf("stud[%d].id: ", i);
+ scanf("%d", &stud[i].id);
+ getchar();
+
+ printf("stud[%d].grade: ", i);
+ scanf("%f", &stud[i].grade);
+ getchar();
+
+ sum += stud[i].grade;
+ }
+
+ printf("avg: %.2f\n", sum / (float)n);
+
+ return 0;
+}
diff --git a/c-data-structures/ptrs_ex1.c b/c-data-structures/ptrs_ex1.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+
+/*
+ * Assign a pointer to a variable and change its
+ * value using both the variable and the pointer. Then
+ * print their value and addresses
+ */
+
+int
+main(int argc, char *argv[])
+{
+ int *ptr, x = 10;
+
+ ptr = &x;
+ x += 20;
+ *ptr += 30;
+
+ printf("x: val: %d | addr: %p\n", x, &x);
+ printf("ptr: val: %d | addr: %p\n", *ptr, ptr);
+
+ return 0;
+}
diff --git a/c-data-structures/ptrs_ex2.c b/c-data-structures/ptrs_ex2.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+
+#define ARRLEN(x) (sizeof(x) / sizeof(x[0]))
+
+/*
+ * Make an array and print its contents using both
+ * indexing and pointer arithemtic
+ */
+
+int
+main(int argc, char *argv[])
+{
+ int arr[5] = {1, 2, 3, 4, 5};
+ int i = 0;
+
+ for (; i < ARRLEN(arr); i++) {
+ printf("arr[%d]: %d | addr: %p\n", i, arr[i], &arr[i]);
+ printf("*(arr + %d): %d | addr: %p\n", i, arr[i], &arr[i]);
+ }
+
+ return 0;
+}
diff --git a/c-data-structures/ptrs_ex3.c b/c-data-structures/ptrs_ex3.c
@@ -0,0 +1,47 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ARRLEN(x) (sizeof(x) / sizeof(x[0]))
+
+/*
+ * Modify arrays_ex3.c to use pointer arithemtic instead
+ * of indexing
+ */
+
+int
+main(int argc, char *argv[])
+{
+ float sum, avg;
+ int arr[10], i;
+ int max, min, maxloc, minloc;
+
+ sum = 0;
+ for (i = 0; i < ARRLEN(arr); i++) {
+ do {
+ printf("arr + %d: ", i);
+ scanf("%d", arr + i);
+ getchar();
+ } while (*(arr + i) < 0 || *(arr + i) > 100);
+
+ sum += *(arr + i);
+ }
+
+ max = min = *arr;
+ maxloc = minloc = 0;
+ for (i = 0; i < ARRLEN(arr); i++) {
+ if (arr[i] < min) {
+ min = *(arr + i);
+ minloc = i;
+ }
+ if (arr[i] > max) {
+ max = *(arr + i);
+ maxloc = i;
+ }
+ }
+
+ avg = sum / (float)ARRLEN(arr);
+ printf("max: %d | maxloc: %d | min: %d | minloc: %d | avg: %.2f\n",
+ max, maxloc, min, minloc, avg);
+
+ return 0;
+}