




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





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; }
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++; } }
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
// At the end sm will reference the first location having the smallest value list
// 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
list
a.push_front(low); for ( p ++; *p != low; ++p ) { } a.erase(p); }
// Return the appropriate iterator return m_data + offset; }
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
(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
Solution:
void Customer::update_all( OrangeAppStore const& store, vector