Docsity

Test 2 Solutions - Data Structures - Fall 2012 | CSCI 1200, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CSci 1200 — Data Structures
Test 2 — March 8, 2012
SOLUTIONS
•Average: 80.3
•Distribution
Range Count
90-100 48
80-89 59
70-79 43
60-69 20
<60 12
•No curve
•Solutions are posted on the calendar section of the course website.
•If you think a mistake was made in grading:
–Write your reasoning on the page with the solution and make a note
of which question on the front of the test.
–Return your test to your lab TA by the end of lab.
–We will regrade all questions on your test, paying special attention
to the questions you marked.
–Regraded tests will be returned in lab next Wednesday.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Test 2 Solutions - Data Structures - Fall 2012 | CSCI 1200 and more Exams Data Structures and Algorithms in PDF only on Docsity!

CSci 1200 — Data Structures

Test 2 — March 8, 2012

SOLUTIONS

• Average: 80.

• Distribution

Range Count

• No curve

• Solutions are posted on the calendar section of the course website.

• If you think a mistake was made in grading:

– Write your reasoning on the page with the solution and make a note

of which question on the front of the test.

– Return your test to your lab TA by the end of lab.

– We will regrade all questions on your test, paying special attention

to the questions you marked.

– Regraded tests will be returned in lab next Wednesday.

  1. (12 points) Use the following non-templated Node class

class Node { public Node * next; double value; Node( double v ) : value(v), next(0) {} };

and write a function that takes a pointer to the header node of a linked list (constructed so that the next value stored in the the last node of the list is 0) and two doubles x1 and x and returns the number of nodes in the linked list that store values greater than x1 and less than x2. The function prototype is

int num_between( Node* head, double x1, double x2 )

Solution:

int num_between( Node* head, double x1, double x2 ) { int count = 0; for ( Node* p = head; p != 0; p = p->next ) if ( x1 < p->value && p->value < x2 ) ++count; return count; }

It is possible to use head itself, since it is passed by value:

int num_between( Node* head, double x1, double x2 ) { int count = 0; while ( head ) { if ( x1 < head->value && head->value < x2 ) ++count; head = head->next; } return count; }

  1. (14 points) Write a segment of code that reads a positive integer value, n, from std::cin, dynamically allocates an array of n float values, and then reads float values from std::cin, storing the positive values in the array. It should stop when there are n positive values stored. For example, if the input from std::cin is

4 -1.

Then at the end your code segment, you should have the 4 values

2.0, 5.6, 8.6, 1.

in your dynamically allocated array. Note that the final 1.4 is never read into your code. You may assume there are enough positive input values to fill the array. Provide all variable declarations that you need. Remember, this is just a segment of code, not an entire function!

Solution:

int n; std::cin >> n; float * arr = new float[n]; int i = 0;

while ( i<n ) { float x; cin >> x; if ( x > 0 ) { arr[i] = x; i++; } }

  1. (16 points) Write a function that finds the smallest value in a std::list and moves this value to the front of the list. For example, if the values in the list before the function call are

8.5, 17.6, 1.3, 19.1, 1.3, 21.

then after the function the values should be

1.3, 8.5, 17.6, 19.1, 1.3, 21.

Notice that the duplicate 1.3 is not moved.

Start by writing the function prototype

Solution: Version 1 involves remembering the iterator that references the first smallest value

void smallest_to_front( list & a ) { if ( a.empty() ) return;

// At the end sm will reference the first location having the smallest value list::iterator p = a.begin(); list::iterator sm = p; for ( p ++; p != a.end(); ++p ) if ( *p < *sm ) sm = p;

// Put the value on the front before erasing the iterator. This // will work properly even if sm == a.begin() a.push_front(*sm); a.erase(sm); }

Solution Version 2: The following version finds the smallest value, pushes it onto the front and then goes back through the list (skipping the first entry) to find the value that should be deleted.

void smallest_to_front( list & a ) { if ( a.empty() ) return;

list::iterator p = a.begin(); double low = *p; for ( p ++; p != a.end(); ++p ) if ( *p < low ) low = p;

a.push_front(low); for ( p ++; *p != low; ++p ) { } a.erase(p); }

// Return the appropriate iterator return m_data + offset; }

  1. (24 points) Below please find the declaration of the OrangeAppStore class from Prof. Stew- art’s solution to HW 4.

class OrangeAppStore { public: OrangeAppStore();

// Return true if and only if the app exists. bool does_app_exist( std::string const& app_name );

// Add the app with version 0. Do nothing and return false if the app already exists. bool add_app( std::string const& app_name, std::string const& category );

// Increment the version for the app. Return false if the app does not exist. bool new_version( std::string const& app_name, int & version_num );

// Get the ordered names of all apps in the given category. void in_category( std::string const& category_name, std::vector<std::string> & app_names ) const;

// Get the current version number of the app. Return false if the app does not exist bool current_version( std::string const& app_name, int & version_num ) const;

private: // Return true if the app exists, providing the index of the app // in the vector. Return false otherwise. bool find_index( std::string const& app_name, int & index ) const;

private: // There are three parallel vectors to represent the names of the // apps, their categories and their version numbers. It might be // better to have a single vector of (simple) app objects. std::vector<std::string> m_names; std::vector<std::string> m_category; std::vector m_version; };


(a) Write the member function add_app. You may use any other member function listed above and assume it is correct.

Solution:

bool OrangeAppStore::add_app( std::string const& app_name, std::string const& category ) { int index; if ( find_index(app_name, index) ) return false; m_names.push_back(app_name); m_category.push_back(category); m_version.push_back(0); return true; }

(b) The Customer class stores the name of the customer, and two vectors, the (unordered) app names and the app versions, where the ith version is for the ith app. Here are these member variables: std::string m_name; std::vector<std::string> m_app_names; std::vector m_versions; Write a member function of the Customer class called update_all that uses an OrangeAppStore object, passed as a parameter, to update all of the apps to their current version and that returns, as a parameter, a vector that contains the sorted names of all updated apps.

Solution:

void Customer::update_all( OrangeAppStore const& store, vector & updated_apps ) { updated_apps.clear(); for ( unsigned int i=0; i<m_app_names.size(); ++i ) { int version; if ( store.current_version(m_app_names[i], version) && version != m_versions[i] ) { m_versions[i] = version; updated_apps.push_back( m_app_names[i] ); } } sort( updated_apps.begin(), updated_apps.end() ); }