CSCI-1200 Data Structures Test 1 — Practice Problems, Lecture notes of Data Structures and Algorithms

CSCI-1200 Data Structures. Test 1 — Practice Problems. Note: This packet contains practice problems from two previous exams. Your exam will contain.

Typology: Lecture notes

2022/2023

Uploaded on 05/11/2023

jesus33
jesus33 🇺🇸

4.2

(16)

422 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI-1200 Data Structures
Test 1 Practice Problems
Note: This packet contains practice problems from two previous exams. Your exam will contain
approximately half as many problems.
1 Catching Bugs [ /20]
Each fragment of C++ code below has an error. Your job is to first describe the problem in one or
two concise and well-written sentences. Will it cause a compile-time error? A run-time error? Or
just an incorrect answer? Also provide an appropriate correction that eliminates the error.
1.1 [ /5]
std::vector<double> my_vec;
my_vec.push_back(4);
my_vec.push_back(8);
std::vector<int> another_vec(my_vec);
1.2 [ /5]
int* p;
int* q = p;
p = new int;
*p = 10;
std::cout << *q << std::endl;
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CSCI-1200 Data Structures Test 1 — Practice Problems and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Data Structures

Test 1 — Practice Problems

Note: This packet contains practice problems from two previous exams. Your exam will contain approximately half as many problems.

1 Catching Bugs [ /20]

Each fragment of C++ code below has an error. Your job is to first describe the problem in one or two concise and well-written sentences. Will it cause a compile-time error? A run-time error? Or just an incorrect answer? Also provide an appropriate correction that eliminates the error.

1.1 [ /5]

std::vector my_vec; my_vec.push_back(4); my_vec.push_back(8); std::vector another_vec(my_vec);

1.2 [ /5]

int* p; int* q = p; p = new int; *p = 10; std::cout << *q << std::endl;

1.3 [ /5]

int arr[5] = { 10, 20, 30, 40, 50 }; int answer = 0; for (int i = 5; i > 0; i--) answer += arr[i];

1.4 [ /5]

int a = 8; int b = 22; int c = 15; float quadratic_root1 = -b + sqrt(bb - 4ac) / 2 * a; float quadratic_root2 = -b - sqrt(bb - 4ac) / 2 * a;

2.2 Sorting Required [ /12]

Now write a second version of the function, called only in one B, that does use the STL sort function (in a useful way). Both versions should return the same answer, although the order of the strings in the output may be different.

2.3 Order Notation [ /5]

Assuming there are n elements in vector a and m elements in vector b, give the order notation estimate of the number of operations required by your solutions

only in one A: only in one B:

3 The Polygon Class [ /33]

In this problem you will implement a simple class named Polygon to store information about different polygon objects. Each Polygon keeps track of the number of edges it has and the floating point lengths of each of those edges. Please carefully read through all of the information below before working on the class design.

Here are a few examples of how to create and initialize Polygon objects. The Polygon constructor takes in two arguments, the number of edges and the initial edge length. Note: initially all edges have the same length.

Polygon triangle1(3,1.5); Polygon triangle2(3,4.1); Polygon quad(4,2.1); Polygon hex(6,0.5);

After a Polygon is constructed the edge lengths can be modified one at a time as seen below:

triangle1.setEdgeLength(0,1.2); triangle2.setEdgeLength(2,3.5); hex.setEdgeLength(4,1.2);

The Polygon class includes an accessor function to calculate the perimeter of the polygon; that is, the sum of all the edges. For example, the perimeter of the quadrilateral Polygon in this example is 8.4:

double q1_perimeter = quad.getPerimeter();

The created objects may be stored in a vector named polys, which allows them to be sorted. We would like Polygons to be ordered by their perimeter (shortest first).

std::vector polys; polys.push_back(triangle1); polys.push_back(triangle2); polys.push_back(quad); polys.push_back(hex); std::sort(polys.begin(),polys.end()); for (int i = 0; i < polys.size(); i++) polys[i].print();

The code above results in this output to the screen:

Polygon [ 0.5 0.5 0.5 0.5 1.2 0.5 ] Polygon [ 1.2 1.5 1.5 ] Polygon [ 2.1 2.1 2.1 2.1 ] Polygon [ 4.1 4.1 3.5 ]

In the implementation of this class you should do error checking on the reasonableness of the arguments relative to our real-world understanding and expectations of polygons. If a problem is detected your program should output an appropriate error message to std::cerr and, if necessary, immediately exit the program via exit(1).

3.2 Polygon Class Implementation [ /20]

Now implement the constructor, member functions, and any helper functions you declared in the previous part, as they would appear in the corresponding polygon.cpp file.

4 Diagramming Memory [ /15]

In this problem you will work with pointers and dynamically allocated memory. First, examine the diagram drawn on the right, which shows memory usage in both the stack and heap, following the conventions from lecture. The dark out- lines indicate blocks of memory allocated for arrays. Within the array the smaller addresses are towards the bottom of the picture. All values shown in the picture are of type int.

Write a fragment a C++ code that will result in memory usage as shown to the right.

7000

20

30

40

a:

b: c: d: 100 5

6

heap

stack

e:

Finally, write a fragment a C++ code that cleans up all dynamically allocated memory within the above example so that the program will not have a memory leak.

6 Arrays, Vectors, & Order Notation [ /18]

6.1 [ /15]

Write a function that takes in an array of integers arr and an integer n, which is the number of elements in the array, and returns a vector of vectors representing all triplets (a, b, c) of integers from the input array that satisfy (b − a) = (c − b). You may assume that there are no repeated elements within the array. For example, if the input array contains the integers: 2, -3, 3, 0, 1

Your program should return the following triplets: (− 3 , 0 , 3), (0, 1 , 2), and (1, 2 , 3).

6.2 [ /3]

Give the order notation estimate of the number of operations required by your solution.

7 Command Line Arguments [ /15]

Write a short C++ program (write the main function) that accepts 1 or more strings as command line arguments and outputs them (using std::cout) in alphabetically sorted order. For example, if the executable is named a.exe, the command line:

a.exe cat apple dog banana

will result in the following output to the screen:

apple banana cat dog

8.1 Vegetable Class Declaration [ /15]

Using the sample code on the previous page as your guide, write the class declaration for the Vegetable object. That is, write the header file (vegetable.h) for this class. You don’t need to worry about the #include lines or other pre-processor directives. Decide how you are going to store the information for each object. Focus on getting the member function prototypes correct. Use const and call by reference where appropriate. Make sure you label what parts of the class are public and private. Don’t include any of the member function implementations that are more than a single line. Save the implementation of multi-line functions for the next part. Include the prototypes of any necessary helper functions related to this class.

8.2 Vegetable Class Implementation [ /20]

Now implement the constructor, member functions, and any helper functions you declared in the previous part, as they would appear in the corresponding vegetable.cpp file.

9.2 Rotation and Replacement [ /10]

Next write a simple function TireRotationAndReplacement that per- forms the tire rotation shown at the right. After rotating the tires, the function checks the tread thickness of each tire and replaces all tires whose thickness is < 3 units. The function returns the number of tires that were replaced. You should use the Tire member function getTreadThickness() and the default Tire constructor (with no argu- ments) that initializes the pressure and tread thickness for a new tire. Be sure to avoid memory leaks! Here is the prototype for the function:

d

a b

c

int TireRotationAndReplacement(Tire* &a, Tire* &b, Tire* &c, Tire* &d);

Modify your diagram from the previous part to show the result of calling this function once:

TireRotationAndReplacement(a,b,c,d);