Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 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 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 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 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 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 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 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 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 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 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 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 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 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}


Download ppt "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."

Similar presentations


Ads by Google