Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ Change struct to class Make all the members private ; you can also add any other private members/methods if you want to Add get

c++

Change struct to class

Make all the members private ; you can also add any other private members/methods if you want to

Add get size , get capacity and get array methods

Complete and debug all the other methods in the sample code

Complete the int operator*(Vec&a, Vec&b); : it does the dot product for two vectors

Write some testing code in the main function

Separate and organize the code into Vec.h , Vec.cpp , and main.cpp

Change to template for any data type elements

You can rename any class/members/methods/variables to meet your coding style

#include #include

using namespace std;

// change to class class Vec {

private: int size = 0; int capacity = 0; int *arr; // the real array

public: Vec(int size); Vec(); Vec(int size, int n); int get_size(); int get_capacity(); int get_array();

void push_back(int n); void insert(int idx, int n); void remove(int idx); void pop_back(); void reserve(int sz); int index_of(int n); int at(int idx); int& operator[](int idx); ~Vec(); };

Vec operator+(Vec& a, Vec& b) { if ( a.get_size() != b.get_size() ) throw 3; Vec c(a.get_size()); for ( int i = 0; i < a.get_size(); i++ ) { c[i] = a[i] + b[i]; } return c; } int Vec::get_size() { return size; }

int Vec::get_capacity() { return capacity; }

int Vec::get_array() { return *arr; }

int operator*(Vec& a, Vec& b)

{int val = 0; for(int i = 0; i < a.get_size(); i++) { val += a[i] * b[i]; }

return val; } int Vec::at(int idx) { return this->arr[idx]; }

int& Vec::operator[](int idx) { return this->arr[idx]; }

void Vec::insert(int idx, int n) { // TODO: // Optimize these code... if ( idx < 0 || idx > size ) throw "[ERR] Insert index out of bound!"; if ( this->size == this->capacity ) { this->reserve(this->size * 2); } for ( int i = size; i > idx; i-- ) { arr[i] = arr[i-1]; } arr[idx] = n; this->size++; }

void Vec::push_back(int n) { if ( this->size == this->capacity ) { this->reserve(this->size * 2); } this->arr[this->size] = n; this->size++; }

void Vec::reserve(int sz) { if ( sz > this->capacity ) { this->capacity = sz; int *new_arr = new int[this->capacity]; for ( int i = 0; i < this->size; i++ ) { new_arr[i] = this->arr[i]; } delete[] this->arr; this->arr = new_arr; } }

Vec::Vec(int size) { this->size = size; capacity = 2 * size; this->arr = new int[capacity]; for ( int i = 0; i < size; i++ ) { this->arr[i] = 0; } }

Vec::~Vec() { delete[] arr; arr = nullptr; }

// int main() { /*Vec v(4); cout << v.size << " " << v.capacity << endl; v.reserve(10); cout << v.size << " " << v.capacity << endl; v.push_back(3); cout << v.size << " " << v.capacity << endl; v.push_back(1); cout << v.size << " " << v.capacity << endl; v.push_back(2); cout << v.size << " " << v.capacity << endl; v.push_back(3); cout << v.size << " " << v.capacity << endl; v.push_back(6); cout << v.size << " " << v.capacity << endl; v.push_back(8); cout << v.size << " " << v.capacity << endl; v.push_back(8); cout << v.size << " " << v.capacity << endl;*/

Vec a(1); a.push_back(3); a.push_back(1); a.push_back(4);

Vec b(1); b.push_back(9); b.push_back(2); b.push_back(7);

Vec c = a + b; for ( int i = 0; i < c.get_size(); i++ ) { cout << c.at(i) << " "; } //v.arr[2] = 9; //v[2] = 9; //cout << endl; //v.insert(3, 7); //for ( int i = 0; i < v.size; i++ ) //{ // cout << v.at(i) << " "; //}

/*vector vec; cout << vec.size() << " " << vec.capacity() << endl; vec.reserve(5); cout << vec.size() << " " << vec.capacity() << endl; vec.push_back(3); cout << vec.size() << " " << vec.capacity() << endl; vec.push_back(3); cout << vec.size() << " " << vec.capacity() << endl; vec.push_back(3); cout << vec.size() << " " << vec.capacity() << endl; vec.push_back(3); cout << vec.size() << " " << vec.capacity() << endl; vec.push_back(3); cout << vec.size() << " " << vec.capacity() << endl; vec.push_back(3); cout << vec.size() << " " << vec.capacity() << endl; vec.push_back(3); cout << vec.size() << " " << vec.capacity() << endl; vec.push_back(3); cout << vec.size() << " " << vec.capacity() << endl; vec.push_back(3);*/ }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Accounting questions

Question

please provide answer rounded to nearest cent

Answered: 1 week ago