Week 6 Class activities

Download Report

Transcript Week 6 Class activities

http://www.comp.nus.edu.sg/~cs1010/
WEEK 6
Class Activities
© NUS
CS1010 (AY2015/6 Semester 2)
Week6 - 2
Week 6: Pointers & Arrays
Pointers
 Tracing Pointers
 Choose the Correct Codes
 Incrementing a Pointer
Arrays




Exercise #1: Reversing an Array
Exercise #2: Missing Digits
Exercise #3: Modularising “Missing Digits” program
Exercise #4: Set Containment – Take home if time
runs out
© NUS
CS1010 (AY2015/6 Semester 2)
Tracing Pointers (1/2)
 Trace the code below manually to obtain the outputs.
 Compare your outputs with your neighbours.
int a = 8, b = 15, c = 23;
int *p1, *p2, *p3;
Week6_TracePointers.c
p1 = &b;
p2 = &c;
p3 = p2;
printf("1: %d %d %d\n", *p1, *p2, *p3);
*p1 *= a;
while (*p2 > 0) {
*p2 -= a;
(*p1)++;
}
printf("2: %d %d %d\n", *p1, *p2, *p3);
printf("3: %d %d %d\n", a, b, c);
Week6 - 3
© NUS
CS1010 (AY2015/6 Semester 2)
Tracing Pointers (2/2)
a
8
int a = 8, b = 15, c = 23;
int *p1, *p2, *p3;
Week6 - 4
p1
b
p2
15
120
121
122
123
p1 = &b;
p2 = &c;
p3 = p2;
printf("1: %d %d %d\n", *p1, *p2, *p3);
*p1 *= a;
while (*p2 > 0) {
*p2 -= a;
(*p1)++;
}
printf("2: %d %d %d\n", *p1, *p2, *p3);
printf("3: %d %d %d\n", a, b, c);
p3
c
23
15
7
-1
1: 15 23 23
2: 123 -1 -1
3: 8 123 -1
© NUS
CS1010 (AY2015/6 Semester 2)
Week6 - 5
Choose the Correct Codes
 Pick the correct codes to read a value into the float
variable var.
(A)
(B)
float var;
scanf("%f", var)
(C)
float var;
float *p;
p = &var;
scanf("%f", p)
float var;
scanf("%f", &var)
(D)
float var;
float *p;
p = &var;
scanf("%f", &p)
© NUS
CS1010 (AY2015/6 Semester 2)
Week6 - 6
Incrementing a Pointer
 If p is a pointer variable, what does it mean by p = p + 1
(or p++)?
Unit 3 Exercise #1:
int a, *ap;
int takes up 4 bytes
float b, *bp; float takes up 4 bytes
char takes up 1 byte
char c, *cp;
double d, *dp; double takes up 8 bytes
Week6_IncrementPointers.c
ap = &a; bp = &b; cp = &c; dp = &d;
printf("%p %p %p %p\n", ap, bp, cp,
ffbff62c ffbff628
ap++; bp++; cp++; dp++;
printf("%p %p %p %p\n", ap, bp, cp,
ffbff630 ffbff62c
ap += 3;
printf("%p\n", ap); ffbff63c
dp);
ffbff627 ffbff618
dp);
ffbff628 ffbff620
© NUS
CS1010 (AY2015/6 Semester 2)
Week6 - 7
Exercise #1: Reversing an Array
 Write a program Week6_ReverseArray.c to read a list of
numbers (at most 10 of them) into the array, reverse the
array and print its elements.
 We will write everything in the main() function for now.
 An incomplete program Week6_ReverseArray.c is given.
 Sample run:
Enter size of array (<=10): 5
Enter 5 elements: 1 -2 3 8 6
After reversing: 6 8 3 -2 1
© NUS
CS1010 (AY2015/6 Semester 2)
Week6 - 8
Exercise #2: Missing Digits (1/3)
 Write a program Week6_MissingDigits.c to read in a
positive integer and list out all the digits that do not
appear in the input number.
 We will write everything in the main() function. (You will
modularise it in exercise #3.)
 Sample run:
Enter a number: 73015
Missing digits in 73015: 2 4 6 8 9
 Recall: How do we extract individual digits from an
integer?
© NUS
CS1010 (AY2015/6 Semester 2)
Week6 - 9
Exercise #2: Missing Digits (2/3)
 Where does the array come in?
 Hint…  (Let you THINK first before giving out the hint)
Input: 73105
0
false
true
Create an array called found, with
10 elements, each element
represents a digit.
1
false
true
2
false
3
false
4
false
That is, found[0] is about digit 0,
found[1] about digit 1, …, found[9]
about digit 9.
5
false
6
false
7
false
8
false
How do you use this array?
9
false
true
true
true
© NUS
CS1010 (AY2015/6 Semester 2)
Week6 - 10
Exercise #2: Missing Digits (3/3)
Show this only after students
have attempted it themselves.
int main(void) {
int number, i;
int found[10] = {0} // found[i]=0 means digit i is missing
printf("Enter a number: ");
scanf("%d", &number);
printf("Missing digits in %d: ", number);
Key idea
while (number > 0) {
found[number%10] = 1; // found digit in input number
number /= 10;
}
for (i = 0; i < 10; i++) {
if (!found[i])
printf("%d ", i);
}
printf("\n");
return 0;
}
Week6_MissingDigits.c
© NUS
CS1010 (AY2015/6 Semester 2)
Week6 - 11
Exercise #3: Modularising Exercise #2
 Let’s re-write our program Week6_MissingDigits.c into
Week6_MissingDigitsModular.c
 Objective: Passing array to a function
 The program should contain a function called
analyseNumber() that takes in a number and analyse
what are the missing digits in that number
 What is/are the parameter(s)?
 The program should also contain a function called
printMissingDigits() to print out all the missing digits
 What is/are the parameter(s)?
© NUS
CS1010 (AY2015/6 Semester 2)
Week6 - 12
Exercise #4: Set Containment
 Consider two arrays, arrA and arrB, of int values, where their
sizes are sizeA and sizeB respectively.
 Write a function
int isSubset(int arrA[], int sizeA, int arrB[], int sizeB)
to determine if the set arrA is a subset of the set arrB.
 The function returns 1 if arrA is a subset of arrB, or 0 otherwise.
You may assume there are no duplicate numbers in each set.
 Example: If arrA[ ] = {14, 5, 1, 9} and arrB[ ] = {2, 9, 3, 14, 5, 6,
1}
 isSubset(arrA, 4, arrB, 7) returns 1
 isSubset(arrA, 4, arrB, 6) returns 0
 An incomplete program Week6_SetContainment.c is given.
Complete the program. This is your take-home exercise. Also
mounted on CodeCrunch
© NUS
CS1010 (AY2015/6 Semester 2)
Things-To-Do

Revise


PE1



This Wednesday!
Refer to CS1010 website “PE” page for details
Preparation for next week


Chapter 6: Numeric Arrays
Multi-dimensional arrays
Continue to do practice exercises on
CodeCrunch
Week6 - 13
© NUS
CS1010 (AY2015/6 Semester 2)
End of File
Week6 - 14