Go with instructions.

Description

I have three PDF files. Please read them carefully and I’ll provide you with the extra material requested in the assignmentsafter match . For assignment Hwork8 you must submit it within 8hrs, and I can give you an extension for the rest of them.

Don't use plagiarized sources. Get Your Custom Assignment on
Go with instructions.
From as Little as $13/Page

Unformatted Attachment Preview

CSCI 201 – Computer Science 1
Homework 8. Due date: Sunday March 24
Objectives: Employ the property of sortedness to make array processing more efficient. Design a
nested for loop for the Matching-sum problem when the data is sorted. Measure the performance
improvement obtained by using sortedness.
IMPORTANT. Use the store and binary search functions from Lab 6 and Homework 6 to
implement the algorithm.
Keeping data sorted often results in query processing being more efficient. We saw this in the
case of searching, where the binary search algorithm is exponentially faster than sequential search.
In this homework, we look at another strategy for the matching sum problem that exploits the
non-decreasing order of items in A. We shall then compare the two strategies by plotting graphs
that show how many array accesses were needed by each of the strategies to find the answer.
Question 1. (Measuring scalability of the nested for loop.) Create 5 input files containing sorted
sequences of numbers (integers). The files should contain 20, 40, 60, 80 and 100 numbers respectively. For each file, identify (manually) a number, x, for which a matching sum cannot be found
(note that this gives us the maximum number of array accesses, i.e., the worst-case scenario). Run
your program from the lab, on all the five test files, using the number, x, you have identified, and
record the number of array accesses in a table.
New Strategy. In the sample run given above, we had the numbers 2 3 3 4 7 9 13 16 16 and
were looking for 23. We started with the first number, 2, and then checked all the other numbers to
see if the sum was 23. Instead, if we observe that 23 – 2 = 21, all we have to do is look for 21, which
can be done very quickly with binary search. More formally, if A[i] and A[j] form a matching sum
for x, then A[j] equals (x − A[i]). In the program written in the lab, the (i + 1)-th iteration of
the outer loop examines the pairs
(i, i+1), (i, i+2), …, (i, n-1)
to see if any of them equals x. Note that this is the same as checking if any item in A[(i+1) . . . (n−1)]
is equal to (x − A[i]). In other words what we are doing is searching the slice A[(i + 1) . . . (n − 1)]
for (x − A[i]). Since the array A is sorted, instead of searching all these items sequentially as we did
in the lab, we can search for the value (x − A[i]) in the slice A[(i + 1) . . . (n – 1)] using a binary
search.
Question 2.Modify the C++ program written in the lab to implement the above strategy for
finding a matching sum. This modification requires two things:
(i) adding the binSearch function (re-use from the earlier homework).
(ii) replacing the inner loop in the main program by a call to binSearch.
The function call will search for for x − A[i] in the slice A[(i + 1)…(n − 1)]. Your program should
also keep track of the number of array accesses using a global variable. Compile and test in a script
session.
Question 3. Run the program that uses the binary search strategy on the data files that you
created for Question 1. Record the number of array accesses in a table.
1
Question 4. Plot the number of array accesses versus the size of the input file, using the same X
and Y axes, for both strategies. Using your intuition and prior knowledge, try to find functions
that best match these graphs.
What to turn in. Upload the script file and answers for Questions 1, 3 and 4 to CourseFiles.
2
CSCI 201 – Computer Science 1
Lab assignment 8: Searching for a matching-sum in an array.
To be completed by Wednesday March 20.
Objectives: Apply the concept of employing a nested for loop for processing an array. Design a
nested for loop for the Matching-sum problem.
The Matching sum problem is defined as follows: Given an array, A, containing n items in nondecreasing order, and an item, x, find out if there are 2 distinct indices i and j, such that A[i] + A[j]
= x. A[i] and A[j] together constitute a Matching Sum for x. (This problem has widespread
applications in several areas of computing.)
We need a program that does the following:
(1) Reads a file containing several numbers in non-decreasing order and stores these in the array.
(2) Prompts the user to input a number for which a matching sum is needed.
(3) Checks for a matching sum. If a matching sum is found, then the indices i and j and the values
in A[i] and A[j] are reported; if no matching sum is found, the program reports failure. The
program also prints a count of the total number of “array access operations” (every time a pair of
square brackets appears in the program, count it as an array access operation).
Sample run: The array contains the numbers: 2 3 3 4 7 9 13 16 16 Two sample runs are
given below.
Please enter the number for which a matching sum is needed: 23
Item 4 (with value 7) and and item 7 (with value 16) add up to 23.
58 array items were accessed
Please enter the number for which a matching sum is needed: 35
No matching sum was found
72 array items were accessed
For the lab, we use the “brute force” strategy: make several passes over the array, so that we check
every pair (A[i], A[j]), and compare the sum A[i]+A[j] against x. The loops should be exited as
soon as the matching sum is found. (See the handout on nested loops.)
Question 1: Think about how we should make passes over the array. List the order in which all
the pairs of items will be examined in each pass, assuming an array of size n. (see the handout on
Designing Nested for Loops to see how this is done.)
Question 2: What action should we take for each pair of items that we are examine?
Question 3: Write a sequence of simple for loops for each pass. (see the handout on Designing
Nested for Loops to see how this is done.)
Question 4: Roll all the loops from your answer to Question 2 into a single nested for loop. (see
the handout on Designing Nested for Loops to see how this is done.)
Question 5: Implement a C++ program that implements your nested loop and terminates the
loops with a break statement when a matching sum is found. Use a global variable to keep track
of the number of array accesses. Run your program using a file with 20 items. Test multiple values
of x, some with a matching sum and some without. Create a script file.
1
Question 6: In the test runs, how many array accesses were needed to verify that no matching
sum exists?
Upload the script for Question 5, and answers to the other questions.
2
CSCI 201 – Computer Science 1
Introduction to classes.
Due on Tuesday March 26
Objectives: Employ the object technology provided by C++ to embed a functional (or imperative)
program into a method of a class, and then invoke that method to execute the program.
The lab assignment for this week is to convert an imperative (not having classes and objects)
program into an object-oriented program. This is done by writing the original program as the
run() method of a class, and write a small main program that creates an object of that class, and
invokes the run() method on that object. Following the example of the object-oriented version of
colorcounter class in the CourseFiles folder, we will rewrite the calculator program from Lab 3
as a class. (This is the simple version of the calculator, i.e., without functions.)
Question 1.
Re-write the calculator program so that it has a function run(istream&), and write a program that
allows the user to specify the input source, and calls the function with the appropriate parameter.
Question 2.
Study the process used in the lecture to change the colorCounter program to an object-oriented
version. We have to apply the same process to the program created in Question 1. What will be
placed in the .h and .cpp files? What will the main program contain?
Question 3.
Implement the calculator program with 3 files: the header file, the body of the class, and the main
program.
What to submit. Upload Script showing all the code files (the re-written program for
Question 1, the .h, .cpp and main() defined in Question 2), compilation of the class, compilation of main, and tests to the Lab9 folder in Github.
1

Purchase answer to see full
attachment