WEEK 8 Class Activities Lecturer’s slides.

Slides:



Advertisements
Similar presentations
WEEK 12 Class Activities Lecturer’s slides.
Advertisements

CS1010 Programming Methodology
WEEK 5 Class Activities Lecturer’s slides.
CS1010 Programming Methodology
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
UNIT 12 UNIX I/O Redirection.
CS1010 Programming Methodology
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference.
CS1010 Programming Methodology
WEEK 9 Class Activities Lecturer’s slides.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
CS1010: Programming Methodology
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Computer Science 210 Computer Organization Arrays.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
WEEK 4 Class Activities Lecturer’s slides.
UNIT 14 Functions with Pointer Parameters.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
WEEK 6 Class Activities Lecturer’s slides.
Week 13 Structures Aaron Tan. Q1 What is the output? 2 #include typedef struct { int p; float q; } one_t; typedef struct { int p; float q; } two_t; int.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
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.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
UNIT 11 Random Numbers.
Computer Programming for Engineers
CS1020 Data Structures and Algorithms I Lecture Note #2 Arrays.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
UNIT 8 Pointers.
UNIT 9 Arrays.
CS1010: Programming Methodology
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
WEEK 10 Class Activities Lecturer’s slides.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Week 12 Class Activities.
1-d Arrays.
CS1010 Programming Methodology
Computer Science 210 Computer Organization
Functions and Pointers
CS1010 Programming Methodology
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
CS1010 Programming Methodology
CS1010 Discussion Group 11 Week 9 – Pointers.
CS1010 Programming Methodology
CS1010 Programming Methodology
CS1010 Programming Methodology
CS1010 Programming Methodology
Pointers.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Pointers.
Functions and Pointers
Computer Science 210 Computer Organization
Looping.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions.
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

WEEK 8 Class Activities Lecturer’s slides

Week 8 CS1010 (AY2014/5 Semester 1)Week8 - 2© NUS  Unit #14: Functions with Pointer Parameters  Exercise #1: Tracing  Exercise #2: Complete a Program  Exercise #3: Volume and Surface Area  Exercise #4: Triangle Centroid  Unit #15: File Processing  Exercise #5: Reverse Array  Exercise #6: Trimming Blanks

Week 8 Programs CS1010 (AY2014/5 Semester 1)Week8 - 3© NUS  Download the programs from this web page   The files are:  array.in  trimblanks.in  Week8_MaxAve_Incomplete.c  Week8_ReverseArray.c  Week8_Trace.c  Week8_TrimBlanks.c  You may also copy the above files directly into your sunfire account using the following UNIX command, where xxx is the name of one of the above files: cp ~cs1010/public_html/lect/prog/2014/week8_for_students/xxx.

Exercise #1: Tracing CS1010 (AY2014/5 Semester 1)Week8 - 4© NUS  Trace the program Week8_Trace.c manually. What are the outputs? #include void f(int, double, int *, double *); int main(void) { int a = 5; double b = 7.1; int c = 12; double d = 22.3; printf("a = %d, b = %f, c = %d, d = %f\n", a, b, c, d); printf("&a = %p, &b = %p\n", &a, &b); f(c, d, &a, &b); printf("After returning from function f:\n"); printf("a = %d, b = %f, c = %d, d = %f\n", a, b, c, d); return 0; } #include void f(int, double, int *, double *); int main(void) { int a = 5; double b = 7.1; int c = 12; double d = 22.3; printf("a = %d, b = %f, c = %d, d = %f\n", a, b, c, d); printf("&a = %p, &b = %p\n", &a, &b); f(c, d, &a, &b); printf("After returning from function f:\n"); printf("a = %d, b = %f, c = %d, d = %f\n", a, b, c, d); return 0; } void f(int w, double x, int *y, double *z) { printf("w = %d, x = %f, y = %p, z = %p\n", w, x, y, z); w = 2 * w; x = 3 * x; *y = *y * 4; *z = 5 * *z; } void f(int w, double x, int *y, double *z) { printf("w = %d, x = %f, y = %p, z = %p\n", w, x, y, z); w = 2 * w; x = 3 * x; *y = *y * 4; *z = 5 * *z; } a = 5, b = , c = 12, d = &a = ffbff74c, &b = ffbff740 w = 12, x = , y = ffbff74c, z = ffbff740 After returning from function f: a = 20, b = , c = 12, d = a = 5, b = , c = 12, d = &a = ffbff74c, &b = ffbff740 w = 12, x = , y = ffbff74c, z = ffbff740 After returning from function f: a = 20, b = , c = 12, d =

Exercise #2: Complete a Program (1/2) CS1010 (AY2014/5 Semester 1)Week8 - 5© NUS  Complete this Unit14_MaxAve_Incomplete.c program that computes the maximum and average of 3 integers in a single function. max_and_average(int, int, int, int main(void) { int num1, num2, num3; printf("Enter 3 integers: "); scanf("%d %d %d", &num1, &num2, &num3); printf("Maximum = %d\n", ); printf("Average = %.2f\n", ); return 0; } Week8_MaxAve_Incomplete.c int max; float ave; int max; float ave; max_and_average(num1, num2, num3, &max, &ave); void int *, float *); max ave

Exercise #2: Complete a Program (2/2) CS1010 (AY2014/5 Semester 1)Week8 - 6© NUS  Complete this Unit14_MaxAve_Incomplete.c program that computes the maximum and average of 3 integers in a single function. max_and_average(int n1, int n2, int n3, { } Week8_MaxAve_Incomplete.c void int *maxPtr, float *avePtr) *maxPtr = n1; if (n2 > *maxPtr) *maxPtr = n2; if (n3 > *maxPtr) *maxPtr = n3; *avePtr = (n1+n2+n3)/3.0; *maxPtr = n1; if (n2 > *maxPtr) *maxPtr = n2; if (n3 > *maxPtr) *maxPtr = n3; *avePtr = (n1+n2+n3)/3.0; (continued…)

Exercise #3: Volume, Surface Area (1/2) CS1010 (AY2014/5 Semester 1)Week8 - 7© NUS  Write a program to read the length, width and depth (all integers) of a cuboid and compute (1) its volume, and (2) its surface area. length width depth  You are to write 2 versions and compare them:  Week8_Cuboid_v1.c: Include 2 functions volume(…) and surface_area(…) to compute the volume and surface area of the cuboid separately.  Week8_Cuboid_v2.c: Include a single function volume_and_surface_area(…) to compute both the volume and surface area of the cuboid.  There should be no printf() statement in your functions (apart from the main() function).

Exercise #3: Volume, Surface Area (2/2) CS1010 (AY2014/5 Semester 1)Week8 - 8© NUS  Sample runs Enter length, width and depth: Volume = 180 Surface area = 216 Enter length, width and depth: Volume = 2520 Surface area = 1116

Exercise #4: Triangle Centroid (1/2) CS1010 (AY2014/5 Semester 1)Week8 - 9© NUS  In a triangle, a median is a line that connects a vertex to the midpoint of its opposite side. (eg: blue dotted lines)  The intersection of the 3 medians is called the centroid. (eg: point G) P (x 1, y 1 ) Q (x 2, y 2 ) R (x 3, y 3 ) G  Write a program Week8_Centroid.c to read in the coordinates (of type float) of 3 vertices of a triangle and compute the coordinates of its centroid.  Your program should have a function centroid(…).  There should be no printf() statement in this centroid() function.  If you can’t complete in class, continue to do it at home.  This exercise is mounted on CodeCrunch.

Exercise #4: Triangle Centroid (2/2) CS1010 (AY2014/5 Semester 1)Week8 - 10© NUS  Sample runs Coordinates of 1st vertex: 0 0 Coordinates of 2nd vertex: 0 1 Coordinates of 3rd vertex: 1 1 Coordinates of centroid = (0.33, 0.67) Coordinates of 1st vertex: Coordinates of 2nd vertex: Coordinates of 3rd vertex: Coordinates of centroid = (-4.37, 12.07)

Exercise #5: Reverse Array (1/3) CS1010 (AY2014/5 Semester 1)Week8 - 11© NUS  Given the program Week8_ReverseArray.c to read values into an integer array, reverse the array, and print the array after reversal.  Modify the program such that it reads from a text file “array.in” and writes to a text file “array.out”

Exercise #5: Reverse Array (2/3) CS1010 (AY2014/5 Semester 1)Week8 - 12© NUS #include #define MAX_SIZE 10 int scanArray(int []); void printArray(int [], int); void reverseArray(int [], int); int main(void) { int array[MAX_SIZE], size; size = scanArray(array); reverseArray(array, size); printf("After reversing: "); printArray(array, size); return 0; } #include #define MAX_SIZE 10 int scanArray(int []); void printArray(int [], int); void reverseArray(int [], int); int main(void) { int array[MAX_SIZE], size; size = scanArray(array); reverseArray(array, size); printf("After reversing: "); printArray(array, size); return 0; } Week8_ReverseArray.c // Reverse the array void reverseArray(int arr[], int size) { int i, temp; for (i=0; i<size/2; i++) { temp = arr[i]; arr[i] = arr[size-i-1]; arr[size-i-1] = temp; } // Reverse the array void reverseArray(int arr[], int size) { int i, temp; for (i=0; i<size/2; i++) { temp = arr[i]; arr[i] = arr[size-i-1]; arr[size-i-1] = temp; }

Exercise #5: Reverse Array (3/3) CS1010 (AY2014/5 Semester 1)Week8 - 13© NUS // Read elements into array and // return number of elements read. int scanArray(int arr[]) { int size, i; printf("Enter size of array (<=%d): ", MAX_SIZE); scanf("%d", &size); for (i=0; i<size; i++) { scanf("%d", &arr[i]); } return size; } // Read elements into array and // return number of elements read. int scanArray(int arr[]) { int size, i; printf("Enter size of array (<=%d): ", MAX_SIZE); scanf("%d", &size); for (i=0; i<size; i++) { scanf("%d", &arr[i]); } return size; } Week8_ReverseArray.c // Print array void printArray(int arr[], int size) { int i; for (i=0; i<size; i++) { printf("%d ", arr[i]); } printf("\n"); } // Print array void printArray(int arr[], int size) { int i; for (i=0; i<size; i++) { printf("%d ", arr[i]); } printf("\n"); }

Exercise #6: Trimming Blanks CS1010 (AY2014/5 Semester 1)Week8 - 14© NUS  Write a program TrimBlanks.c that contains a function int trimBlanks(char infile[], char outfile[]) that takes an input text file and produces a new text file that is a duplicate copy of the input file except that each sequence of consecutive blank characters is replaced by a single blank character.  The function returns -1 if there is an error; otherwise, it returns the number of blank characters trimmed.  An incomplete program Week8_TrimBlanks.c is given. A test input file trimblanks.in is also given.

Things-To-Do CS1010 (AY2014/5 Semester 1)Week Mid-Semester Test 11 October 2014, Saturday, 9:30 am SR1 and SR3 Check CS1010 “Term Tests” web page Continue to do practice exercises on CodeCrunch © NUS

End of File CS1010 (AY2014/5 Semester 1)Week8 - 16© NUS