CS101 Prelab 11

Name: ______________________      NetId: _______________      Section: _______

Print out this prelab and answer the following questions. Hand in this assignment at the beginning of Lab 11. You should use the material found in Lectures 18, 19&20 of the course notes and/or the course C book Chapters 12.1, 12.2, 20, and 22.2.1. As with all prelabs/labs, if you are not sure of the answer you may go to any EWS lab on campus and check your results by using the gcc compiler, or you can ask anyone of the CS101 staff for help.

The following exercises are about pointers in C and should help prepare you for future lab activities MP2 and the Final Exam.

In this prelab, you'll demonstrate your knowledge regarding

  1. Write the output when the following program executes.
       #include <stdio.h>
    void dosumpin(int a, double b[]);
    int didsumpin(int d, double e[]);
     
    void main(void){
      int  a = 9;
      int c;
      double b[2] = {4.0,3.0};
     
      printf("a = %i b[0] = %lf  b[1] = %lf\n",a, b[0], b[1]);
      dosumpin(a,b);
      printf("a = %i b[0] = %lf  b[1] = %lf\n",a, b[0], b[1]);
      dosumpin(5,b);
      printf("a = %i b[0] = %lf b[1] = %lf\n",a, b[0], b[1]);
      b[0] = 6.0;
      b[1] = 7.0;
      printf("a = %i b[0] = %lf b[1] = %lf\n",a, b[0], b[1]);
      c = didsumpin(a,b);
      printf("a = %i b[0] = %lf b[1] = %lf c = %i\n",a , b[0], b[1], c);
    }

    void dosumpin(int a, double b[]){
      b[0] = b[0] + a;
      b[1] = b[1] + a;
      a = 7;
    }
     
     
    int didsumpin(int d, double e[]){
      e[0] = e[0] + d;
      e[1] = e[1] + d;
      return 3*d;
    }


     a = _______________ b[0] = _______________ b[1] = _______________


     a = _______________ b[0] = _______________ b[1] = _______________


     a = _______________ b[0] = _______________ b[1] = _______________


     a = _______________ b[0] = _______________ b[1] = _______________

     
    a = _______________ b[0] = _______________ b[1] = _______________ c = _______________


  2. Write the output when the following program executes.
    #include <stdio.h>
     
     
      int fun1(int x)
    {
    printf(" %i ", x);
    return x + 2;
      }
     
      void main(void)
    {
    int x = 1;
       printf(" %i ", fun1(x));
    printf(" %i\n", x);
      }

    _____________________ ___________________________ ___________________________
  3. Write the output when the following program executes.
  4.   #include <stdio.h>
     
      int i = -5; /* global variable */
     
      int fun2(int x)
    {
    int i = 37; /* another variable named i */

    printf(" %i ", i);
    return x + i;
      }
     
      void main(void)
    {
       printf(" %i \n", fun2(i));
    printf(" %i\n", i);
      }
    __________________________________________

    __________________________________________
     
     
  5. Write the output of the following code. You may have to compile and run this program.
    #include <stdio.h>

    void main(void)
    {
    int x;
    int * ptr; /* ptr is a "pointer" variable. Assign only computer memory addresses to ptr !!! */
    x = 51;

    printf("x is %i\n", x);

    ptr = &x; /* ptr now points to x , &x means... address of x */
    *ptr = 17; /* * operator (dereferencing operator), *ptr means ... the thing ptr points to (which is x in this example)*/
    printf("x is %i\n", x);

    ++x;
    printf("*ptr = %i \n", *ptr); /* so using *ptr is like using x */

    ++(*ptr); /* has same effect as ++x */
    printf("x = %i\n",x);
    }


    ________________________________________




    ________________________________________




    ________________________________________




    ________________________________________

  6. Write the output when the following program executes. See lecture 19&20 slide #15.
  7.   #include <stdio.h>
     
     
     
      void main(void)
    {  
    char str[] = "cat";
       char * ptrA = "dog"; /* note that ptrA is a pointer, it won't hold data of type char, it holds addresses */
    char * ptrB = str; /* the name of an array holds the address of the first byte of an array */
      /* so an array name is a pointer (its value can't be changed however) */
    printf(" %s ", str);
    printf(" %s ", ptrA);
    printf(" %s \n", ptrB);
      printf("%c %c \n", ptrB[0], *ptrB); /* if the name of an array is a pointer conversely a pointer can be used */
    /* an array name */
      }

    _________________________________________________________
     
     
    _________________________________________________________

  8. Write the output of the following program.
    #include <stdio.h>

    void swap1(int a,int b)
    {
    int temp = a;
    a = b;
    b = temp;
    }

    void swap2(int *a,int *b)
    {
    int temp = *a;
    *a = *b;
    *b = temp;
    }

    void swap3(int * x)
    {
    int temp = x[0];
    x[0] = x[1];
    x[1] = temp;
    }

    void main(void)
    {
    int a = 91, b = 16;
    int c[2];
    printf("%i %i\n",a,b);
    swap1(a,b);
    printf("%i %i\n",a,b);
    swap2(&a,&b);
    printf("%i %i\n",a,b);
    c[0] = a; c[1] = b;

    /* 'c' is the name of an array therefore c is a constant pointer */
    /* see lecture 19&20 slide #14 */
    /* remember this example when you write MP 2 */
    swap3(c);
    printf("%i %i\n",c[0],c[1]);

    }


    _________________________________

    _________________________________

    _________________________________

    _________________________________



  9. Write the output of the following code. It might help to draw a picture of memory. Remember, if p2 is a pointer then p2[0] means *(p2 + 0).
    #include <stdio.h>

    void main(void)
    {

    int x[2]={4,9};
    int *p1, *p2;
    int a = 10, b = 1;

    printf("%i %i \n",x[0],x[1]);
    p1 = x;
    printf("%i %i \n",p1[0],p1[1]);
    p2 = &p1[1];
    printf("%i \n",p2[0]);
    *p1 = a;
    *p2 = b;
    printf("%i %i \n",p1[0],p1[1]);
    printf("%i \n",p2[0]);
    }




    _________________________________

    _________________________________

    _________________________________

    _________________________________

    _________________________________