Compare commits

..

10 Commits

Author SHA1 Message Date
lyrgb 8bdf3c0fbf changed structure 2026-06-30 07:01:23 +02:00
gunny b761625110 cpp gitignore 2026-06-25 13:13:07 +02:00
lyrgb 450579d266 ic, basic binary to hex using nibbles 2026-06-17 00:35:30 +02:00
lyrgb 6e1a33db8a file input 2026-06-14 04:13:14 +02:00
lyrgb a214e58f2c setup make file to compile .c into /build 2026-06-14 04:12:17 +02:00
lyrgb 840f5f5424 split into modules 2026-06-14 03:06:47 +02:00
lyrgb bd700e78b2 IC of parser, set up basic struct 2026-06-08 02:25:13 +02:00
lyrgb 20777e094c 3,4 handling strings in/using a struct, redid naming 2026-06-06 02:55:53 +02:00
lyrgb f48b0288d2 setting up clang formatting 2026-06-06 02:55:28 +02:00
lyrgb 152714b4ea enable inputs 2026-06-05 08:37:48 +02:00
39 changed files with 512 additions and 541 deletions
+71
View File
@@ -119,6 +119,8 @@ lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html) # Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
build
# Runtime data # Runtime data
pids pids
*.pid *.pid
@@ -246,6 +248,75 @@ dist
# SvelteKit build / generate output # SvelteKit build / generate output
.svelte-kit .svelte-kit
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Linker files
*.ilk
# Debugger Files
*.pdb
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
*.so.*
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# Build directories
build/
Build/
build-*/
# CMake generated files
CMakeFiles/
CMakeCache.txt
cmake_install.cmake
Makefile
install_manifest.txt
compile_commands.json
# Temporary files
*.tmp
*.log
*.bak
*.swp
# vcpkg
vcpkg_installed/
# debug information files
*.dwo
# test output & cache
Testing/
.cache/
#!! ERROR: vscode is undefined. Use list command to see defined gitignore types !!# #!! ERROR: vscode is undefined. Use list command to see defined gitignore types !!#
# End of https://www.toptal.com/developers/gitignore/api/node,vscode,emacs,c # End of https://www.toptal.com/developers/gitignore/api/node,vscode,emacs,c
+44
View File
@@ -0,0 +1,44 @@
/*
een programma dat newlines leest en ze dan weer print
we gaan een scanf moeten maken die newlines leest en dan malloced voor elke
string
scanf lijkt automatisch te stoppen op newlines, maar gaat wel onmiddelijk door totdat alle scanf's satisfied zijn
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORDCOUNT 5
void storeinput(char input[], int index, char **strarr) {
char *myword = malloc(strlen(input) +1);
//*myword = *input; //blijkbaar hier strcpy voor nodig, want anders overwrite
//ik hier alleen maar de value van de pointer zelf
strcpy(myword, input); //myword is a pointer to allocated memory, but it's empty unless strcopy
printf("myword:%s\n", myword);
strarr[index] = myword;
printf("strarr:%s\n", strarr[index]);
}
int main() {
// laten we eerst een array van pointers naar strings maken
char **strarr = malloc(sizeof(char *) * 10);
// moeten nu een functie maken die individuele strings malloced?
for (int i = 0; i < WORDCOUNT; i++) {
char temp[50];
scanf("%s", temp);
storeinput(temp, i, strarr);
printf("wordin:%d %s\n", i, temp);
}
for (int i = 0; i < WORDCOUNT; i++) {
printf("word:%d, %s\n", i, strarr[i]);
}
return 0;
}
+47
View File
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <string.h>
/*
take input
decide if hex or binary
then convert
then print
let's convert binary to hex first
for each byte, split into nibble,
*/
//might have to add some padding, but basic works
void bintohex(char* string_in){
const char hexmap[] = "0123456789ABCDEF";
char hexnum[4] = {};
int stringl = strnlen(string_in, 10)-1;
//aantal nibbles
int loopamount = stringl/4;
printf("totalstringlength=%d\n", stringl);
for (int loopcount = 0; loopcount < loopamount; loopcount++) {
int bin2dec = 0;
//rechts naar links in nibble
for (int nibbledigit = 0 ; nibbledigit <4 ; nibbledigit++) {
//nibblecount + current
int targetdigit = loopcount*4+(4-1-nibbledigit);
//check if j+i = 1
//printf("nibbledigit:%d=%c=%i\n", j, string_in[targetdigit],1<<j);
bin2dec += 1 * (string_in[targetdigit]-'0')*1<<(nibbledigit);
}
hexnum[loopcount]=hexmap[bin2dec];
printf("nibble:%i=%d\n",loopcount,bin2dec);
}
printf("hex=%s", hexnum);
}
int main(){
printf("input either binary or hexadecimal\n");
char buff[32+2]; //4bytes+\n+null
fgets(buff, sizeof(buff), stdin);
bintohex(buff);
}
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
#ifndef HASH_FNV_H
#define HASH_FNV_H
#include <stddef.h>
#include <stdint.h>
uint32_t hasher(void *data, size_t len);
+27
View File
@@ -0,0 +1,27 @@
#include "stringchicken.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]) {
char myString[100];
char ch;
FILE *fptr = fopen(argv[1], "r");
if (fptr == NULL) {
printf("file doesn't exist");
exit(1);
}
struct stringchicken *mystringarray = create_string_chicken();
while (fgets(myString, 100, fptr)) {
push_SC(myString, mystringarray);
}
print_SC(mystringarray);
int mylength = strnlen(myString, 100);
fclose(fptr);
}
+2
View File
@@ -0,0 +1,2 @@
dit is een test
test2
+60
View File
@@ -0,0 +1,60 @@
#include "stringchicken.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
parser that can read K=V, maybe also K:V? although would just be configurable seperator
I know python uses dictionary which uses a hash table so key -> hash -> value table
for this assignment, I could use 2 arrays? though that's... a bit dangerous? or at least tricky, in terms of reassignments.
let's just go with a struct with 2 dynamic arrays where the indices are aligned
ik neem dus eerst 1 die het omzet naar een struct van strings, dan een struct van de KV values.
*/
struct stringchicken *create_string_chicken(){
struct stringchicken *mystruct = malloc(sizeof(struct stringchicken));
mystruct->length = 0;
mystruct->capacity = 2;
mystruct->data = malloc(sizeof(char*) * mystruct->capacity);
return mystruct;
}
void push_SC(char *string_input, struct stringchicken *struct_input){
if (struct_input->length == struct_input->capacity) {
struct_input->capacity*=2;
char **tempptr = realloc(struct_input->data, sizeof(char*) * struct_input->capacity);
if (tempptr == NULL) {
exit(1);
}
struct_input->data = tempptr;
}
char *newstring = malloc(strnlen(string_input, 101)+1);
strcpy(newstring,string_input);
struct_input->data[struct_input->length] = newstring;
struct_input->length++;
}
void free_SC(struct stringchicken *struct_input){
for (int i = 0; i < struct_input->length; i++) {
free(struct_input->data[i]);
}
free(struct_input->data);
free(struct_input);
}
void print_SC(struct stringchicken *input_struct){
for (size_t i = 0; i < input_struct->length; i++) {
printf("line:%zu equals %s", i, input_struct->data[i]);
}
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef STRINGCHICKEN_H
#define STRINGCHICKEN_H
#include <stddef.h>
struct stringchicken{
char **data;
size_t length;
size_t capacity;
};
struct stringchicken *create_string_chicken(void);
void push_SC(char *string_input, struct stringchicken *struct_input);
void print_SC(struct stringchicken *input_struct);
#endif // STRINGCHICKEN_H
+100
View File
@@ -0,0 +1,100 @@
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*
todo
change intarray into str array
make push array malloc for new str
make input strings instead of int (should use fgets?)
*/
struct StringArray{
char **data;
size_t length;
size_t capacity;
};
void print_SA(struct StringArray *in_str) {
for (size_t i = 0; i < in_str->length; i++) {
printf("word:%zu = %s\n", i, in_str->data[i]);
}
}
struct StringArray *SA_create(size_t in_capacity) {
struct StringArray *mystr = malloc(sizeof(struct StringArray)); //allocate memory for struct
mystr->data = malloc(sizeof(char*) * in_capacity); //make sure the pointer in the struct points to the array
mystr->length = 0; //forgot to actually assign a value to the struct
mystr->capacity = in_capacity;
if (mystr->data == NULL) {
exit(1);
}
return mystr;
}
int get_string_len(char *in_string){
int length = 0;
char currchar;
do {
currchar = in_string[length];
length++;
}
while (currchar != '\0');
printf("length of word: %d\n", length); //for some reason length is always 2 more than expected, so 1 more than necessary? apparently because of \n, not sure how to filter that.
return length;
}
/* push value to struct, also handle realloc */
void SA_push(char *in_string, struct StringArray *in_str){
if (in_str->length == in_str->capacity) { //check if already full
in_str->capacity*=2; //if so we should double
printf("array capacity is now:%zu\n", in_str->capacity);
char **temp; //create a temporary pointer
temp = realloc(in_str->data, sizeof(char*)*in_str->capacity); //realloc the data into a new bucket
if(temp == NULL){exit(0);}; //check if actually valid
in_str->data = temp; //if valid, make sure the pointer in the struct points to the realloced block
}
char *newword = malloc(sizeof(char) * get_string_len(in_string)); //allocate memory for new string, apparantly my function already includes the length of null term
strcpy(newword, in_string); //copy word into memory
in_str->data[in_str->length] = newword;
printf("word:%zu = %s\n", in_str->length, in_str->data[in_str->length]);
in_str->length++; //didn't read the flow when transferring and accidentally put this in the if block
printf("array length is now:%zu\n", in_str->length);
//also had an off-by-one error because I increased length before doing anything
}
void SA_free(struct StringArray *inp_str) {
for (size_t i = 0; i < inp_str->length; i++) { //for each pointer in array
free(inp_str->data[i]); //free at pointer
}
free(inp_str->data); //free pointer to pointers
free(inp_str); //free the struct
}
int main() {
srand(time(NULL));
printf("input words:\n");
struct StringArray *mystringarraystr = SA_create(2);
char input[50];
do{
fgets(input, sizeof(input), stdin); //get input from stdin of size input[] and put it in input
SA_push(input, mystringarraystr); //push the value of input into mystr
} while (*input != '-');
print_SA(mystringarraystr);
SA_free(mystringarraystr);
return 0;
}
BIN
View File
Binary file not shown.
+38
View File
@@ -0,0 +1,38 @@
#include <ncurses.h>
#include <stdio.h>
int main(){
initscr();
noecho();
cbreak();
keypad(stdscr, TRUE);
curs_set(1);
if(has_colors() == FALSE ) {
endwin();
puts("no colors");
return 1;
}
start_color();
init_pair(1, COLOR_WHITE, COLOR_BLUE);
attron(COLOR_PAIR(1));
int y,x;
getmaxyx(stdscr, y,x);
y=y*0.5;
x=(x*0.5) -6;
mvwprintw(stdscr, y, x, "Hello World");
mvwprintw(stdscr, 2, 2, "Hellow World1");
refresh();
attroff(COLOR_PAIR(1));
getch();
endwin();
return 0;
}
+6
View File
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.24)
project(cppout)
file(GLOB SOURCE_FILES "*.cpp")
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
+4
View File
@@ -0,0 +1,4 @@
#include <cstdio>
void printhello(const char *name) {
printf("hello %s \n", name);
}
+1
View File
@@ -0,0 +1 @@
void printhello(const char *name);
+7
View File
@@ -0,0 +1,7 @@
#include <cstdlib>
#include "hello.h"
int main() {
printhello("test");
return EXIT_SUCCESS;
}
BIN
View File
Binary file not shown.
+6
View File
@@ -0,0 +1,6 @@
#include <iostream>
int main() {
std::cout << "Hello, world";
return 0;
}
+6
View File
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.24)
project(cppout)
file(GLOB SOURCE_FILES "*.cpp")
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
+47
View File
@@ -0,0 +1,47 @@
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <vector>
void bsort(std::vector<int> &input_vec){
/*
voor elk nummer i in k behalve de laatste
ga langs elk volgende nummer j behalve de laatste
en kijk of i+1 groter is dan i, als i groter is dan j, verplaats i naar
j
*/
for (size_t i = 0; i < input_vec.size(); i++) {
printf("%zu\n", i);
for (size_t j = 0; j < input_vec.size()-1-i; j++) {
if (input_vec[j]>input_vec[j+1]) {
int temp = input_vec[j + 1];
input_vec[j + 1] = input_vec[j];
input_vec[j] = temp;
}
}
}
}
// returns an array of length filled with random positive integers of up to max
std::vector<int> createranarr(size_t input_length, size_t max){
std::vector<int> myv;
for (size_t i = 0; i < input_length; i++) {
myv.push_back(rand() % max);
}
return myv;
}
void arrprinter(const std::vector<int> &input_vec) {
for (size_t i = 0; i < input_vec.size(); i++) {
std::printf("number:%zu = %d\n",i, input_vec[i]);
}
}
int main() {
std::vector<int> myvector = createranarr(10, 100);
arrprinter(myvector);
bsort(myvector);
arrprinter(myvector);
return 0;
}
+10
View File
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.24)
project(cppout)
file(GLOB SOURCE_FILES "*.cpp")
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${CURSES_LIBRARIES})
+12
View File
@@ -0,0 +1,12 @@
#include <ncurses.h>
int main() {
initscr();
move(11, 26);
printw("Hello curses");
refresh();
getch();
endwin();
return 0;
}
@@ -1,18 +0,0 @@
#include "stdio.h"
int main()
{
int i = 5;
int j = 7;
int x;
x = (i>j)-(j>i);
/*
* -1 if i less than j
* 0 if i equal to j
* +1 if i more than j
* */
printf("%d: ", x);
return 0;
}
@@ -1,94 +0,0 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
void numberprinter(int inarr[], int arrlength) {
for (int i = 0; i < arrlength; i++) {
printf("number%d: %d\n", i, inarr[i]);
}
}
struct Darray {
int *digits;
size_t length; // blijkbaar correcte eenheid voor length, malloc stuff ofzo (instead of hardcoding it)
};
struct Darray *numbertodigitDarr(int input) {
// same calculation for arrsize
int op = 0;
int tempinput = input;
while (tempinput) {
tempinput /= 10;
op++;
}
// allocate memory for struct and array, apparently more ideomatic to have a single malloc
struct Darray *DigDarr =
malloc(op * sizeof(int) + sizeof(*DigDarr) + sizeof(int));
if (DigDarr == NULL) {
printf("allocation failed");
exit(0);
}
// forgot to assign a value to the pointer variable in the struct.
DigDarr->digits = (int *)(DigDarr + 1);
DigDarr->length = op;
// insert numbers into array in reverse order
for (int i = op - 1; i >= 0; i--) {
int last_digit = input % 10;
printf("last digit: %d\n", last_digit);
DigDarr->digits[i] = last_digit;
input /= 10;
}
return DigDarr;
}
//
void checkarrdupes(struct Darray input) {
for (int i = 0; i < input.length; i++) {
printf("digitstr: %d\n", input.digits[i]);
}
int digit_seen[10] = {};
// kijk door elke arr element
for (int i = 0; i < input.length; i++) {
// kijk of input[i] al in digit_seen zit
if (digit_seen[input.digits[i]]) {
printf("duplicate number: %d\n", input.digits[i]);
} else {
digit_seen[input.digits[i]] = 1;
}
}
}
int main(void) {
int n;
printf("enter a number: ");
// TODO: scanf is fragile. Try using fgets() and strtol() for robust input
// sanitization.
scanf("%d", &n);
int digitslength = 0;
struct Darray *mydigitsDarr = numbertodigitDarr(n);
checkarrdupes(*mydigitsDarr);
free(mydigitsDarr);
return 0;
}
/*
somehow I have to find a way to get the number of digits in my number
for now, we use modulo. whatever remains is the last digit of the number
we then divide by 10 to remove the last number
we can keep a counter, and we can put it into an array. however, if we use the
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.
malloc, blijkbaar very different
*/
@@ -1,20 +0,0 @@
#include "stdlib.h"
#include "stdio.h"
int main(void)
{
float height, length, width, volume, weight;
printf("Enter height of box: ");
scanf("%f", &height);
printf("Enter length of box: ");
scanf("%f", &length);
printf("Enter width of box: ");
scanf("%f", &width);
volume = height * length * width;
weight = (volume + 165) / 166;
printf("Dimensions: %fx%fx%f\n", length, width, height);
printf("Volume: %f\n", volume);
printf("Weight: %f\n", weight);
}
@@ -1,24 +0,0 @@
#include "stdio.h"
#include "stdlib.h"
int main(){
int input;
printf("input:");
scanf("%d", &input);
// assume 3 digits,
int d1, d2, d3;
d1 = input / 100; //uitgaande van flooring
d2 = input % 100 / 10;
d3 = input % 10;
printf("%d%d%d",d3, d2,d1);
printf("next: ");
scanf("%1d%1d%1d", &d1,&d2,&d3);
printf("%d%d%d",d3,d2,d1);
return 0;
}
@@ -1,7 +0,0 @@
#include "stdio.h"
int main(void)
{
printf("To C, or not to C: that is the question.\n");
return 0;
}
@@ -1,30 +0,0 @@
#include "stdio.h"
int main()
{
/*
* modify 24 hour time to 12 hour
* take first 2 numbers,
* if numbers <=12, AM
* else PM
* */
int i ; int j; char c;
printf("input time: ");
scanf("%2d%1c%2d", &i,&c, &j);
printf("int i: %d int j: %d\n", i, j);
if(i!=12&&j==00){
i%=12;
}
if(i <=12){
printf("%d:%.2d AM",i,j);
} else {
printf("%d:%.2d PM",i,j);
}
return 0;
}
BIN
View File
Binary file not shown.
-74
View File
@@ -1,74 +0,0 @@
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
struct IntArrayStr{
int *data;
size_t length;
size_t capacity;
};
void read_int_array_str(struct IntArrayStr *in_str) {
printf("inputlength:%zu\n", in_str->length);
for (size_t i = 0; i < in_str->length; i++) {
printf("digit:%zu = %d\n", i, in_str->data[i]);
}
}
struct IntArrayStr *create_int_array_str(size_t in_capacity) {
struct IntArrayStr *mystr = malloc(sizeof(struct IntArrayStr)); /* allocate memory for struct */
mystr->data = malloc(sizeof(int) * in_capacity); /* make sure the pointer in the struct points to the array */
mystr->length = 0; //forgot to actually assign a value to the struct
mystr->capacity = in_capacity;
if (mystr->data == NULL) {
exit(0);
}
return mystr;
}
void push_int_array_str(int in_int, struct IntArrayStr *in_str){
if (in_str->length == in_str->capacity) {
in_str->capacity*=2;
printf("intarray capacity is now:%zu\n", in_str->capacity);
int *temp;
temp = realloc(in_str->data, sizeof(int)*in_str->capacity);
if(temp == NULL){exit(0);};
in_str->data = temp;
printf("intarray length is now:%zu\n", in_str->length);
}
in_str->data[in_str->length] = in_int;
printf("number:%zu = %d\n", in_str->length, in_str->data[in_str->length]);
in_str->length++; //didn't read the flow when transfering and accidentally put this in the if block
//also had an off-by-one error because I increased length before doing anything
}
int main() {
srand(time(NULL));
// change to while loop and take inputs rather than fill with random numbers
printf("input numbers:\n");
//int input;
size_t length = 10;
/* while (input != -1) { */
/* scanf("%d", &input); */
/* length++; */
/* } */
struct IntArrayStr *myintarraystr = create_int_array_str(2);
for (size_t i = 0; i < length; i++) {
int input = rand() % 10;
push_int_array_str(input, myintarraystr);
}
read_int_array_str(myintarraystr);
free(myintarraystr->data);
free(myintarraystr);
return 0;
}
-39
View File
@@ -1,39 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j;
/* TODO: define the 2D pointer variable here */
int ** pnumbers;
/* TODO: complete the following line to allocate memory for holding three rows */
pnumbers = (int **) malloc(sizeof(int)*6);
/* TODO: allocate memory for storing the individual elements in a row */
pnumbers[0] = (int *) malloc(1 * sizeof(int));
pnumbers[1] = (int *) malloc(2 * sizeof(int));
pnumbers[2] = (int *) malloc(3 * sizeof(int));
pnumbers[0][0] = 1;
pnumbers[1][0] = 1;
pnumbers[1][1] = 1;
pnumbers[2][0] = 1;
pnumbers[2][1] = 2;
pnumbers[2][2] = 1;
for (i = 0; i < 3; i++) {
for (j = 0; j <= i; j++) {
printf("%d", pnumbers[i][j]);
}
printf("\n");
}
for (i = 0; i < 3; i++) {
/* TODO: free memory allocated for each row */
free(pnumbers[i]);
}
/* TODO: free the top-level pointer */
free(pnumbers);
return 0;
}
-80
View File
@@ -1,80 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Darray {
int size;
int data[]; //flexible array member
};
//randomized array
void rarr(struct Darray *darr) {
for (int i=0; i<darr->size; ++i) {
darr->data[i] = rand() % 32;
}
if (darr == NULL) {
printf("failed");
exit(1);
}
}
struct Darray *createdarray(int mysize){
struct Darray *darrstruct = malloc(mysize * sizeof(int) + sizeof(*darrstruct));
if (darrstruct == NULL) {
printf("failed");
exit(1);
}
darrstruct->size = mysize;
rarr(darrstruct);
return darrstruct;
}
void msort(int *myarr, int mysize) {
for (int i = 0 ; i < mysize; i++) {
for (int j = 0 ; j < mysize ; j ++)
if (myarr[i] > myarr[j]) {
int k = myarr[i];
myarr[i] = myarr[j];
myarr[j] = k;
}
}
}
void bsort(struct Darray *myarr) {
for (int i = 0; i < myarr->size; i++) {
for (int j = 0; j < myarr->size -1 ; j++ ) {
if (myarr->data[j] < myarr->data[j+1]) {
int k = myarr->data[j];
myarr->data[j] = myarr->data[j+1];
myarr->data[j+1] = k;
}
}
}
}
int main() {
srand(time(NULL));
int mysize = 7;
struct Darray *myarray;
myarray = createdarray(mysize);
printf("myarr:\n");
for (int i = 0 ; i < myarray->size; i++) {
printf("%d\n", myarray->data[i]);
}
bsort(myarray);
printf("sorted myarr:\n");
for (int i = 0 ; i < myarray->size; i++) {
printf("%d\n", myarray->data[i]);
}
free(myarray);
return 0;
}
-15
View File
@@ -1,15 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
struct node * next;
};
struct node_t * head = NULL;
head = (struct node_T*)malloc(sizeof(struct node_t));
int main(){
return 0;
}
-36
View File
@@ -1,36 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int *pascallarr(int iarrsize, int *ptr){
int *mypascallar = malloc(sizeof(int) * iarrsize + 1);
/* declare edges, as they're always 1 anyways so we can start the loop at 1 (so i-1 = 0) */
mypascallar[0] = 1;
mypascallar[iarrsize] = 1;
for (int i = 1; i <= iarrsize; i++) {
if (i < iarrsize) {
mypascallar[i] = ptr[i-1] + ptr[i];
}
}
return mypascallar;
}
int main() {
int trianglesize = 18;
int ** pnumbers;
/* create pascals triangle, on row i, we have i numbers */
/* should create 2d array of ints, with an array of pointers to arrays*/
int **ptrtriangle = malloc(sizeof(int *)*trianglesize);
/* need to create a function that returns the pointer to an array of i numbers filled with numbers */
for (int i = 0; i < trianglesize; i++) {
ptrtriangle[i] = pascallarr(i,ptrtriangle[i-1]);
for (int j = 0; j <= i; j++) {
printf("%d|",ptrtriangle[i][j]);
}
printf("\n");
}
return 0;
}
-58
View File
@@ -1,58 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int recfact(int x){
int y = 1;
if (x==1){
return y;
} else {
return y *= x *recfact(x-1);
}
}
int recfor(int x){
int gaga = 1;
for (int i = 1; i < x;) {
gaga *= i++;
}
return gaga;
}
/*
* i = 1, gaga = 1*2
* i = 2, gaga = 1*2*3 = 6
* i = 3, 1*2*3*4 = 24
*
* wrong loop time condition
* wrong calculation
*
* ---
*
* recursion
*
* forgot to assign, forgot a number (multiplied by 0)
* misunderstood calculation
wel exploren, maar wel naar een doel.
kijken naar auxililiaries.
curiosity wss, leren vragen. leren wat ik niet heb, weten wat ik niet weet.
zoektocht heeft wat sturing nodig.
hoe ik mijn probeer stel/vind
het probleem onderzoeken
limiteer de pogingen
hardware / networking
* * */
int main () {
int i = 6; // we can not handle negative numbers
printf("%d\n", recfor(i));
printf("%d\n", recfact(i));
return 0;
}
-46
View File
@@ -1,46 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
/*
* factorials
*
* in both for loop, as well as recursive
* factorial: voor int i, vermenigvuldig dan elke i to je bij 1 komt (uit gaande van positive ints)
*
* dus, f(3)=3*2*1 = 6 ; f(4)=4*3*2*1=24 ; f(a)=no ; f(-2)= no ; f(2.1) = no
*
* will use 2 functions:
* recfac using recursion
* itfac using iteration
*
* should probably figure out how to test this? maybe a dumb K:V?
* * */
int recfact(int input){
int fac = 1;
if (input==1) {
return fac;
}
else {
fac *= input * recfact(input-1);
}
}
int itfact(int input){
int sum = 1;
for (int i = 1; i <= input; i++) {
sum *= i;
}
return sum;
}
int main(){
int input = 5;
//recfact(input);
//itfact(input);
printf("%d\n", recfact(input));
printf("%d\n", itfact(input));
return 0;
}