Week 01 Tutorial Questions
Getting Started and Recap!
-
Introductions
- Introduce yourself to the other people in the tutorial class
- Discuss what you learned from COMP1511
- Discuss what parts you're still unsure of
New C Syntax
-
Convert the following while loop to a for loop:
int i = 10; while (i >= 0) { printf("%d\n", i); i--; }
-
What
while
loop is equivalent to the followingfor
statement:int ch; for (ch = getchar(); ch != EOF; ch = getchar()) { putchar(ch); }
-
Consider a function to check whether the elements in an array occur in ascending order. Duplicates are allowed in the array, as long as they are adjacent.
// Input: // - a is a valid pointer to the start of an array of ints // - n is a positive int indicating how many elements in a[] // Output: // - returns true if a[i] <= a[i + 1] for all i in [0..n - 2] bool isSorted(int *a, int n) { ... }
Implement this function in two styles:
- using COMP1511 C Style
- using for, break/return to exit the loop early
-
Describe the difference in behaviour (and in the final result) of the following two
switch
statements:// S1 switch (ch) { case 'a': printf("eh? "); break; case 'e': printf("eee "); break; case 'i': printf("eye "); break; case 'o': printf("ohh "); break; case 'u': printf("you "); break; } printf("\n");
// S2 switch (ch) { case 'a': printf("eh? "); case 'e': printf("eee "); case 'i': printf("eye "); case 'o': printf("ohh "); case 'u': printf("you "); } printf("\n");
What will be printed for each of the following values for
ch
:'u'
,'o'
,'e'
? -
Consider the following function to convert a number in the range 0..6 into a weekday name. 0 returns "Sun", 1 returns "Mon", 2 returns "Tue", and so on.
char *numToDay(int n) { assert(0 <= n && n <= 6); char *day; if (n == 0) { day = "Sun"; } else if (n == 1) { day = "Mon"; } else if (n == 2) { day = "Tue"; } else if (n == 3) { day = "Wed"; } else if (n == 4) { day = "Thu"; } else if (n == 5) { day = "Fri"; } else if (n == 6) { day = "Sat"; } return day; }
Suggest at least two alternative and more concise ways of achieving the same result. Include the
assert(...)
in each case.For each function, including the one above, explain what would happen if the
assert(...)
was removed and the function was invoked vianumToDay(7)
. -
Give an
if
statement equivalent to the followingswitch
statement:assert(islower(ch)); switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': printf("vowel"); break; default: printf("consonant"); break; }
-
Use a conditional expression to replace the if in the following code:
int ch = getchar(); char *type; if (isdigit(ch)) { type = "digit"; } else { type = "non-digit"; } printf("'%c' is a %s\n", ch, type);
-
Consider the following definitions:
typedef int Integer; struct point { int x; int y; }; typedef struct point Point; struct node { int value; struct node *next; }; typedef struct node *Node;
Explain the meaning of each of the
typedef
s.
Linked List Revision
-
Consider the following two list representations:
// Representation 1 struct node { int value; struct node *next; }; typedef struct node *List;
// Representation 2 struct node { int value; struct node *next; }; struct list { struct node *head; }; typedef struct list *List;
- Compare the two representations diagramatically.
- How is an empty list represented in each case?
- Suppose we want to write a function that inserts a number into a list at a given position. What would the function prototype look like for each representation?
- What are the advantages of having a separate list struct as in Representation 2?
-
Consider the following simple linked-list representation and a function that sums the values in the list:
typedef struct node { int value; struct node *next; } Node; typedef Node *List; // pointer to first node
Write a function to sum the values in the list. Implement it first using
while
and then usingfor
. Finally, implement it using recursion (can leave this for next week). -
Implement a function to delete the first instance of a value from a list, if it exists. Use the following list representation and prototype:
struct node { int value; struct node *next; }; struct list { struct node *head; }; void listDelete(struct list *l, int value);
C Revision
-
Consider a program called
myprog
which is invoked as:./myprog hello there, 'John Shepherd' > myFile
- What are the values of
argc
andargv
? - Where would the statement
printf("%s", argv[1]);
place its output? - Where would the statement
ch = getchar();
get its input?
- What are the values of
-
Consider the following two sets of definitions and statements:
int x, y; char *c, *d, *e, *f; y = 2; x = y; d = "abc"; c = d; e = "xyz"; f = "xyz"; x++; *c = 'A';
- Show the state of the memory after the first set of assignments.
- Would anything be different if we had done
c = "abc"; d = c;
? - Show the state of memory after
x++;
. - What happens when
*c = 'A';
is executed? Why?
Before starting this week's lab, make sure you properly understand the following topics:
- Structs (see videos on structs)
- Pointers (see videos on pointers)
- Malloc (see videos on malloc)
- Linked Lists (see videos on linked lists)