



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A lab exercise where students are required to implement and test the sortedlisttype class, and use the binary search algorithm on it. The document also covers the concept of pointers and their relationship with memory. Students are expected to complete an application named scores.cpp and trace through the binary search algorithm for given keys.
Typology: Lab Reports
1 / 5
This page cannot be seen from the preview
Don't miss anything!
Lab 12 Sorted List Class and Binary Search This week’s lab will allow you to implement and test the SortedListType class that has been discussed in lecture. Later in lab, you’ll look at some example code using pointers. Copy the directory needed for today’s lab using the following command: cp –R ~ernie/220/lab. Sorted vs. Unsorted Lists We have discussed the implementation for a sorted list. We’ve seen that keeping a list in sorted order provides some benefits: we know the list is always ordered and we can use some additional algorithms like the binary search algorithm on the structure. Today you will be completing an application named Scores.cpp that uses the SortedList.h and SortedList.cpp. Compile Scores.cpp with SortedList.cpp and run the resulting program. Use the file temps.dat when prompted to enter the name of an input file and the name temps.out when prompted for the name of an output file. When you look at the output you’ll see that some things aren’t working as they should and here is some work that needs to be done. Read the source and complete the functions. When you have completed making the changes, put links to Scores. cpp and temps.out on your Web page that lists. Binary Search The program above doesn’t use the method BinSearch, but I’d like you to think about it here. BinSearch implements an algorithm that uses a process similar to one that you might use to locate a name in the phone book or a word in a dictionary. The algorithm is more complex than the sequential search algorithm, but the algorithm may result in fewer steps for the computer to execute. Question1: Trace through the binary search algorithm using the following array. For each trace show the number of iterations required to locate the item (or determine that it is not present) and the value of first, middle and last at each iteration. array: key = 9 trace: iteration 1: first = 0 last = 7 middle = 3 iteration 2: first =? last =? middle =? etc… Question 2: Repeat the tracing steps using the following key. key = 1 Question 3: Repeat the tracing steps using the following key. key = 12 Question 4: Repeat the tracing steps using the following key. key = 55 Question 5: Is it always possible to use the binary search algorithm on any list? Explain. Question 6 : Does the binary search algorithm always require fewer steps than the sequential search algorithm? Explain. Simple Pointers In this section, we will review pointers and stress some facts about pointers that the practicing programmer should keep in mind. We will also introduce a pictorial way of communicating facts about pointers and their relationship to memory. 1 2 3 9 10 11 12 23
Suppose two pointers ptr1 and ptr2 are declared as follows: **int ptr1; int ptr2; These declarations indicate that two new pointers should be created and that the pointers will point to (reference) memory locations containing integers. You should know by now that the mere declaration of a variable does not carry with it the blessing of initialization. Pointer variables are no exception to this rule; after declaring a pointer, you should assume that it essentially points nowhere. This initial pointer/memory relationship might best be communicated graphically as shown below. Memory cells ptr1 ptr To remedy this situation, you need to assign a valid pointer value to the pointer variable. This can be done in several ways. First, you may assign any pointer variable the value NULL. NULL is a reserved word in C++. The reserved word NULL is used to indicate that a pointer does not currently reference another memory location. So a NULL pointer is one that currently does not reference a memory cell. Assigning a new pointer the value NULL allows the programmer to test whether the pointer is unassigned using a Boolean expression such as the following if (ptr1 == NULL) In contrast, if we left all unassigned pointers pointing to their initial random memory locations, we would have no way of telling whether that pointer was pointing to something meaningful. To initialize our pointers to NULL , we use the following assignment statement: ptr1 = NULL; ptr2 = NULL; The pointer/memory relationship following these statements is illustrated below. Memory cells ptr1 ptr You may also use the new operator to assign a valid address to your pointer variable. new is a reserved word in C++. The new operator has the system search through memory, find the address of an int -sized block (in this case because we declared the pointers to be of type int ),
Our developing pointer/memory relationship is now shown below. Notice how the reference to the block containing the value 10 has now been lost. There is no way to recover the pointer to this memory location. Memory cells 10 20 ptr1 ptr A Program Using Pointers Open the file pointer.cpp in your editor. Read through the program and trace on paper what you anticipate the result of each line will be. Use the diagrams provided to draw the memory representation (can be done on this page rather than separate paper). Do not execute the program yet!! Question 4 : Draw an illustration of the pointers pointer1, pointer2, and pointer3 following the execution of the line *int pointer3; Memory cells Question 5: Draw an illustration of the pointers pointer1, pointer2, and pointer3 following the execution of the line *pointer1 = 20; You may assign a pointer to any memory cell in your picture. Memory cells Question 6: Draw an illustration of the pointers pointer1, pointer2, and pointer3 following the execution of the line pointer2 = pointer1; Memory Cells
Question 7: Draw an illustration of the pointers pointer1, pointer2, and pointer3 following the execution of the line *a = pointer1; Memory Cells In order to check your results above, we will add cout statements to our program that will indicate the values of the variables and pointers through out the execution of the program. Add cout statements to your program as indicated by the program comments. Remember that a pointer variable contains a memory address. To obtain the value referenced by the pointer, you must dereference the pointer (place an * in front of it). Execute the program and verify that your anticipated results match those produced by the program. Let me know if you are confused about any aspect of the program or results. Create Links to Sscores.cpp and temps.out pointer.cpp program Answers to Questions