Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class VectorDouble {
- private:
- double* array;
- int max_count;
- int count;
- public:
- VectorDouble() {
- max_count = 50;
- array = (double*)malloc(max_count * sizeof(double));
- count = 0;
- }
- VectorDouble(VectorDouble& v) {
- max_count = v.size();
- array = (double*)malloc(max_count * sizeof(double));
- for (int i = 0; i < max_count; i++) {
- array[i] = v.value_at(i);
- }
- count = max_count;
- }
- int size() {
- return count;
- }
- void push_back(double d) {
- if (count == max_count) {
- max_count *= 2;
- double* newarr = (double*)malloc(max_count * sizeof(double));
- for (int i = 0; i < count; i++) {
- newarr[i] = array[i];
- }
- delete array;
- array = newarr;
- }
- array[count++] = d;
- }
- void change_value_at(double d, int i) {
- array[i] = d;
- }
- double value_at(int i) {
- return array[i];
- }
- void resize(int num) {
- if (num == max_count) {
- count = max_count;
- return;
- }
- double* newArray = (double*)malloc(num * sizeof(double));
- int temp = (count < num) ? count : num;
- for (int i = 0; i < temp; i++) {
- newArray[i] = array[i];
- }
- delete array;
- array = newArray;
- count = num;
- }
- void reserve(int num) {
- if (num <= max_count) return;
- max_count = num;
- double* newarr = (double*)malloc(max_count * sizeof(double));
- for (int i = 0; i < count; i++) {
- newarr[i] = array[i];
- }
- delete array;
- array = newarr;
- }
- };
- int main() {
- VectorDouble v;
- v.push_back(4.7);
- v.push_back(1992.4);
- v.push_back(842);
- v.push_back(.11);
- VectorDouble v2;
- for (int i = 0; i < 76; i++) { //Creating an array that is larger than max count to display it can still handle it by resizing
- v2.push_back(i);
- }
- cout << "Size of v is: " << v.size() << endl;
- cout << "Size of v2 is: " << v2.size() << endl;
- cout << "V = ";
- for (int i = 0; i < v.size(); i++) { //Printing contents of v
- cout << v.value_at(i) << " ";
- }
- cout << endl;
- v.change_value_at(56.1, 1); //Changing a value
- for (int i = 0; i < v.size(); i++) { //Printing contents of v
- cout << v.value_at(i) << " ";
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement