COMP 1917 Computing 1
Session 2, 2016

Tutorial - Week 13


Presentation Topic for Week 13

For many of you the next step will be taking COMP1927(COMP1521???) where you will learn about data structures and some well known algorithms. One of the crucial concepts in that course is complexity. Explain to the rest of the class what is "big O notation" and give example of programs running in O(1), O(n) and O(n^2).

  1. What will be the value of the variable z after the following code is executed?
    
    int x = 1; 
    int y = 2; 
    int z = 3; 
    if (x < y) { 
        if (y > 4) { 
            z = 5; 
        } else { 
            z = 6; 
        } 
    }     
    
    
  2. What will be the value of result after the following code statements are executed?
    
    int nums1[LENGTH] = { 1, -5, 2, 0, 4, 2, -3 };
    int nums2[LENGTH] = { 1, -5, 2, 4, 4, 2, 7 };
    int result = 0;
    int j = 0;
    while (j < LENGTH) {
        if (nums1[j] != nums2[j]) {
            result = result + 1;
        }
        j = j + 1;
    }
    
    
  3. What is the purpose or outcome of the following piece of code?
    int number[LENGTH];
    ...
    int result = 0;
    int j = 0;
    while ( j < LENGTH) {
       if (number[j] < 0) {
           result = result + 1;
       }
       j++;
    }
    

  4. When the following code is compiled and executed it eventually executes the printf and terminates. What value does it print?
    #include <stdio.h>
    #define NINE 9
    
    int nine (int n);
    int nein (int n);
    int main (int argc, char *argv[]) {
       
       int Nine = 3;
       printf ("%d\n", nine (Nine));
       return !NINE;
    }
    
    int nein (int nein) {
       return ( !(nein == ((NINE*nein) % NINE)) );
    }
    
    int nine (int Nein) {
       int Nine = NINE;
       while (nein (Nine)) {
          Nine += Nein;
          Nine = Nine % NINE;
       }
       return Nine;
    }
    

  5. Describe the memory layout of a typical C program. What information can be stored in each of the segments?

  6. Consider the following C function prototype:
    int f(int *x, int y);
    
    Which of the following statements is true of argument `x`? What about variable y?
     .[A] it is passed by magic
     .[B] It is passed by copying
     .[C] It is passed by reference
     .[D] It is passed by abstraction
     .[E] None of the above
    

Ask your tutor any questions you might have about assignments, exam or any other topic.

More past papers are available at the following link.