Week 05 Tutorial Questions
-
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
. -
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. -
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)
-
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
-
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?
-
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);
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!