0) { *p2 -= a; (*p1)++; } printf("2: %d %d %d\n", *p1, *p2, *p3); printf("3: %d %d %d\n", a, b, c); Week5_TracePointers.c"> 0) { *p2 -= a; (*p1)++; } printf("2: %d %d %d\n", *p1, *p2, *p3); printf("3: %d %d %d\n", a, b, c); Week5_TracePointers.c">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

WEEK 5 Class Activities Lecturer’s slides.

Similar presentations


Presentation on theme: "WEEK 5 Class Activities Lecturer’s slides."— Presentation transcript:

1 http://www.comp.nus.edu.sg/~cs1010/ WEEK 5 Class Activities Lecturer’s slides

2 Week 5: Pointers & Arrays  Tracing Pointers  Choose the Correct Codes  Incrementing a Pointer CS1010 (AY2014/5 Semester 1)Week5 - 2© NUS  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 Pointers Arrays

3 Tracing Pointers (1/2) CS1010 (AY2014/5 Semester 1)Week5 - 3© NUS  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; 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); Week5_TracePointers.c

4 Tracing Pointers (2/2) CS1010 (AY2014/5 Semester 1)Week5 - 4© NUS int a = 8, b = 15, c = 23; int *p1, *p2, *p3; 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); 8 a 15 b 23 c p1p2p3 1: 15 23 23 120 123 15 121 7 122 2: 123 -1 -1 3: 8 123 -1

5 Choose the Correct Codes CS1010 (AY2014/5 Semester 1)Week5 - 5© NUS  Pick the correct codes to read a value into the float variable var. float var; scanf("%f", var) (A) float var; scanf("%f", &var) (B) float var; float *p; p = &var; scanf("%f", p) (C) float var; float *p; p = &var; scanf("%f", &p) (D)

6 Incrementing a Pointer CS1010 (AY2014/5 Semester 1)Week5 - 6© NUS  If p is a pointer variable, what does it mean by p = p + 1 (or p++)? int a, *ap; float b, *bp; char c, *cp; double d, *dp; ap = &a; bp = &b; cp = &c; dp = &d; printf("%p %p %p %p\n", ap, bp, cp, dp); ap++; bp++; cp++; dp++; printf("%p %p %p %p\n", ap, bp, cp, dp); ap += 3; printf("%p\n", ap); Week5_IncrementPointers.c Unit 3 Exercise #1: int takes up 4 bytes float takes up 4 bytes char takes up 1 byte double takes up 8 bytes ffbff62c ffbff628 ffbff627 ffbff618 ffbff630 ffbff62c ffbff628 ffbff620 ffbff63c

7 Exercise #1: Reversing an Array CS1010 (AY2014/5 Semester 1)Week5 - 7© NUS  Write a program Week5_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 Week5_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

8 Exercise #2: Missing Digits (1/3) CS1010 (AY2014/5 Semester 1)Week5 - 8© NUS  Write a program Week5_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?

9 Exercise #2: Missing Digits (2/3) CS1010 (AY2014/5 Semester 1)Week5 - 9© NUS  Where does the array come in?  Hint… (Let you THINK first before giving out the hint) Create an array called found, with 10 elements, each element represents a digit. That is, found[0] is about digit 0, found[1] about digit 1, …, found[9] about digit 9. How do you use this array? false 0 1 2 3 4 5 6 7 8 9 Input: 73105 true

10 Exercise #2: Missing Digits (3/3) CS1010 (AY2014/5 Semester 1)Week5 - 10© NUS 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); 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; } Week5_MissingDigits.c Key idea Show this only after students have attempted it themselves.

11 Exercise #3: Modularising Exercise #2 CS1010 (AY2014/5 Semester 1)Week5 - 11© NUS  Let’s re-write our program Week5_MissingDigits.c into Week5_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)?

12 Exercise #4: Set Containment CS1010 (AY2014/5 Semester 1)Week5 - 12© NUS  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 Week5_SetContainment.c is given. Complete the program. This is your take-home exercise. Also mounted on CodeCrunch

13 Things-To-Do CS1010 (AY2014/5 Semester 1)Week5 - 13 Revise Chapter 6: Numeric Arrays Deadline for Lab #2 Deadline: 13 September 2014, Saturday, 9am Preparation for next week Multi-dimensional arrays Continue to do practice exercises on CodeCrunch © NUS

14 End of File CS1010 (AY2014/5 Semester 1)Week5 - 14© NUS


Download ppt "WEEK 5 Class Activities Lecturer’s slides."

Similar presentations


Ads by Google