Anton Vorderman
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 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.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully