WEEK 5 Class Activities Lecturer’s slides.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
C Language.
And c++?. What will we do? C vs JAVA Dev-C++ tutorial and your first program C++ structure Reading input/output Flow Control (if, for, while) array function.
WEEK 12 Class Activities Lecturer’s slides.
UNIT 15 File Processing.
CS1010 Programming Methodology
WEEK 13 Class Activities Lecturer’s slides.
UNIT 16 (Extra) Characters and Strings.
What is shape function ? shape function is a function that will give the displacements inside an element if its displacement at all the node locations.
Computer Programming w/ Eng. Applications
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
CS1010 Programming Methodology
CS1010 Programming Methodology
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
UNIT 12 UNIX I/O Redirection.
CS1010 Programming Methodology
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
C Slides and captured lecture (video and sound) are available at:
CS1010 Programming Methodology
WEEK 9 Class Activities Lecturer’s slides.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CS1101: Programming Methodology
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology Aaron Tan.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
1 Lab Session-8 CSIT-121 Spring 2005 Call by Reference Lab Exercise for Demo Practice Problems.
CS1010: Programming Methodology
WEEK 4 Class Activities Lecturer’s slides.
CS1101: Programming Methodology Aaron Tan.
UNIT 14 Functions with Pointer Parameters.
1 ร. ศ. ดร. สุเทพ มาดารัศมี Understanding Pointers in C Chapter 10 of Programming with C Book.
CS1101: Programming Methodology
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Chapter 3 Input and Output
WEEK 6 Class Activities Lecturer’s slides.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
UNIT 10 Multidimensional Arrays.
WEEK 8 Class Activities Lecturer’s slides.
CS1101: Programming Methodology
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Fun fun ’til daddy takes your C turbo away! A C program is organized into one or more modules (source files created by a programmer), within which are.
UNIT 8 Pointers.
UNIT 9 Arrays.
CS1010: Programming Methodology
WEEK 10 Class Activities Lecturer’s slides.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Arrays. Arrays are objects that help us organize large amounts of information.
Week 12 Class Activities.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
CS1010 Programming Methodology
CS1010 Programming Methodology
CS1010 Discussion Group 11 Week 9 – Pointers.
CS1010 Programming Methodology
CS1010 Programming Methodology
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
INC 161 , CPE 100 Computer Programming
CSC 253 Lecture 8.
CSC 253 Lecture 8.
CS 2308 Exam I Review.
CSCE 206 Lab Structured Programming in C
Presentation transcript:

WEEK 5 Class Activities Lecturer’s slides

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

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

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: : :

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)

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

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: After reversing:

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: Missing digits in 73015:  Recall: How do we extract individual digits from an integer?

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 Input: true

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.

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)?

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

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

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