Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/cs104/arraylist/bounded_array_list by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Complete the given member functions of the BAListInt (Bounded array list of integers) class.
Engine
Template / Reference solution
 
#include <iostream>
#include <algorithm>
using namespace std;
class BAListInt
{
public:
   BAListInt(unsigned int cap);
   ~BAListInt() { delete [] data_; }
   bool empty() const { return size_ == 0; }
   unsigned int size() const { return size_; }
   void insert(int pos, const int& val);
   void remove(int pos);
   int const & get(int loc) const { return data_[loc]; }
   int& get(int loc) { return data_[loc]; }
   void set(int loc, const int& val) { data_[loc] = val; }
   void push_back(const int& val);
private:
   int* data_;
   unsigned int size_;
   unsigned int cap_;
};
BAListInt::BAListInt(unsigned int cap)
\[
REDACTED
]\
  
void BAListInt::push_back(const int& val)
{
   \[
REDACTED
]\
}
void BAListInt::insert(int loc, const int& val)
{
\[
REDACTED
]\
}
\hide[
void BAListInt::remove(int loc)
{
   if(loc >= 0 && loc < size_){
      for(int i=loc+1; i < size_; i++){
         data_[i-1] = data_[i];  
      }
      size_--;
   }
} 
]\
   
int main()
{
   BAListInt mylist(3);
   mylist.push_back(2);
   mylist.insert(0,1);
   mylist.insert(2,3);
   // 1 2 3 should be in list in that order
   for(int i=0; i < mylist.size(); i++){
      cout << mylist.get(i) << " ";
   }
   cout << endl;
   
   mylist.remove(2);
   mylist.remove(0);
   // 2 should be in the only item in list now
   for(int i=0; i < mylist.size(); i++){
      cout << mylist.get(i) << " ";
   }
   cout << endl;
   
   return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[{}]
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.