Worksheet: C1 | CS 2113 Software Engineering - Fall 2021

Worksheet: C1

Worksheets are self-guided activities that reinforce lectures. They are not graded for accuracy, only for completion. Worksheets are due by Sunday night before the next lecture.

Questions

  1. Examine the program below and answer the following questions. You may assume the necessary headers are included already when checking for correctness.

    int correct_answer = 0x10;
    int student_answer = 10;
        
    //Check if student_answer is correct
    if (correct_answer == student_answer){
        printf("Correct Answer! :)");
    }
    else{
        printf("Wrong answer! :(");
    }
    

    Attempt to answer these questions without running the code!

    • What is the output of this program?
    • Explain why the student got either a correct or incorrect answer (as indicated by your answer to part a).

    Reveal Solution

  2. Examine the program below and answer the following questions. You may assume the necessary headers are included already when checking for correctness.

    int correct_answer = 02113;
    int student_answer = 2113;
    
    //Check if student_answer is correct
    if (correct_answer == student_answer){
        printf("Correct Answer! :)");
    }
    else{
        printf("Wrong answer! :(");
    }
    

    Attempt to answer these questions without running the code!

    • What is the output of this program?
    • Explain why the student got either a correct or incorrect answer (as indicated by your answer to part a).

    Reveal Solution

  3. For each of the statements, indicate if it evaluates to True? (Assume all needed headers are included).

    a) sizeof(int) == sizeof(int *)
    b) sizeof(char) == sizeof(char *)
    c) sizeof(short) == sizeof(short *)
    d) sizeof(long) == sizeof(long *)
    e) sizeof(char *) == sizeof(short *)
    

    Reveal Solution

  4. Examine the following program closely, and describe what happened when it is run. What output do you expect?

    unsigned int bigger_largest_uint = 4294967295 + 1; //largest_uint + 1
    int bigger_largest_int = 2147483647 + 1; //largest_int + 1
    
    //Before you run this, what do you expect the value to be
    if (4294967295 < bigger_largest_uint){
        printf("A");
    }
    else{
        printf("B");
    }
    
    if (2147483647 < bigger_largest_int){
        printf("C");
    }
    else{
        printf("D");
    }
    

    Reveal Solution

  5. Describe a situation in which the difference between x++ and ++x would make a significant difference in the operation of the program.

    Reveal Solution

  6. Why does the expression

    (3 & 4) && (1)
    

    differ from

    (3 && 4) && (1)
    

    Your answer should be specific to the output and the underlying reason why they differ.

    Reveal Solution

  7. What is the output for a, b, and c?

       int a = 1 << 1;
       int b = a << 1;
       int c = b << 2
    

    Can you find a closed-form formula for shifting bits, using the variables x, y, and z?

       int z = x << y
    

    Reveal Solution

  8. This program compiles, but does not work as expected. What should be changed to fix it?

    #include <stdio.h>
    int main(){
    int a = 10000000;
    short b = 10000000;
    if( a == b){
        printf("equal");
    }
    else{
        printf("not equal");
    }
    

    Reveal Solution

  9. Consider the following snippet of c code

    int a[] = {1,2,3,4};
    short b[] = {1,2,3,4};
    char c[] = {1,2,3,4};
    int * p = a;   
    

    a. What is the sizeof(a[0])?
    b. What is the sizeof(a)?
    c. What is the sizeof(b)?
    d. What is the sizeof(c)?
    e. What is the sizeof(p)?
    f. What is the sizeof(*p)?
    g. Explain the differences in the sizeof for each of these output?

    Reveal Solution

  10. Consider the following snippet of c code. Upload an image to the repo and/or include it in the Markdown file. (You can also do it in ASCII art, like in lecture notes).

    int a = 5;
    int b = a;
    int c[2];
    
    int *p1 = &a;
    int *p2 = c;
    *p2 = b;
    c[1] = a;
    //MARK 1
       
    *p1 = *p2;
    b=20;
    //MARK 2
       
    

    a. Draw the memory diagram at MARK 1
    b. Draw the memory diagram at MARK 2

    Reveal Solution

  11. For the following snippet of c code, draw a memory diagram for each step in the for loop at the MARK. (This requires drawing 4 memory diagrams) Upload an image to the repo and/or include it in the Markdown file. (You can also do it in ASCII art, like in lecture notes).

    #include <stdio.h>
    
    int foo[4] {4,7,3,9};
    int * bar = NULL;
    int * fubar = NULL;
        
    int main() {
        for (int i=0; i < 4; i++){
        if (i%2==0){
            foo[i]=foo[i]%2;
            bar = &(foo[i])
        }
        else{
            foo[i]=foo[i]*2;
            fubar = &(foo[i])
        }
        //MARK
        }
    }
    

    Reveal Solution

  12. Fill in the sum function so that it adds the elements in array.

    #include <stdio.h>
    
    int sum(<ARGUMENTS GO HERE!>){
        //Code goes here!
    }
    
    int main(){
        int array[] = {1,2,3,4,5,6,7,8,9,10};
        int s = sum(array);
        printf("%d\n",s);
    }
    

    Reveal Solution

  13. Using the following struct declaration, print out the f member of FOO. (Hint: It might be helpful to first figure out a value that you can then debug with!)

    #include <stdio.h>
    
    int main(){
    
        struct bar{
            float f;
            double d;
        };
    
        struct foo{
            int i;
            int j;
            struct bar * BAR;
        };
        
        struct foo * FOO;
    
        // ... some code that sets up foo!
        
        printf("f=%g", <FILL-IN-HERE> ); //finish this line.
    }
    

    Reveal Solution

  14. Using the following struct definition:

    typedef struct{
        short head;
        int left_arm;
        int * right_arm;
        char left_leg;
        long * right_leg;
    } frankenstein;
    
    frankenstein monster;
    
    
    //size of the monster's parts
    int head_length = sizeof(monster.head);
    int left_arm_length = sizeof(monster.left_arm);
    int right_arm_length = sizeof(monster.right_arm);
    int left_leg_length = sizeof(monster.left_leg);
    int right_leg_length = sizeof(monster.right_leg);
    int height = sizeof(monster);
    

    First, without running the code, write down the expected sizes of each of the int length variables above, including height.

    Second, write a program to reveal the actual sizes. The height may differ than what you expect, right? Why might you think that’s case. (We’ll go over the true reason in class)

    Reveal Solution