start king

This commit is contained in:
Your Name
2026-05-02 03:10:01 +02:00
parent 05e167b58a
commit 64ef5c8a7c
8 changed files with 42 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
#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
@@ -0,0 +1,80 @@
#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
@@ -0,0 +1,15 @@
#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
@@ -0,0 +1,36 @@
#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
@@ -0,0 +1,58 @@
#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
@@ -0,0 +1,46 @@
#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;
}