Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Vector is a data structure that is similar to array. The difference is that vect

ID: 3571243 • Letter: V

Question

Vector is a data structure that is similar to array. The difference is that vector can dynamically change its size. The following gives the vector class declaration: #ifndef VECTOR_H #define VECTOR_H #include using namespace std; calss Vector {public: Vector();//constructor Vector (int newSize);//constructor -Vector ();//destructor void loadData(string filename);//load data from a text file void Store (int val, int index);//store the first parameter val into data on the index-th location int ValueAt(int index);//get the value from data at the index-th location int Max();//find the max value in data using a loop int Max_r (const int data[], int first, int last);//find the max Value in data using recursion int Sum();//calculate the sum of data using a loop int Sum_r (const int data[], int first, in last);//calculate the Sum of data using recursion Void Print();//print the list of numbers in data in order using a loop Void Print_r (const in data[], int first, int last);//print the list of numbers in data in order using recursion int*data;//store a list of data int size;//store the size of input data int capacity;//store the capacity of data array}; #endif

Explanation / Answer

#include<iostream>// for using cout
#include<string>
#include <stdio.h>
using namespace std;
class Vector
{
public:
Vector();//constructor
Vector(int newSize); //constructor
~Vector();//destructor
void loadData(string filename);
void Store(int val,int index);
int ValueAt(int index);
int Max();
int Max_r(const int data[],int first,int last);
int Sum();
int Sum_r(const int data[],int first,int last);
void Print();
void Print_r(const int data[],int first,int last);
int *data;
int size;
int capacity;
};
Vector::Vector()
{
capacity=2;
data=new int[capacity];
size=0;
}
Vector::Vector(int newSize)
{
capacity=newSize;
data=new int[capacity];
size=0;
}
Vector::~Vector()
{
delete data;
}
void Vector::loadData(string filename)
{
freopen(filename.c_str(),"r",stdin);
int n,val;// n is number of integer values after that n values
// file contain following
// 2 4 5
cin>>n;
if(n>capacity)
{
data=new int[n];
size=n;
capacity=n;
}else
{
size=n;
}
for(int i=0;i<n;i++)
{
cin>>val;
data[i]=val;
}

}
void Vector::Store(int val,int index)
{
if(index>=capacity)
{

int* temp=new int[index+1];
for(int i=0;i<size;i++)
{
temp[i]=data[i];
}
delete data;//free the memory
capacity=index+1;// assuming 0 index
data=temp;
data[index]=val;
size=index+1;


}else if(index>=size)
{

data[index]=val;
size=index+1;
}else
{

data[index]=val;
}


}
int Vector::ValueAt(int index)
{
if(index<size && index>=0)
{
return data[index];
}
}
int Vector::Max()
{
if(size>0)
{

int maxVal=data[0];
for(int i=0;i<size;i++)
{
int temp=data[i];
if(temp>maxVal)
maxVal=temp;
}
return maxVal;
}

}
int Vector::Max_r(const int data[],int first,int last)
{
if(first==last)
{
return data[first];
}else
{
int left=data[first];
int right=Max_r(data,first+1,last);
if(left>right)
return left;
else
return right;

}
}
int Vector::Sum(){
if(size>0)
{
int sum=data[0];
for(int i=1;i<size;i++)
{
sum+=data[i];
}
return sum;

}
}
int Vector::Sum_r(const int data[],int first,int last)
{
if(first==last)
{
return data[first];
}else
{
return data[first]+Sum_r(data,first+1,last);
}

}
void Vector::Print()
{
for(int i=0;i<size;i++)
{
cout<<data[i]<<endl;
// printf("%d ",data[i]);
}
}
void Vector::Print_r(const int data[],int first,int last)
{
if(first==last)
{
cout<<data[first]<<endl;
}else
{
cout<<data[first]<<endl;
Print_r(data,first+1,last);
}
}
int main()
{
Vector v;
v.Store(3,0);
v.Store(100,1);
v.Store(90,2);
v.Print();
v.Print_r(v.data,0,2);
cout<<"Max:";
cout<<v.Max()<<endl;
cout<<"Sum:";
cout<<v.Sum()<<endl;
cout<<"Sum_r:";
cout<<v.Sum_r(v.data,0,2)<<endl;
cout<<"Max_r:";
cout<<v.Max_r(v.data,0,2)<<endl;
Vector vec;
vec.loadData("C:\Users\ArunAdmin\Desktop\android_constant_values.txt");
vec.Print();
}