Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Huy
- * @date 11.20.18
- * @file h18.cpp
- */
- #include <string>
- #include <iostream>
- #include <memory>
- using namespace std;
- string STUDENT = "hnguyen771"; // Add your Canvas/occ-email ID
- #include "h18.h"
- // Write your functions here
- FlexArray& readData(istream& in, FlexArray& a)
- {
- size_t capacity = INITIAL_CAPACITY, i = 0;
- int num;
- a.data_.reset(new int[INITIAL_CAPACITY]);
- while (in >> num)
- {
- a.data_[i] = num;
- i++;
- if (i == capacity)
- {
- capacity *= 2;
- int * temp = new int [capacity];
- for (size_t j = 0; j < i; j++) temp[j] = a.data_[j];
- a.size_ = i;
- a.data_.reset(temp);
- }
- else
- {
- int * temp = new int [capacity];
- for (size_t j = 0; j < i; j++) temp[j] = a.data_[j];
- a.size_ = i;
- a.data_.reset(temp);
- }
- }
- int * temp = new int[a.size_];
- a.data_.reset(temp);
- return a;
- }
- string toString(const FlexArray& a)
- {
- string result = "{";
- if (a.size_ == 0) return "{}";
- result += to_string(a.data_[0]);
- for (size_t i = 1; i < a.size_; i++) result += ", " + to_string(a.data_[i]);
- return result + "}";
- }
- //////////////////////// STUDENT TESTING //////////////////////////
- #include <iostream>
- #include <sstream>
- int run()
- {
- cout << "Add your own tests here" << endl;
- istringstream in("8 9 Q 4 5");
- FlexArray a;
- in >> a;
- cout << "a->" << a << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement