# Week 1 Array
## Team
Team name: Team Afwezig
Date: 17/02/2021
Members:
Anton
Jesse
Larissa
| Role | Name |
| ------------------------------------------------------------ | ------- |
| **Facilitator** keeps track of time, assigns tasks and makes sure all the group members are heard and that decisions are agreed upon. | Larissa |
| **Spokesperson** communicates group’s questions and problems to the teacher and talks to other teams; presents the group’s findings. | Anton |
| **Reflector** observes and assesses the interactions and performance among team members. Provides positive feedback and intervenes with suggestions to improve groups’ processes. | Larissa |
| **Recorder** guides consensus building in the group by recording answers to questions. Collects important information and data. | Jesse |
## Activities
### Activity 1: Fixed-sized arrays
Discuss with your group members what the advantages and disadvantages are of having an array with a fixed capacity. Provide at least one use case where arrays with a fixed capacity are useful.
Answer:
Als je als docent cijfers invoert van je klas dat je fixed capacity het aantal studenten in je klas is. Want de hoeveelheid studenten in de klas staat vast. Daarnaast is het een stuk makkelijker om een array van een vaste grootte te gebruiken. Als je een array van oneindige grootte wilt gebruiken zul je memory allocating moeten gebruiken.
### Activity 2: Random access
Can you think of a situation or use case where having random access is not necessary? Provide at least one use case.
Answer:
Bijvoorbeeld file storage op je computer. Al je bestanden staan in mapjes gesorteerd. Deze staan dan in sequential access. Op deze manier zijn bestanden ook erg makkelijk te restoren.
### Activity 3: The sizeof operator
Find out, using the sizeof function, how many bytes are used to store a memory address (a pointer). Does the size of a pointer depend on its type? What is the reason for this?
Answer:
8 bytes
Nee want de pointer slaat alleen het memory adress op. Dit adres is altijd dezelfde grootte. De size is dus altijd hetzelfde.
### Activity 4: The sizeof operator and arrays
Use the sizeof operator to find out what the size of an array is.
Answer:
```c=
int array[10]; // an array of 10 ints
printf("%d", sizeof(array));
Output:
40 bytes
```
### Activity 5: Variables in function scope
Discuss: Does this function work properly? Provide arguments supporting your answer.
Answer:
De functie werkt. De functie is bedoeld om een array te initialiseren. Deze wordt dan geinitialiseerd met behulp van de eerder gemaakte struct.
```c=
typedef struct Array_T
{
int capacity;
int count;
int *values;
} Array;
void initArray(Array *pArray, int capacity)
{
int values[capacity];
pArray->capacity = capacity;
pArray->values = values;
pArray->count = 0;
}
int main(){
Array myArray;
initArray(&myArray, myArray.capacity);
return 0;
}
```
### Activity 6: Stack vs. heap
Search on the internet what the difference between the stack and the heap is, and explain these
differences in your own words.
Answer:
De stack en de heap zijn allebij deel van het RAM geheugen. De stack kan alleen local variables accessen en de heap kan ook global variables accessen. De stack wordt altijd van bovenaf aagevuld of weggehaald (LIFO "last in first out"). De heap kan op elke plek worden aangevuld of dingen worden weggehaald. Hierdoor kan het geheugen ook "fragmented" worden.
### Activity 7: Using malloc
And that's how you inser code blocks:
Allocate memory to store one unsigned long number:
```c=
unsigned long *number;
printf("%d", sizeof(&number));
number = malloc(sizeof(number));
```
Allocate memory to store 256 float numbers:
``` c=
int size = 256;
float *array[size];
printf("%d", sizeof(&array[size]));
*array = malloc(sizeof(array[size]));
```
Write a function that takes a count as its argument, allocates enough memory to hold
count ints and returns the pointer to the allocated memory block:
``` c=
int *allocateMemory(int count){
int *number[count];
*number = malloc(sizeof(number[count]));
return *number;
}
}
```
### Activity 8: Limits of malloc
Answer:
Je kunt 16711568 bytes toewijzen
### Activity 9: Creating an array with dynamic capacity
Below, the prototype for the function array_init is given. This function initializes an empty array (i.e. count = 0) with a given capacity. Implement the function by making use of the malloc function
```c=
// creates an empty array with the given capacity
void array_init(Array *pArray, int capacity);
```
```c=
void array_init(Array *pArray, int capacity){
int values[capacity];
pArray->capacity = capacity;
pArray->values = values;
pArray->count = 0;
pArray = (int*) malloc(capacity * sizeof(int));
}
```
### Activity 10: Dangerous frees
Find out what happens when you try to call the free function on a pointer that was not obtained
using malloc, and on a pointer, the value of which is NULL:
```c=
int main() {
int* ptr = (int*)123456789;
printf("Before free: \n%d\n", ptr);
printf("%p\n", &ptr);
free(ptr);
printf("After free: \n%d\n", ptr);
printf("%p\n", &ptr);
int* null_ptr = NULL;
printf("Before free: \n%d\n", null_ptr);
printf("%p\n", &null_ptr);
free(null_ptr);
printf("After free: \n%d\n", null_ptr);
printf("%p\n", &null_ptr);
return 0;
}
```
Er gebeurt niks de waardes en de adressen blijven hetzelfde omdat de pointers niet een plek hebben in de heap.
### Activity 11: Infinite memory?
Discuss: What happens when the dynamic memory that is no longer needed is not freed?
How is it called when such a thing happens and what can be the consequences of not reclaiming
memory in a longer run?
Answer: It is called leaked memory. En het kan er voor zorgen dat je programma crashed
### Activity 12: Resizing an Array
Implement the resize function with a prototype given below.
```c=
#include <stdio.h>
#include <stdlib.h>
typedef struct Array_T{
int capacity;
int count;
int *values;
} Array;
void initArray(Array *pArray, int capacity){
int values[capacity];
pArray->capacity = capacity;
pArray->values = values;
pArray->count = 0;
}
// resizes the array so that it can contain at most newCapacity elements
void resize(Array *pArray, int newCapacity){
int values[newCapacity];
pArray->capacity = newCapacity;
}
int main() {
Array *myArray;
initArray(myArray, 5);
resize(myArray, 10);
return 0;
}
```
### Activity 13: Array insertion
Suppose an element is inserted into an array that contains n elements. What kind of steps are
involved in this insertion? What is the worst-case number of steps that need to be performed?
Answer:
Het verplaatsen van alle elementen en dan op het laatst moet het nieuwe element op de goede plek gezet worden.
Stel je wilt het nieuwe element op de eerste plek zetten in een array die op één plek na helemaal vol is. In dit geval moet je n-1 acties uitvoeren om alles te verplaatsen en daarna moet je het nieuwe element op de juiste positie zetten. Het slechtste geval aantal stappen is dan dus gelijk aan 'n'.
### Activity 14: Basic list operations
Implement the functions that are described above, and listed below. Make sure to use the
resize function to dynamically resize the array when necessary.
```c=
// append an element to the end of an array
void array_append(Array *pArray, int element);
// remove an element from the array at index
void array_remove(Array *pArray, int index);
// remove all elements
void array_clear(Array *pArray);
// insert an element into the array, before index
void array_insert(Array *pArray, int index, int value);
// swap the two elements located at indices index_a and index_b
void array_swap(Array *pArray, int index_a, int index_b);
```
Answer:
### Activity 15: Test your code
### Activity 16: Solve the partitioning problem
## Look back
### What we've learnt
Fill in...
De malloc functie
### What were the surprises
Fill in...
Bij activity 10 kon Larissa de code gewoon runnen, maar bij Anton crashte het programma (na testen bleek de crash te komen door de 'free()' statement).
### What problems we've encountered
We liepen tegen vraag 5 aan. Wij vonden de vraag een beetje onduidelijk. Wij hadden het idee dat we uiteindelijk gewoon uit moesten leggen wat de functie nou doet.
Op de vraag: "Does this function work properly, and why?" kun je ook zeggen, ja hij werkt want hij geeft geen foutcodes.
Bij vraag 7 konden we er niet achter komen hoe we het geheugen van 256 floats konden toewijzen.
### What was or still is unclear
Naar aanleiding van activity 6 en 7:
Malloc voor de heap en een pointer zonder malloc voor de stack??
### How did the group perform?
How was the collaboration? What were the reasons for hick-ups? What worked well? What can be improved next time?
Answer:
Na een onrustig begin van het missen van de eerste les hebben we onszelf goed herpakt en hebben de rollen netjes verdeeld. De meeste opdrachten hebben we samen uitgewerkt en dit ging prima. Bij opdrachten die we individueel hadden gemaakt hebben we achteraf samen besproken zodat we wel allemaal het antwoord begrepen.