COMP1911 23T2 Introduction to Programming
  1. Given these declarations
    int     n;
    int    *p, *q;
    
    What will happen when each of the following statements are executed (in order)?
    p = &n;
    *p =  5;
    *q = 17;
    q =  p;
    *q =  8;
    

  2. We haven't used scanf to it's full potential yet. What do you think the following program will output?
    int main(void) {
        int scanfResult;
        int a, b, c;
        scanfResult = scanf("%d %d %d", &a, &b, &c);
        printf("%d %d %d %d", scanfResult, a, b, c);
    }
    

  3. Consider the following code
    void swap (int x, int y);
    
    int main(void) {
        int x = 10;
        int y = 99;
        
        printf("%d %d\n",x,y);
        
        swap(x,y);
        
        printf("%d %d\n",x,y);
        
    
        return EXIT_SUCCESS;
    }
    
    void swap (int x, int y) {
       int tmp;
       tmp = x;
       x = y;
       y = tmp;
    }
    
    1. What will the output be and why?

    2. Modify the code so that the function prototype is void swap (int *x, int *y);

    3. What will the output of the modified code be and why?

  4. Consider:

    float ff[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
    float *fp = ff;
    

    What are the similarities between ff and fp? What are the differences?

  5. What would be the output of the following code?
    int x = -9;
    int *p1 = &x;
    int *p2;
    
    p2 = p1;
    printf("%d\n", *p2);
    *p2 = 10;
    printf("%d\n",x);
    
    -9
    10
    
  6. Write a function with the prototype below that calculates the sum and the product of all the integers in the given array.
    void sumProd(int nums[], int len, int *sum, int *product);