Week 12 Class Activities.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
C Language.
WEEK 12 Class Activities Lecturer’s slides.
CS1010 Programming Methodology
WEEK 13 Class Activities Lecturer’s slides.
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
WEEK 5 Class Activities Lecturer’s slides.
1 Various Methods of Populating Arrays Randomly generated integers.
CS1010 Programming Methodology
UNIT 12 UNIX I/O Redirection.
CS1010 Programming Methodology
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
1 CSE1301 Computer Programming: Revision 1. 2 Topics Type of questions What do you need to know? About the exam Exam technique Staff consultation Sample.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
WEEK 9 Class Activities Lecturer’s slides.
CS1010: Programming Methodology
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
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.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
CS1101: Programming Methodology Aaron Tan.
1 CSE1301 Computer Programming: Revision. 2 Topics Type of questions What do you need to know? About the exam Exam technique Staff consultation Revision.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
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 18 Searching and Sorting.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
WEEK 4 Class Activities Lecturer’s slides.
UNIT 14 Functions with Pointer Parameters.
XII CBSE Previous Year Question Paper QUESTION NO 3 (a) 3 Marks.
CSC 211 Data Structures Lecture 13
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
CS1101: Programming Methodology
WEEK 6 Class Activities Lecturer’s slides.
CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02.
CS1020 Data Structures and Algorithms I Lecture Note #14
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
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.
UNIT 11 Random Numbers.
WEEK 8 Class Activities Lecturer’s slides.
WEEK 1 Class Activities.
CS1020 Data Structures and Algorithms I Lecture Note #2 Arrays.
24 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
CS1010: Programming Methodology
WEEK 10 Class Activities Lecturer’s slides.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Common Elementary Algorithms Some of the basic but frequently used algorithms for manipulating arrays. These algorithms are so important that: a)Some programming.
WEEK 1 Class Activities.
CS1010 Programming Methodology
CS1010 Programming Methodology
CS1010 Programming Methodology
Introduction to Search Algorithms
CS1010 Discussion Group 11 Week 8 – Searching and Sorting.
CS1010 Programming Methodology
CS1010 Programming Methodology
CS1010 Programming Methodology
CS1010: Programming Methodology
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Presentation transcript:

Week 12 Class Activities

Week 12: Searching and Sorting CS1010 (AY2015/6 Semester 2)Week12 - 2© NUS  Unit #18: Searching (30 minutes)  Exercise #1: Compatible Teammate (20 min.)  Unit #18: Sorting (45 minutes)  Exercise #2: Points and Lines (25 minutes)  Exercise #3: Module Sorting (25 minutes)

Week 12 Programs CS1010 (AY2015/6 Semester 2)Week12 - 3© NUS  Download the programs from this web page   The files are:  Week12_TeamMate.c  Week12_Points.c  Week12_SortModules.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/lect/prog/week12_for_students/xxx.

Unit #18: Sections 1 – 4 CS1010 (AY2015/6 Semester 2)Week12 - 4© NUS 1.Overall Introduction 2.Introduction to Searching 3.Linear Search 4.Binary Search

Exercise #1: Compatible Teammate (1/5) CS1010 (AY2015/6 Semester 2)Week12 - 5© NUS  Given an array of players_t variables with the following structure definition. typedef struct { char name[STR_LENGTH+1]; int age; char gender; } player_t; Your task is to accept as input a player’s name and find a compatible teammate for this player – a teammate who is of the same age but opposite sex.  Analysis: Inputs: player_t players[MAX_PLAYER]; char player_name[STR_LENGTH+1];

Exercise #1: Compatible Teammate (2/5) CS1010 (AY2015/6 Semester 2)Week12 - 6© NUS  Sample run:  Given an incomplete program Week12_TeamMate.c, complete the program by filling in the search_teammate() function. Enter 5 players’ info: Alvin 23 M Byron 21 M May 19 F June 21 F Jupiter 22 M The list of players are: Name Age Gender Alvin 23 M Byron 21 M May 19 F June 21 F Jupiter 22 M Enter a player's name: Byron Byron’s compatible teammate is June. Enter a player's name: Alvin Sorry, we cannot find a teammate for Alvin! or Enter a player's name: John No such player John. or

Exercise #1: Compatible Teammate (3/5) CS1010 (AY2015/6 Semester 2)Week12 - 7© NUS int main(void) { player_t players[MAX_PLAYER]; char player_name[STR_LENGTH+1]; int result; read_players(players); print_players(players); printf("Enter a player's name: "); scanf("%s", player_name); result = search_teammate(players, player_name); printf("\n"); if (result == -2) printf("No such player %s.\n", player_name); else if (result == -1) printf("Sorry, we cannot find a teammate for %s!\n", player_name); else printf("%s's compatible teammate is %s.\n", player_name, players[result].name); return 0; } Week12_TeamMate.c

Exercise #1: Compatible Teammate (4/5) CS1010 (AY2015/6 Semester 2)Week12 - 8© NUS // Read information of players void read_players(player_t players[]) { int i; printf("Enter %d players' info:\n", MAX_PLAYER); for (i=0; i<MAX_PLAYER; i++) { scanf("%s %d %c", players[i].name, &players[i].age, &players[i].gender); } // Print players' information void print_players(player_t players[]) { int i; printf("\nThe list of players are:\n"); printf("Name\tAge\tGender\n"); for (i=0; i<MAX_PLAYER; i++) { printf("%s\t%d\t%c\n", players[i].name, players[i].age, players[i].gender); } Week12_TeamMate.c

Exercise #1: Compatible Teammate (5/5) CS1010 (AY2015/6 Semester 2)Week12 - 9© NUS // Search for a player's compatible teammate // Return index in array if compatible teammate found, or // -1 if compatible teammate not found, or // -2 if player not found int search_teammate(player_t players[], char player_name[]) { } Week12_TeamMate.c int i, player_index = -9; // First, check that the player_name appears in the names array for (i=0; i<MAX_PLAYER; i++) { if (strcmp(player_name, players[i].name) == 0) player_index = i; } if (player_index == -9) return -2; // no such player for (i=0; i<MAX_PLAYER; i++) { if ((players[i].age == players[player_index].age) && (players[i].gender != players[player_index].gender)) return i; } return -1; // cannot find compatible teammate

Unit #18: Sections 5 – 7 CS1010 (AY2015/6 Semester 2)Week © NUS 5.Introduction to Sorting 6.Selection Sort 7.Bubble Sort

Exercise #2: Points and Lines (1/6) CS1010 (AY2015/6 Semester 2)© NUS  Problem: 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.  The points are stored in an array of point_t variables with the following structure definition: typedef struct { int x, y; } point_t;  You may assume that there are at most 20 points and no two points are identical.  Do the sorting by calling Selection Sort only once. How do you adapt the Selection Sort code for this problem?  You are given an incomplete program Week12_Points.c  This exercise is mounted on CodeCrunch. Week

Exercise #2: Points and Lines (2/6) CS1010 (AY2015/6 Semester 2)© NUS  In the preceding section, the comparison is done using a simple relational operation: for (i = start_index+1; i < size; i++) if (arr[i] < arr[min_index]) min_index = i;  What if the problem deals with more complex data, like this exercise?  The simple relational operation above would have to be replaced by a more complex one. Or you could call another function to do it. For example, for this exercise: for (i = start_index+1; i < size; i++) if ( lessThan(pts, i, min_index) ) min_index = i; Week

Exercise #2: Points and Lines (3/6) CS1010 (AY2015/6 Semester 2)© NUS  Here’s the incomplete lessThan() function: // 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) { } return (pts[p].x < pts[q].x) || ((pts[p].x == pts[q].x) && (pts[p].y < pts[q].y)); Week

Exercise #2: Points and Lines (4/6) CS1010 (AY2015/6 Semester 2)© NUS  Here’s the incomplete sortPoint() function: // Sort points in ascending order of x-, and then y-coordinates void sortPoints(point_t pts[], int size) { int i, start, min_index; point_t temp; } 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; } Week

Exercise #2: Points and Lines (5/6) CS1010 (AY2015/6 Semester 2)© 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

Exercise #2: Points and Lines (6/6) CS1010 (AY2015/6 Semester 2)© NUS  Sample run: Enter number of points: 15 Enter x- and y-coordinates of 15 points: : 2 1 After sort: Point # 0: (1,2) Point # 1: (1,3) : Point #14: (12,2) Sum of lengths of vertical and horizontal lines = 13 See input file points.in Week

Exercise #3: Module Sorting (1/2) CS1010 (AY2015/6 Semester 2)Week © NUS  Week12_SortModules.c: Given an array of module_t variables with the following structure definition: typedef struct { char code[CODE_LENGTH+1]; int enrolment; } module_t; Your task is to sort the modules in ascending order of student enrolment, using Selection Sort, Bubble Sort, or Insertion Sort (if you happen to know it).  You may assume that there are at most 10 modules and a module code is at most 7 characters long  This exercise is mounted on CodeCrunch.

Exercise #3: Module Sorting (2/2) CS1010 (AY2015/6 Semester 2)Week © NUS  Sample run: Enter number of modules: 10 Enter module codes and student enrolment: CS CS CS1010E 358 CS IS IS IS GEK IT MA1101S 123 Input Sorted by student enrolment: IT GEK IS IS MA1101S 123 CS IS CS CS CS1010E 358 Output See input file modules.in

Things-To-Do CS1010 (AY2015/6 Semester 2)Week Deadline for Lab #5 8 Apr 2016, Friday 10pm 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) © NUS

End of File CS1010 (AY2015/6 Semester 2)Week © NUS