///////////////////////////////////////////////////////////////////
//  Some examples of operations on arrays shown in class
///////////////////////////////////////////////////////////////////

#define SIZE 20

int x[SIZE], y[SIZE], i, t;

// Initialize all array elements to a value:
    for (i=0; i<SIZE; i++) {
        x[i] = value;
    }

// Add one to all elements in array:
    for (i=0; i<SIZE; i++) {
        x[i]++;
    }

// Print all elements of an array       
    for (i=0; i<SIZE; i++) {
        printf("Value of x[%d] is %d\n", i, x[i]); 
    }

// Input elements of an array       
    for (i=0; i<SIZE; i++) {
        scanf("%d", &x[i]);     // Note & and [] are both operators
    }

// Shift the elements of an array down one
    for (i=0; i<SIZE-1; i++) {      // note SIZE-1
        x[i] = x[i+1];
    }

// Shift the elements of an array up one
    for (i=SIZE-1; i>0; i--) {      // note bounds on i
        x[i] = x[i-1];
    }

// Copy arrays
    x = y;      // Error - does not work in C!

    for (i=0; i<SIZE; i++) {
        x[i] = y[i];
    }

// Swap arrays x and y
    for (i=0; i<SIZE; i++) {
        t = x[i];
        x[i] = y[i];
        y[i] = t;
    }

// Copy shifted  
    for (i=0; i<SIZE-1; i++) {
        x[i] = y[i+1];
    }

// Copy y reversed to x
    for (i=0; i<SIZE; i++) {
        x[i] = y[SIZE-1-i];
    }

// Reverse array x "in place"
    for (i=0; i<SIZE/2; i++) {
        t = x[i];
        x[i] = x[SIZE-1-i];
        x[SIZE-1-i] = t;
    }

// Add array y to array x: 
    for (i=0; i<SIZE; i++) {
        x[i] += y[i];
    }   

// Sum and average of elements of an array 
    sum=0; 
    for (i=0; i<SIZE; i++) {
        sum += x[i];
    }
    average = sum / SIZE;
    averageRounded = (sum + SIZE/2) / SIZE;

// minimum of array elements: 
    min = MAXPOSSIBLEVALUE;
    min = x[0];                 // Alternate initial value
    for (i=0; i<SIZE; i++) {    // Can start at 1 if min initialized to x[0]
        if (min > x[i]) min = x[i]; // OK in my book without {} if all on one line
    }   

// Search array for a particular value 
    for (i=0; i<SIZE; i++) {
        if (x[i] == value) break;
    }
    if (i==SIZE) printf("value %d not found in array x[]\n", value);
    else prinf("Value %d found at index %d in array x[]\n", value, i);

// String arrays:
    char mystring[] = "Hello";                        // string literal
    char mystring[] = {'H', 'e', 'l', 'l', 'o', '\0'};    // same - terminating \0
    // mystring[0] is 'H', etc.

    char string[20];    // can hold 19 characters plus terminating '\0'
    scanf("%s", string);    // reads characters until tab, space, newline or EOF
    scanf("%19s", string);  // safer - reads 19 characters max
    printf("%s\n", string); // will print string - note must be null terminated!
    printf(string);     // Functions same as previous line

// Arrays local to function
    static int a[5];    // static local - elements initialized to 0 once at program start
    static int b[5] = {2, 3, 5, 7, 11}; // static local - initialized once
    int c[5];               // automatic local - not initialized automatically (ever)
    int d[5] = {0};         // automatic local - initialize each element to 0

// Passing arrays to functions - only the address of the first element is passed (pass by reference)
    void sortArray(char arr[], int size);   // Function header/declaration only has [] (size ignored)
                                            // Unlike single variables, 
                                            // the function can change the array elements

    int findMax(const int arr[], int size); // Use const keyword to indicate the function should 
                                            // not be able to change the array elements

    sortArray(x, SIZE);         // Pass the array name to the function
    findMax(x, SIZE);


// Sort array using straight insertion 
void sortArray(char x[], unsigned char length) {
    char ctemp;
    signed int i, j;
    
    for (j=1; j<length; j++) {      // sort longer and longer sublists adding one element at a time
        ctemp = x[j];                   // pull out last element in sublist into ctemp
        for (i = j-1; i>=0 && x[i]>ctemp; i--) { // shift elements up that are bigger
            x[i+1] = x[i];
        }
        x[i+1] = ctemp;             // Insert value back into new "hole" in list
    }
}


// In C there is no array bounds checking (unlike some other languages)
    x[-1] = 0;      // ERROR
    x[SIZE] = 0;    // ERROR 
// C can be more efficient - but it requires the programmer to be very careful







