IC
This commit is contained in:
Executable
BIN
Binary file not shown.
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user