start of stringsort, just basic array for now.
This commit is contained in:
@@ -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) {
|
||||||
@@ -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
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user