Week 05 Tutorial Questions
-
The tutorial will start with a code review.
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!
-
Assignment 1 (Slide) -- how is your class going? Are there any questions, or lessons learned from the first week of it being out?
-
Which of the following functions would be possible to write? If they are not possible to write, discuss what you could to to make them work.
-
int array_length(int nums[]);
which returns the number of elements in the array
nums
. -
int test_all_positive(int nums[]);
which returns 1 if all elements of array
nums
are positive, otherwise returns 0. -
int test_all_initialized(int length, int nums[]);
which returns 1 if all elements of array
nums
are initialized, otherwise returns 0. -
int test_all_positive(int length, int nums[]);
which returns 1 if all elements of array
nums
are positive, otherwise returns 0.
-
-
What is a 2D array?
-
The code
square.c
prints a 7×7 grid of 1's#include <stdio.h> #define SIZE 7 int main(void) { int row, column; row = 0; while (row < SIZE) { column = 0; while (column < SIZE) { printf("1"); column = column + 1; } printf("\n"); row = row + 1; } return 0; }
How would you change it so that it instead filled a 2D array with 1's and printed it out? What are the benefits of storing the values of the grid in a 2D array?
-
Write a function that takes in a 2D array of
int
s and multiplies every value in the array by a givenint
.It will have this prototype:
void scalar_multiply(int rows, int columns, int matrix[rows][columns], int scalar);
-
What is a pointer? How can you declare and initialise a pointer?
-
Why did we have to always include the
&
symbol in our arguments given toscanf
? -
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
-
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);
-
Write an int *function which returns the address of the greater of two integers passed by reference.
It should have the following prototype:
int *max(int *a, int *b);
-
This program has been left incomplete:
void pointer_info(int a, int ptr) { scanf("%d", a); scanf("%d", ptr); if (a != ptr) { printf("a and ptr are different: a == %d and ptr == %d\n", a, ptr); } }
Fill in the blanks such that you have a C program that compiles.
-
This C code:
int x; int a[6]; x = 10; a[3 * 2] = 2 * 3 * 7; printf("%d\n", x);
mysteriously printed
42
. 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 C program
occur.c
which reads 6 numbers then reads another number and prints how many times that number occurred in the first 6.For example:
./occur Enter 6 numbers: 1 3 1 3 1 9 Enter a number: 1 1 occurred 3 times in the 6 numbers read
Make sure you make you make it very easy to change how many numbers the program reads.
-
Write a C program which reads numbers until end-of-input is reached and then prints the middle number.
So if 9 numbers were entered the program should print the 5th number entered.
If 27 numbers were entered the program should print the 14th number entered.
If an even number of numbers is entered there are two middle numbers, print the second of them.
For example if 8 numbers are entered, prints the 5th.
You can assume at most 10,000 numbers will be entered.
-
Write a C function that takes in an array of integers, and returns the average.
double average(int numbers[], int num_elements);
-
Consider:
double ff[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6}; double *fp = &ff[0];
What are the similarities between
ff
andfp
? What are the differences? -
What would be the output of the following code?
int x = -9; int *p1 = &x; int *p2; p2 = p1; printf("%d\n", *p2); *p2 = 10; printf("%d\n",x);
Code Review
Assignment 1 - Slide
Valid Functions
2D Arrays
Pointers
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.