Week 05 Tutorial Sample Answers
-
The tutorial will start with a code review.
- New lab pairs - you have a new lab partner for weeks 5-7.
-
Write a function with this prototype
int array_length(int nums[])
which returns the number of elements in the arraynums
.You can not write such a function in C.It is not possible for a C function to determine the length of an array it has been passed.
Programmers usually do one of 3 things.
- Pass the array length as another parameter to the function.
- Use a special value in an array element to mark the finish of the array - e.g. 0 if the array need contain only positive ints
- Pass an array of a specific length to the function - e.g. always pass arrays of 20 elements
For functions you write in this course, you should opt for option (a).
-
Write a function with this prototype
int test_all_initialized(int nums[])
which returns 1 if all elements of arraynums
are initialized, otherwise returns 0.You can not write such a function in C.It is not possible at runtime in C to determine if a variable has been initialized
C programmers must take care to ensure that all variables, including array elements, are appropriately initialized.
-
Write a function that takes in a 2d array of ints and multiplies every value in the array by a given int.
It will have this prototype:void scalar_multiply(int rows, int columns, int matrix[rows][columns], int scalar)
void scalar_multiply(int rows, int columns, int matrix[rows][columns], int scalar){ int i = 0; while (i < rows) { int j = 0; while (j < columns) { matrix[i][j] = matrix[i][j] * scalar; j++; } i++; } }
-
What will happen when each of the following statements is executed (in order)?
int n = 42; int *p, *q; p = &n; *p = 5; *q = 17; q = p; *q = 8;
Filling out the follwing table may be helpful:
Address int n = 42;
int *p;
int *q;
p = &n;
*p = 5;
*q = 17;
q = p;
*q = 8;
0xFF80
0xFF84
0xFF88
0xFF8C
0xFF90
Address int n = 42;
int *p;
int *q;
p = &n
*p = 5;
*q = 17;
q = p;
*q = 8;
0xFF80
0xFF84
0xFF88
0xFF8C
0xFF90
-
This C code:
int x; int a[6]; x = 10; a[3 * 2] = 2 * 3 * 7; printf("%d\n", x);
mysteriously printed42
. How could this happen whenx
is clearly assigned only the value10
?How does the output change if we create a another int after the array?
How can you easily detect such errors before they have mysterious effects?
The C code assigns to42
toa[6]
an array element that does not exist.The result of this is undefined.
In this case the value
42
has apparently been assigned to the variablex
. Silently changing another variable is a common consequence of use of an illegal array index. Subtler and more confusing behaviour are also quite possible.If you are using a CSE machine you can compile the program with
dcc
.It produces a program which will stop immediately with a clear error message if an invalid array index is used.
-
Write a void function which places the sum of two numbers inside a variable passed in by reference.
It should have the following prototype:
void sum_nums(int a, int b, int *sum);
void sum_nums(int a, int b, int *sum) { *sum = a + b; }
Your tutor has asked a lab pair to present their week 4 work.
Discuss the good, the bad and the ugly aspects of their code.
Please be gentle in any criticism - we are all learning!