Просмотр исходного кода

start of stringsort, just basic array for now.

my name 6 месяцев назад
Родитель
Сommit
e0d044e719
2 измененных файлов с 67 добавлено и 3 удалено
  1. 4 3
      med/C-programming-a-modern-standard/digitseen.c
  2. 63 0
      qscr/c/stringsort.c

+ 4 - 3
med/C-programming-a-modern-standard/digitseen.c

@@ -1,4 +1,5 @@
 #include <stdbool.h>
 #include <stdbool.h>
+#include <stddef.h>
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
 
 
@@ -10,7 +11,7 @@ void numberprinter(int inarr[], int arrlength) {
 
 
 struct Darray {
 struct Darray {
   int *digits;
   int *digits;
-  int length;
+  size_t length; // blijkbaar correcte eenheid voor length, malloc stuff ofzo (instead of hardcoding it)
 };
 };
 
 
 struct Darray *numbertodigitDarr(int input) {
 struct Darray *numbertodigitDarr(int input) {
@@ -25,7 +26,7 @@ struct Darray *numbertodigitDarr(int input) {
 
 
   // allocate memory for struct and array, apparently more ideomatic to have a single malloc
   // allocate memory for struct and array, apparently more ideomatic to have a single malloc
   struct Darray *DigDarr =
   struct Darray *DigDarr =
-      malloc(op * sizeof(int) + sizeof(*DigDarr) + sizeof(int));
+    malloc(op * sizeof(int) + sizeof(*DigDarr) + sizeof(int));
   if (DigDarr == NULL) {
   if (DigDarr == NULL) {
     printf("allocation failed");
     printf("allocation failed");
     exit(0);
     exit(0);
@@ -90,4 +91,4 @@ iterator (which has to start from 0, because we don't know the size of the
 number?) then our number will be the wrong way around.
 number?) then our number will be the wrong way around.
 
 
 malloc, blijkbaar very different
 malloc, blijkbaar very different
-*/
+ */

+ 63 - 0
qscr/c/stringsort.c

@@ -0,0 +1,63 @@
+/*
+
+idee, een programma dat een lijst van strings krijgt, en ze dan sorteert op
+alphabetische volgorde, idealiter ook met hele nummbers if relevant (dus niet
+alleen individuele digits)
+
+split into strings and numbers
+
+
+
+*/
+
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+struct chickendynarr {
+  int *arr;
+  size_t length;
+};
+
+struct chickendynarr *createarr(int size) {
+
+  struct chickendynarr *myarr = malloc(sizeof(int) * size);
+  myarr->length = size;
+  myarr->arr = (int *)(myarr + 1);
+
+  for (int i = 0; i < size - 1; i++) {
+    int randnumb = rand() % 20;
+    myarr->arr[i] = randnumb;
+    printf("num%d:%d\n", i, randnumb);
+  }
+
+  return myarr;
+}
+
+/* eerst gewone bubblesort weer doen */
+int mybubblesort(struct chickendynarr *inputarr) {
+  for (int i = 0; i < inputarr->length - 1; i++) {   // voor elk nummer
+    for (int j = 0; j < inputarr->length - 1; j++) { // ga langs elk nummer
+      if (inputarr->arr[j] >
+          inputarr->arr[j + 1]) { // als huidig nummer groter is
+                                  // swap hem dan
+        int temp = inputarr->arr[j];
+        inputarr->arr[j] = inputarr->arr[j + 1];
+        inputarr->arr[j + 1] = temp;
+      }
+    }
+  }
+
+  for (int i = 0; i < inputarr->length - 1; i++) {
+    printf("num:%d, %d\n", i, inputarr->arr[i]);
+  }
+
+  return 0;
+}
+
+int main() {
+  srand(time(NULL));
+  struct chickendynarr *myarr = createarr(5);
+  mybubblesort(myarr);
+  return 0;
+}