/* 
* Code Snippets for this week
* Small programs to show bits of C and bits of C. 
* >> I've tried to put * at the start of all the comment lines. 
*/

/* to declare an array 
* static memory allocaiton - at compile time 
*/
const int MAX_ARRAY_SIZE = 10;

/* type name [ size ] */
int myNumbers[MAX_ARRAY_SIZE];

float anotherExample[10];

/*The above is illistrative but not good practice ! */

/* access - use array name and index */
myNumbers[0] = 1;

/* Working with arrays - short example */

#include <stdio.h>

int main(char* argv, int argc)
{
    /* Declare array size*/
    const int MAX_ARRAY_SIZE = 10;

    /* An array of integers */
    int myNumbers[MAX_ARRAY_SIZE];

    /*Declare a single integer and read something in */
    int aNumber;
    scanf("%d",&aNumber);

    /* assign an element of the array using its index */
    myNumbers[0] = aNumber;

    /*read directly into array */
    scanf("%d",&myNumbers[1]);

    /*getting things out of the array */
    aNumber = myNumbers[1];

    printf("Number at index 1 = %d\n",aNumber);

    /*print out directly */
    printf("Number at index 0 = %d \n",myNumbers[0]);
}

/* Array alloation - dynamic 
* Use this when you don't know in adavance how bit the array will be, 
* and there is no safe maximum.  
*/

/* Must include stdlib for malloc/free/new/delete */
#include <stdlib.h>

/* take a non-constant from somewhere, e.g. keyboard, file etc. */
int size = 10;  

/* using C */

/* v pointer          v cast             v memory required for one */
int* dynamicCArray = (int*)malloc(size * sizeof(int));

/* use the array   .... */

/* free at the end and set pointer to NULL to avoid accidental re-use */
free(dynamicCArray);
dynamicCArray = NULL;

/* using C++ */
int *dynamicCppArray = new int[size];

/* use the array   .... */

/* delete at the end and set pointer to NULL to avoid accidental re-use */
delete [] dynamicCppArray;
dynamicCppArray = NULL;


