CS1010 Programming Methodology

Slides:



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

Introduction to C Programming
1 Community Right to Know Electronic Reporting Bruce Boyd Tina Gutierrez & Latoshia Parker Office of Pollution Prevention and Right to Know.
Chapter 7: Arrays In this chapter, you will learn about
Report Card P Only 4 files are exported in SAMS, but there are at least 7 tables could be exported in WebSAMS. Report Card P contains 4 functions: Extract,
CS1010: Programming Methodology
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
LIST PROCESSING.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course.
1 Symbol Tables Chapter Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.
CS1010: Programming Methodology
Social Web Design 1 Darby Chang Social Web Design.
Molecular Biomedical Informatics Web Programming 1.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
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);
Programming In C++ Spring Semester 2013 Practice 2 Programming In C++, Practice By Umer Rana.
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.
Binary Search Trees Ravi Chugh March 28, Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution:
WEEK 12 Class Activities Lecturer’s slides.
UNIT 15 File Processing.
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
WEEK 13 Class Activities Lecturer’s slides.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
UNIT 16 (Extra) Characters and Strings.
WEEK 5 Class Activities Lecturer’s slides.
CS1020 Week 3: 29th January 2015.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.
LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Sort the given string, without using string handling functions.
CS1010 Programming Methodology
CS1010 Programming Methodology
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
UNIT 12 UNIX I/O Redirection.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
WEEK 9 Class Activities Lecturer’s slides.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
1 Project: File System Textbook: pages Lubomir Bic.
1 Project 5: Median. 2 The median of a collection of numbers is the member for which there are an equal number less than or equal and greater than or.
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.
Use THIS LINK to get to AutoDesk’s Educational webpage.THIS LINK.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Command Line Arguments plus Variable-Length Arrays Systems Programming.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
WEEK 6 Class Activities Lecturer’s slides.
UNIT 11 Random Numbers.
WEEK 8 Class Activities Lecturer’s slides.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
WEEK 10 Class Activities Lecturer’s slides.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Week 12 Class Activities.
CS1010 Programming Methodology
Sample Presentation. Slide 1 Info Slide 2 Info.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Presentation transcript:

CS1010 Programming Methodology Lecturer’s slides http://www.comp.nus.edu.sg/~cs1010/ WEEK 11 Class Activities

Week 11: Searching and Sorting CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Week 11: Searching and Sorting Unit #18: Searching Exercise #1: Compatible Teammate Unit #18: Sorting Exercise #2: Points and Lines Exercise #3: Module Sorting

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Week 11 Programs Download the programs from this web page http://www.comp.nus.edu.sg/~cs1010/lect/prog/2014/week11_for_students The files are: Week11_TeamMate.c Week11_Points.c Week11_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/public_html/lect/prog/2014/week11_for_students/xxx .

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Unit #18: Sections 1 – 4 Overall Introduction Introduction to Searching Linear Search Binary Search

Exercise #1: Compatible Teammate (1/5) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #1: Compatible Teammate (1/5) Given three arrays: The first array contains the players’ names. The second array stores the players’ ages corresponding to the entries in the first array. The third array contains the gender information for each player corresponding to the entries in the first array. 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: char player_name[STR_LENGTH+1]; char names[MAX_PLAYER][STR_LENGTH+1]; int ages[MAX_PLAYER]; char genders[MAX_PLAYER];

Exercise #1: Compatible Teammate (2/5) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #1: Compatible Teammate (2/5) Sample run: 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 Given an incomplete program Week11_TeamMate.c, complete the program by filling in the search_teammate() function.

Exercise #1: Compatible Teammate (3/5) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #1: Compatible Teammate (3/5) int main(void) { char names[MAX_PLAYER][STR_LENGTH+1]; int ages[MAX_PLAYER]; char genders[MAX_PLAYER]; char player_name[STR_LENGTH+1]; int result; read_players(names, ages, genders); print_players(names, ages, genders); printf("Enter a player's name: "); scanf("%s", player_name); result = search_teammate(names, ages, genders, 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, names[result]); return 0; } Week11_TeamMate.c

Exercise #1: Compatible Teammate (4/5) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #1: Compatible Teammate (4/5) // Read information of players void read_players(char names[][STR_LENGTH+1], int ages[], char genders[]) { int i; printf("Enter %d players' info:\n", MAX_PLAYER); for (i=0; i<MAX_PLAYER; i++) { scanf("%s %d %c", names[i], &ages[i], &genders[i]); } // Print players' information void print_players(char names[][STR_LENGTH+1], int ages[], char genders[]) { printf("\nThe list of players are:\n"); printf("Name\tAge\tGender\n"); printf("%s\t%d\t%c\n", names[i], ages[i], genders[i]); Week11_TeamMate.c

Exercise #1: Compatible Teammate (5/5) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #1: Compatible Teammate (5/5) // 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(char names[][STR_LENGTH+1], int ages[], char genders[], char player_name[]) { } Week11_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, names[i]) == 0) player_index = i; } if (player_index == -9) return -2; // no such student in array if ((ages[i] == ages[player_index]) && (genders[i] != genders[player_index])) return i; return -1; // cannot find compatible teammate

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Unit #18: Sections 5 – 7 Introduction to Sorting Selection Sort Bubble Sort

Exercise #2: Points and Lines (1/6) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #2: Points and Lines (1/6) 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. Two arrays are used to store the points: array x for their x- coordinates, and array y for their y-coordinates. x[p] and y[p] refer to the point p. 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 Week11_Points.c This exercise is mounted on CodeCrunch.

Exercise #2: Points and Lines (2/6) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #2: Points and Lines (2/6) 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(x, y, i, min_index) ) min_index = i;

Exercise #2: Points and Lines (3/6) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #2: Points and Lines (3/6) 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(int x[], int y[], int p, int q) { } return (x[p] < x[q]) || ( (x[p] == x[q]) && (y[p] < y[q]) );

Exercise #2: Points and Lines (4/6) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #2: Points and Lines (4/6) Here’s the incomplete sortPoint() function: // Sort points in ascending order of x-, and then y-coordinates void sortPoints(int x[], int y, int size) { int i, start, min_index, temp; } for (start = 0; start < size-1; start++) { // find the index of minimum element min_index = start; for (i = start+1; i < size; i++) // check if point at index i is "less than" point at min_index if ( lessThan(x, y, i, min_index) ) min_index = i; // swap minimum element with element at start index temp = x[start]; x[start] = x[min_index]; x[min_index]=temp; temp = y[start]; y[start] = y[min_index]; y[min_index]=temp; }

Exercise #2: Points and Lines (5/6) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #2: Points and Lines (5/6) [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 3 4 5 6 7 8 9 10 11 12 Sum of lengths of horizontal and vertical lines = 1 + 3 + 2 + 3 + 3 + 1 = 13 (1,3) (1,2)

Exercise #2: Points and Lines (6/6) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #2: Points and Lines (6/6) Sample run: Enter number of points: 15 Enter x- and y-coordinates of 15 points: 5 3 2 4 : 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

Exercise #3: Module Sorting (1/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #3: Module Sorting (1/2) Week11_SortModules.c: Given two arrays: one containing the module codes, and the other containing the number of students enrolled in the modules. 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 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #3: Module Sorting (2/2) Sample run: Enter number of modules: 10 Enter module codes and student enrolment: CS1010 292 CS1234 178 CS1010E 358 CS2102 260 IS1103 215 IS2104 93 IS1112 100 GEK1511 83 IT2002 51 MA1101S 123 Input Sorted by student enrolment: IT2002 51 GEK1511 83 IS2104 93 IS1112 100 MA1101S 123 CS1234 178 IS1103 215 CS2102 260 CS1010 292 CS1010E 358 Output See input file modules.in

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Things-To-Do PE2 this Saturday! 1st November 2014, Saturday See CS1010 “PE” website for details Next week’s lecture: Structures

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) End of File