1 / 14

CS1010 Week 5: Pointers & Arrays Class Activities

Explore tracing pointers, choose correct codes, exercise on reversing arrays, missing digits, modularizing code, and set containment. Practice incrementing pointers and writing programs to enhance understanding.

jcarr
Download Presentation

CS1010 Week 5: Pointers & Arrays Class Activities

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. CS1010 (AY2014/5 Semester 1) Week 5: 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

  3. CS1010 (AY2014/5 Semester 1) Tracing Pointers (1/2) • Trace the code below manually to obtain the outputs. • Compare your outputs with your neighbours. Week5_TracePointers.c 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);

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

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

  6. CS1010 (AY2014/5 Semester 1) Incrementing a Pointer • If p is a pointer variable, what does it mean by p = p + 1 (or p++)? Unit 3 Exercise #1: int takes up 4 bytes float takes up 4 bytes char takes up 1 byte double takes up 8 bytes Week5_IncrementPointers.c inta, *ap; floatb, *bp; charc, *cp; doubled, *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); ffbff62c ffbff628 ffbff627 ffbff618 ffbff630 ffbff62c ffbff628 ffbff620 ffbff63c

  7. CS1010 (AY2014/5 Semester 1) Exercise #1: Reversing an Array • Write a program Week5_ReverseArray.cto 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.cis 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. CS1010 (AY2014/5 Semester 1) Exercise #2: Missing Digits (1/3) • Write a program Week5_MissingDigits.cto 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. CS1010 (AY2014/5 Semester 1) Exercise #2: Missing Digits (2/3) • Where does the array come in? • Hint…  (Let you THINK first before giving out the hint) Input: 73105 true 0 9 1 6 2 3 4 7 5 8 false false false false false false false false false false 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? true true true true

  10. CS1010 (AY2014/5 Semester 1) 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); 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"); return0; } Key idea Week5_MissingDigits.c

  11. CS1010 (AY2014/5 Semester 1) Exercise #3: Modularising Exercise #2 • Let’s re-write our program Week5_MissingDigits.cinto 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. CS1010 (AY2014/5 Semester 1) Exercise #4: Set Containment • Consider two arrays, arrA and arrB, of int values, where their sizes are sizeA and sizeBrespectively. • Write a functionint isSubset(int arrA[], int sizeA, int arrB[], int sizeB) to determine if the set arrAis 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.cis given. Complete the program. This is your take-home exercise. Also mounted on CodeCrunch

  13. CS1010 (AY2014/5 Semester 1) Things-To-Do • 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

  14. CS1010 (AY2014/5 Semester 1) End of File

More Related