#include <stdio.h>


/*
 *  ==============================================
 *  CSC 151 Lab:  Pointers, Arrays, and I/O in C
 *  =============================================
 */

/*
 *  PART I:  Formatted Output
 * 
 *  C provides the printf function for formatted output.
 *  This has a very flexible mechanism for creating output
 *  in any desired format.  We give a few examples, and 
 *  then ask you to take over.
 */

void printf_practice() {
    // Begin by defining some variables.  These will be all
    // we will need for this part of the lab.
    int a = 7;
    int b = -12;
    int c = 65536;
    int d = 0;
    char f = 'A';
    char g = 52;
    char* r = "A string";
    char* s = "barrel of monkeys";
    char* t = "supercalifragilisticexpialidocious";
    float x = 1.414;
    float y = 6.02e23;
    float z = -2.7183e-2;

    // Now look at some example print statements and their output.
    // Do you understand why they produce what they produce?
    // The first few examples use no variables, but demo \n, \t, and \\.

    // \n starts a new line:
    printf("*\n**\n***\n\n");

    // \t advances to the next tab stop:
    printf("#\t####\t######\n###\t##\t####\n\n");

    // \\ gives one backslash character:
    printf(" .-.\n(o o) boo!\n| O \\\n \\   \\\n  `~~~'\n\n");

    // The next set of examples includes variables.

    // Basic variable output types:
    printf("An int: %d; a float: %f; a string: %s\n\n",a,x,t);

    // Float variations:
    printf("%f\n",z);
    printf("%7.4f\n",z);
    printf("%11.8f\n",z);
    printf("%5.2f\n",z);
    printf("%2.0f\n",z);
    printf("%g\n",z);
    printf("%f\n",y);
    printf("%g\n\n",y);

    // Int variations:
    printf("%d\n",g);
    printf("%5d\n",g);
    printf("%05d\n",g);
    printf("%c\n",g);
    printf("%d\n",f);
    printf("%c\n\n",f);

    ////////////////
    // Exercise 1 //
    ////////////////

    /* Using the variables a, b, and x, write a single printf that will produce the following line of output:

Hermione scored 7 points for Griffindor, but Ron scored -12 for not knowing the square root of 2 is 1.41

    */

    ////////////////
    // Exercise 2 //
    ////////////////

    /* Using the variables a, f, and s, write a single printf that will produce the following lines of output:

James Bond is agent 007.  
There are 65 animals in a barrel of monkeys.

    */

    ////////////////
    // Exercise 3 //
    ////////////////

    /* Using the variable t, write a single printf that will produce the following line of output:

supercalifragilisticexpialidocious supercalifragilisticexpialidocious supercalifragilisticexpialidocious!

*/
}


/*
 *  PART II:  Working with Pointers
 * 
 *  Pointers allow you to look at where your variables are stored in memory.
 *  This part of the lab gives you some practice with pointer referencing and 
 *  dereferencing.  We do some examples, and then there are exercises for you.
 * 
 *  This section begins with some short functions, some of which you will modify.
 *  Skip over these for now, and come back to them when instructed in the main
 */

// An example function:  no effect in caller
void fun1(int a, int b) {
    printf("Address of a in fun1:  %ld (different from caller)\n",&a);
    a = b;
}

// Another example function:  sets a in the caller to the value of b
void fun2(int *a, int b) {
    printf("Address of a in fun2:  %ld (same as caller)\n",a);
    *a = b;
}

// A third example function:  swap two pointers so that they point to each others' original spots
void fun3(int **a, int **b) {
    int *tmp = *a;
    *a = *b;
    *b = tmp;
}

// To be completed with Exercise 4
void fun4(int *a, int *b) {
    // add your code here
}

// To be completed with exercise 5
void fun5(int **ppa, int *pb) {
    // add your code here
}

void pointer_practice() {
    int a = 7, b = 3;
    int *pa = &a, *pb = &b;
    int **ppa = &pa, **ppb = &pb;

    // We can look at pointer values.
    printf("Address of a:  %ld\n",pa);
    printf("Address of b:  %ld\n",pb);
    printf("Address of pa:  %ld\n",ppa);
    printf("Address of pb:  %ld\n\n",ppb);

    // Pointers give multiple ways to access values
    printf("a = %d = %d = %d\n",a,*pa,**ppa);

    // With call-by-value, variables in calling function won't change
    printf("Address of a in caller:  %ld\n",&a);
    fun1(a,b);
    printf("a = %d (unchanged)\n",a);

    // With call-by-reference, variables in calling function can change
    fun2(&a,b);
    printf("a = %d (changed)\n",a);

    // Swapping pointers
    printf("Before:  a = %d, b = %d; *pa = %d, *pb = %d\n",a,b,*pa,*pb);
    fun3(&pa,&pb);
    printf("After:  a = %d, b = %d; *pa = %d, *pb = %d\n",a,b,*pa,*pb);
    
    // Reset:
    pa = &a;
    pb = &b;

    // Dereferencing a pointer that isn't a valid memory address will 
    // probably cause your program to crash.
    // Uncomment the line below to see what happens, then comment it again.
    //int *pp = (int*)0; printf("%d",*pp);


    ////////////////
    // Exercise 4 //
    ////////////////

    // Write code for fun4 above so that the call below swaps the values
    // of a and b.
    // (Hint:  you will need a temporary variable.  See fun3 for an example.)
    a = 7; b = 3;
    fun4(&a,&b);

    // Test of your result:  should print a = 3, b = 7
    printf("Exercise 4 test:  a = %d, b = %d\n",a,b);

    ////////////////
    // Exercise 5 //
    ////////////////

    // Write code for fun4 above so that the call below makes pa point to b
    printf("Before:  pa = %ld\n",pa);
    printf("Before:  pb = %ld\n\n",pb);
    b = 11;
    fun5(&pa,&b);

    // Test of your result:  should print a = 3, b = 7
    printf("Exercise 5 test:  a = %d, b = %d, *pa = %d\n\n",a,b, *pa);

    printf("After:  pa = %ld\n",pa);
    printf("After:  pb = %ld\n\n",pb);

    // Thought question:  would it be possible to do the same thing with a functon
    // that was called like this? : fun4a(&pa,b);
}


/*
 *  PART II:  Working with Arrays
 * 
 *  Arrays are blocks of contiguous memory storing multiple values of the same type.
 *  They may be allocated on the heap using malloc, or on the stack as an array declaration.
 * 
 *  For today, we won't look much at the allocation. Instead we'll look at how to
 *  access individual elements
 */

void array_practice() {
    int r[5] = {1,5,25,125,625};
    char s[40];
    sprintf(s,"One fish two fish red fish blue fish");

    // We can access each of the elements of an array using an index
    // Note that counting begins at zero and goes up to one less than the array length
    printf("Powers of 5:  5^0 = %d, 5^1 = %d, 5^2 = %d, 5^3 = %d, 5^4 = %d\n",r[0],r[1],r[2],r[3],r[4]);

    // The [] index is an easier alternative to pointer arithmetic, which can do the same thing
    printf("Powers of 5:  5^0 = %d, 5^1 = %d, 5^2 = %d, 5^3 = %d, 5^4 = %d\n\n",*r,*(r+1),*(r+2),*(r+3),*(r+4));

    // Once declared, the length of an array cannot change.  
    // Attempts to access nonexistent elements will give unpredictable results.
    // The program may crash, or do something strange as memory gets overwritten.
    // Uncomment the lines below one at a time to see what happens, then comment again
    //r[-1] = 0; 
    //r[6] = 3125; 
    //printf("%d\n",r[20]);
    //printf("%d\n",r[20000]);

    // Strings are arrays of char in C.  Each element is an ASCII code.
    // You can edit a string by modifying the characters inside it.
    printf("%s\n",s);
    s[8] = 0;
    printf("%s\n",s);
    s[8] = '-';  // note use of single quotes around char values (distinct form strings which use double quote)
    printf("%s\n",s);

    // If there is enough space allocated, you can extend the string.
    s[36] = 'i';
    s[37] = 'e';
    s[38] = 's';
    s[39] = 0;
    printf("%s\n",s);


    ////////////////
    // Exercise 6 //
    ////////////////

    // Write code to replace the word 'blue' in the string with 'teal'
    // (Hint:  you will have to replace four elements.)
    
    printf("%s\n",s);


    ////////////////
    // Exercise 7 //
    ////////////////

    // Write code to replace each lower-case f with an upper-case F.
    // (Hint:  you will have to replace four elements.)

    printf("%s\n",s);
}

void main() {
    // This just calls each of the lab sections, in turn.
    printf("**********\n* Part I *\n**********\n\n");
    printf_practice();
    printf("***********\n* Part II *\n***********\n\n");
    pointer_practice();
    printf("************\n* Part III *\n************\n\n");
    array_practice();
}
