Week 04 Tutorial Questions

    Code Review

  1. The tutorial will start with a code review.

    A code review is where we discuss a piece of code: the good, the bad and the ugly. Remember in a code review:

    • The class and tutor (the reviewers) give feedback, ask questions, make suggestions. You tutor will show you how to do this at first but then will expect the other reviewers to take over.
    • Reviewers are NOT negative, a review is to be supportive and constructive and helpful to the reviewees.
    • The reviewees should speak very little, just give a brief overview of the code they want reviewed. This is because good code speaks for itself. The writer shouldn't have to explain it.
    • Let everyone have a turn to speak, don’t dominate the conversation.
    • Contribute and participate, don’t be silent. If you don’t understand anything then that doesn’t mean be silent - it means ASK for an explanation. By asking you are helping the coders to see how to be clearer.

    Some examples of possible comments in a code review:

    • The indentation and whitespace is incorrect so it makes it hard to read.
    • I don't understand what this if statement is doing. Can you please explain it?
    • Wouldn't a variable name like sum be better than n here?

    Code reviews are very commonplace in the industry and so, give it a try with the two following pieces of code!

  2. The program below determines if a student is in high school given their current year. What are the good qualities of the program? What could be improved?

    #include <stdio.h>
      int main(void) {
      int a = 7; int b = 12; int c;
      printf("What year are you in? ");
      int c = 20;
      scanf("%d", &c);
          if (c > a || c == a){
          if (c < b || c == b){
          printf("You are in high school\n");
          if ( c < 7 || c > 12) {
          printf("You are not in high school\n");
          return 0;
  3. What are the good qualities of the program? How could this program be improved?

    #include <stdio.h>
    int main(void) {
        // Naming variables
        int num1;   // num1 is the first number the user types in
        int num2;   // num2 is the second number the user types in
        int sum;    // sum is the result of adding the first and second number
        // Getting the numbers from the user
        scanf("%d", &num1);
        scanf("%d", &num2);
        // Adding the two numbers together
        sum = num1 + num2;
        // Tell the user the result
        printf("Your numbers add up to %d\n", sum);
        // Check if its divisible by 2
        if (sum %2 != 0) {
        if(num1 %2 == 0) {
        printf("The first number you've typed was even and the second number was odd\n");
        } else if (num1 %2 != 0) {
        printf("The first number you've typed was odd and the second number was even\n");
        } else if (sum %2 == 0) {
        if(num1 %2 == 0) {
        printf("Both the numbers you've typed were even\n"); 
        } else if (num1 %2 != 0) {
        printf("Both the numbers you've typed were odd\n");
        return 0;
  4. Functions

  5. What is a function?

  6. Where have we seen functions already?

  7. Why would we use them? Can't we just write all our code in the main function? (Hint: Imagine having to implement printf() yourself everytime you want to use it)

  8. Here is an example of a function, what are the key sections of it?

    int get_larger(int first_num, int second_num) {
        int larger = first_num;
        if (second_num > first_num) {
            larger = second_num;
        return larger;
  9. In what case do we not need a return in a function? Why would we want a function without a return?

  10. Create a function called is_even which takes in an integer and returns 1 if it is an even number, and 0 if it is not.

  11. Here is a program change_number.c, what will this code print before and after the function is called?

    #include <stdio.h>
    void change_number(int num);
    int main(void) {
        int num;
        printf("Enter a number: ");
        scanf("%d", &num);
        printf("Before function: %d\n", num);
        printf("After function: %d\n", num);
        return 0;
    // Change the given variable "num" to be the value 10
    void change_number(int num) {
        num = 10;
  12. Arrays

  13. What is an array?

  14. Create an array called zeros of a size 7 and initialise all of the elements to 0.

  15. How do we print out all these elements to ensure they are all 0?

  16. How would we do the same thing but this time have the elements initialised to 1?

  17. If an array is declared as int numbers[20]; and your program assigns a value to each element in the array, what is the problem with the statement x = numbers[20];?

  18. Write a function array_print(int length, int arr[]) that prints all the contents in an array, separated by new lines. Why do we need a length parameter?

  19. Here is a program change_element.c, what will this code print before and after the function is called? Is this output expected?

    #include <stdio.h>
    #define SIZE 1000
    void change_array_element(int arr[SIZE], int index);
    int main(void) {
        int array[SIZE] = {0};
        int index;
        printf("Enter index to change: ");
        scanf("%d", &index);
        printf("Before function: %d\n", array[index]);
        change_array_element(array, index);
        printf("After function: %d\n", array[index]);
    // Change an element of an array at a given index to
    // now have 42
    void change_array_element(int arr[SIZE], int index) {
        arr[index] = 42;
  20. Write a C code function to store, in each element of this array, the square of the index of that element, e.g., squares[5] would contain the value 25.

  21. Using scanf() in a loop

    scanf is a function that can have a result. This result will usually be the number of values it has read and assigned to variables.

    For example:

    int result = scanf("%d", &variable);

    The variable result will have the value 1 if one integer was read from the user. We call input from the user "standard input".

  22. What would scanf() return in each of these cases?

    Scanf // Input 13 20.5
    apple 22 21 37.5 42 56 hello hello 56
    scanf("%d %d");
  23. Discuss how we could create a loop that would continually read values in from standard input until the user inputs anything other than a number (or the input ends).

    This question will be continued in one of the Lab exercises this week.

  24. Write a program, character_loop.c that scans in characters until CTRL + D is pressed, at which point, the program ends. The program should print out the character if it is a letter, e.g. 'a' to 'z' including upper/lowercase.

    An example of the program is below.

    'a' scanned in!
    'z' scanned in!
    'A' scanned in!
    'Z' scanned in!
    'B' scanned in!
    'M' scanned in!
  25. CSE Valley

    CSE Valley is your first assignment this term! The theme of the assignment is a farming game. Have fun and if you have any questions about the assignment, feel free to ask in your lab, on the forum and in help sessions!

    The assignment specification is found on the top bar - just click the button that says "Assignment 1"!

  26. Have you watched Sasha’s lecture detailing the assignment?

  27. Have you downloaded the starter code and have it ready to work on in your coding environment?