Week 04 Tutorial Questions

    Code Reviews

  1. Tuts from now on will start with code reviews.

    This week your tutor will show you how it is to be done in future weeks.

    Your tutor will nominate a lab pair this week to do next week's code review.

    How a code review works:

    • The reviewees show their code on the screen, and briefly walk through how it works.
    • 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.
    • 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.
  2. Please ensure you set all your blogs that contain code to be viewable to course staff only.
  3. Slide

  4. Assignment 1 (Slide) has just been released. Where can we find the spec? When is it due?
  5. What's in the starting code? What needs to be implemented? Show the 1511 slide_reference command.
  6. Assignment 1 will be the first time in this course where you are assessed on style. What factors are considered for the style mark?
  7. Where can we get help with assignment 1?
  8. Style

  9. 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;
                  }
    
  10. Arrays

  11. What is an array?

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

  13. Write a function called firstPlusThird that sums the first and third element of an array called numbers and returns the answer.

  14. 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];?

  15. How would you declare a variable squares to be an array of integers with 15 elements?

  16. 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.

    Note that arrays are very special in that, if they are given as the input to a function, the function will have access to the array and can change it.

    We'll cover why this is and the difference between that and variables later!

  17. Using scanf() in a loop

  18. 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".

    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.

  19. Edit the code written in the previous question so that the integers scanned in are placed into an array called inputs of size 1000. You can assume no more than 1000 numbers will be entered.

  20. Functions

  21. What is a function?

  22. 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.

  23. Revision questions

    The remaining tutorial questions are primarily intended for revision - either this week or later in session.

    Your tutor may still choose to cover some of the questions time permitting.

  24. Write a program that repeatedly reads in integers until a non-integer is read in and then prints the number of integers read in.

    For example:

        ./read_ints
        Please enter some integers:
        10 -90 100 999 78hello
        You entered 5 integers
        ./read_ints
        Please enter some integers:
        1 2 3 4 5 6 7 8 9
        10 11 12 hello
        You entered 12 integers
        
  25. Write a program that reads in an integer n, and then prints a n×n multiplication table. For example:

        ./multiplication_table
        Enter multiplication table size: 5
        1|   1    2    3    4    5
        2|   2    4    6    8   10
        3|   3    6    9   12   15
        4|   4    8   12   16   20
        5|   5   10   15   20   25
        
  26. Write a function

        int non_decreasing(int n, int a[n])
        

    which checks whether items in an array are sorted in non-decreasing order. (i.e. a[i] ≥ a[i-1], for 0<i<N). Your function should returns 1 if the items are in non-decreasing order, 0 otherwise.

  27. Write a function

        int find_index(int x, int n, int a[n])
        

    which takes two integers x and n together with an array a[] of n integers and searches for the specified value within the array. Your function should return the smallest index k such that a[k] is equal to x (or -1 if x does not occur in the array).

  28. Write a function to calculate the Manhattan distance between two points.

    Use this function from the math.h library:

        double fabs(double x);
        
  29. Write a program median.c which reads integers until end-of-input is reached. It should then print the median (middle) of the integers. If there are an even number of integer you can print either of the two middle integers.

    Assume the numbers of integer is > 0 and < 1000.

    Assume the integer are entered in sorted (non-decreasing) order.

    Your program should behave like this:

        ./a.out
        1
        2
        4
        8
        16
        
        5 numbers read. Median was 4
        
  30. Modify the program from the previous question to check that the numbers of integers supplied is > 0 and < 1000, and to check they are in sorted (non-decreasing) order.

  31. Modify the program from the previous question to handle integers entered in any order, e.g.

        ./a.out
        16
        8
        2
        1
        4
        5 numbers read. Median was 4
        
  32. Write a function that takes an array of integers and the array length as arguments and performs the following:

    • Determines the number, say n (n <= len) of distinct integers in the array.
    • Modifies the array such that the first n elements are the distinct integers in the array - it does not matter what is in the rest of the array.

    Since the length of the array is variable you should not create additional arrays, nor assume a maximum array length. You may write extra functions in answering this question.

    Your function should have the following prototype:

        int distinct_nums(int size, int nums[size]);
        

    Running the function with the following input:

        int nums[] = {7,3,1,4,7,3,6,5,3};
        int num_distinct = distinct_nums(9, nums);
        

    Should return the value 6 and the first six elements of the array should be changed to: {7,3,1,4,6,5}

/import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-messy.html /import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-loop-scanf.html