/* Lab 9 start * Add these methods: insert, sum, average, sort * Descriptions: * insert(value, position) : Takes a value and position, and inserts the value at that position * Note: It must move all later items one place to the right, and increase isize by 1 * sum() : Takes no parameters, returns the sum of all numbers in the array * average() : Takes no parameters, returns the avarage of all numbers in the array * Note: Feel free to call sum from inside average. You can do that, it'll work fine * sort() : Takes no parameters, sorts items from least to greatest * Note: You can use any algorithm you want, including bubble sort from previous demos * * This file was described in some detail in video 48, "abstract_data_type". This assignment is * based on the simpler adt example, and does not use template classes. Template classes would * complicate methods like sum and average, because with a template class, it's possible to store * characters or other types that don't naturally sum or average in the ADT. */ #include using namespace std; // Just stores integers for now class ourvector { size_t isize; size_t allocated_space; // how many integers we have space for int *data; public: ourvector(){ // This is a constructor, note return type is omitted data = new int[10]; isize = 0; allocated_space = 10; } /* TODO: Add your 4 methods here. If you really want, anywhere before the closing }; is ok. */ void push_back(int n){ if(isize == allocated_space){ allocated_space *= 2; int *newdata = new int[allocated_space]; for(int i = 0; i < isize; i++) newdata[i] = data[i]; delete [] data; data = newdata; } data[isize] = n; isize++; } int at(size_t idx){ if(idx >= isize) cout << "idx is too large!\n"; return data[idx]; } size_t size(){ return isize; } int& operator[](size_t idx){ if(idx >= isize) cout << "idx is too large!\n"; return data[idx]; } void remove_by_index(size_t idx){ for(int i = idx; i < isize - 1; i++) data[i] = data[i+1]; isize--; } ~ourvector(){ // This is a destructor, runs automatically when object goes out of scope delete [] data; } friend ostream& operator<<(ostream& output_stream, const ourvector& ov); }; /* Note: I oversimplified in the video, and later regretted it. If you pass the second parameter * as "ourvector ov", it'll pass a copy. When the copy goes out of scope, the destructor will run, * which will delete the memory backing "data". It does work in the video, but only by luck. * Passing a reference instead of a copy fixes the problem. Since we don't intend to change ov * in this function, a const reference is appropriate. */ ostream& operator<<(ostream& output_stream, const ourvector& ov){ output_stream << "["; for(int i = 0; i < ov.isize; i++) output_stream << ov.data[i] << ", "; output_stream << "\b\b]"; return output_stream; } void test_ourvector(){ /* test routine for our data structure */ ourvector ov; ov.push_back(7); ov.push_back(2); ov.push_back(654); ov.push_back(97); ov[1] = 60000; cout << ov << endl; for(int i = 0; i < 100; i++) ov.push_back(i); ov.remove_by_index(2); for(int i = 0; i < ov.size(); i++) cout << ov[i] << " "; cout << "\n"; /* TODO: Add appropriate test code for your new methods here */ } int main(){ test_ourvector(); return 0; }