WEEK 12 Class Activities Lecturer’s slides.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.
Introduction to C Programming
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Introduction to Programming
1.
Chapter 7: Arrays In this chapter, you will learn about
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
CS1010: Programming Methodology
Chapter 17 Linked Lists.
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
CS1010: Programming Methodology
1 Chapter Eleven Arrays. 2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3);
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Backup Slides. An Example of Hash Function Implementation struct MyStruct { string str; string item; };
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays.
Arrays.
C Language.
Addition 1’s to 20.
Week 1.
Pointers and Arrays Chapter 12
UNIT 15 File Processing.
CS1010 Programming Methodology
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Chapter 13 – Introduction to Classes
WEEK 13 Class Activities Lecturer’s slides.
UNIT 16 (Extra) Characters and Strings.
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
WEEK 5 Class Activities Lecturer’s slides.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
1 Various Methods of Populating Arrays Randomly generated integers.
CS1010 Programming Methodology
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
WEEK 9 Class Activities Lecturer’s slides.
CS1101: Programming Methodology Aaron Tan.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
WEEK 4 Class Activities Lecturer’s slides.
CS1101: Programming Methodology Aaron Tan.
Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations.
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.
CS1010: Programming Methodology
WEEK 10 Class Activities Lecturer’s slides.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Week 12 Class Activities.
CS1010 Programming Methodology
CS1010 Programming Methodology
CS1010 Programming Methodology
Presentation transcript:

WEEK 12 Class Activities Lecturer’s slides

Week 12: Structures CS1010 (AY2014/5 Semester 1)Week12 - 2© NUS  Unit #19: Sections  Exercise #1: Perimeter  Unit #19: Sections  Exercise #2: Points and Lines  Unit #19: Sections  Exercise #3: Health Screen

Week 12 Programs CS1010 (AY2014/5 Semester 1)Week12 - 3© NUS  Download the programs from this web page   The files are:  Week12_Perimeter.c  Week11_Points_Complete.c  Week12_Points.c  Week12_Health_Screen.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/week12_for_students/xxx.

Unit #19: Sections 1 – 4 CS1010 (AY2014/5 Semester 1)Week12 - 4© NUS 1.Organizing Data 2.Structure Types 3.Structure Variables 4.Assigning Structures

Exercise #1: Perimeter (1/2) CS1010 (AY2014/5 Semester 1)Week12 - 5© NUS  Write a program Week12_Perimeter.c to do the following: 1.Define a structure type rectangle_t with 2 integer members: side1 and side2, which are the lengths of its 2 sides. 2.Declare a variable of type rectangle_t and read values into its members. 3.Compute the minimum perimeter if we fold the rectangle into halves once, either along the x-axis or the y-axis.  A skeleton program is given  Do not use any additional variables besides the two given variables.  You may write the code in the main() function. You may modularise the program later.

Exercise #1: Perimeter (2/2) CS1010 (AY2014/5 Semester 1)Week12 - 6© NUS #include int main(void) { rectangle_t rect; int perimeter; printf("Enter lengths: "); printf("Perimeter = %d\n", perimeter); return 0; } Week12_Perimeter.c typedef struct { int side1, side2; } rectangle_t; scanf("%d %d", &rect.side1, &rect.side2); if (rect.side1 > rect.side2) perimeter = rect.side1 + 2*rect.side2; else perimeter = rect.side2 + 2*rect.side1 ;

Unit #19: Sections 5 – 6 CS1010 (AY2014/5 Semester 1)© NUS 5.Passing Structures to Functions 6.Array of Structures Week12 - 7

Exercise #2: Points and Lines (1/5) CS1010 (AY2014/5 Semester 1)© NUS  Week 11 Exercise #2:  You are given a list of points on a 2-dimensional plane, each point represented by its integer x- and y-coordinates. You are to sort the points in ascending order of their x-coordinates, and for those with the same x-coordinate, in ascending order of their y-coordinates.  Two arrays are used to store the points: array x for their x-coordinates, and array y for their y-coordinates. x[i] and y[i] refer to the point i.  You may assume that there are at most 20 points and no two points are the same.  The completed program for the above exercise, Week11_Points_Complete.c, is given. Study the program now.  Modify the program by replacing the arrays x and y with an array of structure. The partial program Week12_Points.c is given.  You are to do sort the points in class, and do the traceLines() function after class (if you cannot complete it in class). Week12 - 8

Exercise #2: Points and Lines (2/5) CS1010 (AY2014/5 Semester 1)© NUSWeek #include #define MAX_POINTS 20 typedef struct { int x, y; // x- and y-coordinates of a point } point_t; // Function prototypes omitted for brevity int main(void) { point_t points[MAX_POINTS]; int num_points; // number of points scanPoints(points, &num_points); sortPoints(points, num_points); printf("After sort:\n"); printPoints(points, num_points); return 0; } Week12_Points.c

Exercise #2: Points and Lines (3/5) CS1010 (AY2014/5 Semester 1)© NUSWeek // Sort the points in ascending order of x-coordinates and // then y-coordinates, using Selection Sort. void sortPoints(point_t pts[], int size) { int i, start, min_index; point_t temp; } Week12_Points.c for (start = 0; start < size-1; start++) { min_index = start; for (i = start+1; i < size; i++) // check if point at i is "less than" at min_index if ( lessThan(pts, i, min_index) ) min_index = i; // swap minimum element with element at start index temp = pts[start]; pts[start] = pts[min_index]; pts[min_index] = temp; }

Exercise #2: Points and Lines (4/5) CS1010 (AY2014/5 Semester 1)© NUSWeek // Returns 1 if point at index p is "less than" point // at index q; otherwise returns 0. // Point at index p is "less than" point at index q if // the former has a smaller x-coordinate, or if their // x-coordinates are the same, then the former has a // smaller y-coordinate. int lessThan(point_t pts[], int p, int q) { } Week12_Points.c return (pts[p].x < pts[q].x) || ((pts[p].x == pts[q].x) && (pts[p].y < pts[q].y));

Exercise #2: Points and Lines (5/5) CS1010 (AY2014/5 Semester 1)© NUS  [Do this part after class] After sorting the points, imagine that you trace the points in their order in the sorted array. Write a function traceLines() to compute the sum of the lengths of those lines that are horizontal or vertical.  For example, after sorting, here are the points: (1,2), (1,3), (2,1), (2,4), (3,2), (3,3), (3,4), (5,3), (5,6), (6,2), (6,5), (7,2), (10,4), (11,4), (12,2). The vertical and horizontal lines are marked in green (1,2) (1,3)  Sum of lengths of horizontal and vertical lines = = 13 Week

Unit #19: Sections 7 – 9 CS1010 (AY2014/5 Semester 1)© NUS 7.Passing Address of Structures to Functions 8.The Arrow Operator (->) 9.Returning Structure from Functions Week

Exercise #3: Health Screening (1/2) CS1010 (AY2014/5 Semester 1)© NUS  Write a program Week12_Health_Screen.c to read in a list of health screen readings  Each input line represents a reading consisting of 2 numbers: a float value indicating the health score, and an int value indicating the number of people with that score.  You may assume that there are at most 50 readings.  The input should end with the reading 0 0, or when 50 readings have been read.  As the readings are gathered from various clinics, there might be duplicate scores in the input. You are to determine how many unique scores there are.  A skeleton program Week12_Health_Screen.c is given.  This exercise is mounted on CodeCrunch. Week

Exercise #3: Health Screening (2/2) CS1010 (AY2014/5 Semester 1)© NUSWeek  A sample run is shown below Enter score and frequency (end with 0 0): Number of unique readings = 7  Possible extension: Which is the score that has the highest combined frequency? (Do this on your own.)

Things-To-Do CS1010 (AY2014/5 Semester 1) Lab #5 deadline this Saturday, 9am Keep an eye on the IVLE announcements on CA marks Participate in IVLE forums Try out past years’ exam papers No solutions provided Post on forums for clarification/discussion Next week’s lecture: From C to Java (preparation for CS1020) © NUSWeek

End of File CS1010 (AY2014/5 Semester 1)© NUSWeek