1 Lab Session-XI CSIT121 Fall 2000 b Arrays and Their Usage b Finding the largest element b Lab Exercise 11-A b Searching for some value b Lab Exercise.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
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.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
实验11.28.
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
HST 952 Computing for Biomedical Scientists Lecture 9.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
1 Pre-Exam Class CSIT121 Fall 2000 Exam-II (Final Examination) TUESDAY DECEMBER 19th 8:30AM.
1 Lab Session-3 CSIT221 Spring 2003 b Group Worksheet 3 Exercise (Demo Required) b No new lab demo will be assigned to allow you to focus on HW#1.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
1 Lab Session-VIII CSIT-121 Fall 2000 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
1 Lab Session-VI CS121 Fall 2000 l HW#2 Assigned l Multiple Choice Selectors l The While Loop l Switch-Case-Break Exercise l The Counter Controlled Loops.
1 Lab Session-XII CSIT121 Spring 2002 b Arrays and Their Usage b Finding the largest element b Lab Exercise 12-A b Lab Demo (12-B) b Practice Problems.
1 Lab Session-IX CSIT121 Fall 2000 w Arrays and Their Usage w Examples and Lab Exercises w Passing Arrays to Functions w Examples and Exercises w Sorting.
1 Lab Session-VIII CSIT-121 Spring 2002 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
1 Lab Session-12 CSIT121 Fall 2004 Structures and Their Usage Passing Struct Variables to Functions.
Introduction to Object- Oriented Programming Prof. Shie-Jue Lee Dept. of Electrical Engineering National Sun Yat-sen University.
Arrays.
1 Techniques of Programming CSCI 131 Lecture 29 Search.
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.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
1 Lab Session-XI CSIT121 Spring 2002 w Sorting the arrays (array application) w Bubble Sort w Structures and Their Usage w Passing Struct Variables to.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Data Structures.  Consider storing data for 100 employees by their Social Security Numbers (SSN)  SSN range: –  A fast search:
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Lecture 6 : Intro. to Generic Programming Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
C++ Classes and Data Structures Jeffrey S. Childs
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
CSC Programming for Science Lecture 27: Arrays as Parameters, Searching, & Sorting.
Data Structures Arrays and Lists Part 2 More List Operations.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
CS 116 Object Oriented Programming II Lecture 4 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Searching Arrays Linear search Binary search small arrays
Chapter 7 Part 1 Edited by JJ Shepherd
Topics Introduction to File Input and Output
Chapter 9: Data Structures: Arrays
Topics Introduction to File Input and Output
Presentation transcript:

1 Lab Session-XI CSIT121 Fall 2000 b Arrays and Their Usage b Finding the largest element b Lab Exercise 11-A b Searching for some value b Lab Exercise 11-B b Files and Their Usage b Lab Exercise 11-C

2 Arrays and Their Usage b Arrays hold similar and related data items however data values are different b We can use arrays in any program where we have to deal with large amount of similar data using an INDEX into the data b For example, consider seats reservation status in a coach of AMTRAK.

3 Arrays and Their Usage b An inquiry is made to find out if seat number 23 of coach H1 is booked or not booked b bool H1[MAX_SEATS]; b if (H1[23] == true) b cout<<“Seat is booked\n”; b else cout<<“Seat is empty\n”;

4 Finding Largest Value b Often we will need to find the largest value in an array. b We have already done an example using records of the customers of a shop b We can designate the first element as the largest in the beginning b As we compare one by one, we change our mind if we find a larger value

5 Lab Exercise 11-A b Let us define an array and fill it with a lot of values b const int PLACES=15 b int distances[PLACES]={24,56,78,24, b 12,94,18,91,77,11,97,82,45,95,88}; b We wish to find the largest value in this array

6 Lab Exercise 11-A b Data Modeling b an array int distances[] b an integer largest as index to largest value b Algorithm largest=0, for(0 to PLACES-1) b 1. Designate distances[largest] as largest b 2. Compare with next value and change the largest index if found higher and loop to 1

7 Searching For Some Value b An application is in searching for a specific value in an array. b Searching can be done by extracting each value in the array and comparing it with the target value b Searching can be speeded up when using sorted arrays. b Let us do searching on an unsorted array

8 Lab Exercise 11-B b Given an array of ID numbers of library defaulters, search for the occurrence of a specific ID number to see if this person is a library defaulter or not b Data Modeling b int defaulters[MAXSIZE]; b int target_ID; b bool not_found;

9 Sample Data b const int MAXSIZE=20; b int defaulters[MAXSIZE] = b {2435,1211,7643,2437,9834,2001,1029,1290, b 2340,2200,1214,4438,2105,4355,8254,9120, b 2502,9403,1006,1205}; b target_ID = 8254 b target_ID=2000

10 Lab Exercise 11-B b Algorithm b not_found=true; b While (not_found) b Match the target_ID with contents of the defaulters array one by one b If matched, assign not_found=false b Else if array end reached, person not found

11 Files and Their Usage b Using files in your programs makes it easier to handle a lot of data b Files are permanently stored on disk b Files can be read by various programs so multiple applications can run off a single file b Files can be printed and plotted

12 Lab Exercise 11-C b A data file contains names and social security numbers of all library members that have not returned the books issued by the due date. The due dates are also indicated in the file as a string mmddyyyy. b Read the data file (lib.dat) and display a nicely formatted list of defaulters showing their names, SS# and due date

13 Lab Exercise 11-C b Data Modeling b To read the information from the file, we need the following data items b const int LEN=20; const int DUE=8; b int ssnumber; b char last_name[LEN], first_name[LEN]; b char due_date[DUE];

14 Lab Exercise 11-C b We also need an input file handle. We can use the following code to get started b ifstream my_infile; b my_infile.open (“lib.dat”); b if (my_infile.fail ()) b { b cerr << "*** ERROR: Cannot open " << “in.dat” b << " for input." << endl; b return EXIT_FAILURE; b}b}b}b}