C++ answers only In this project, you will take your code from Assignment 3 and
ID: 3585178 • Letter: C
Question
C++ answers only
In this project, you will take your code from Assignment 3 and convert it into a myArray object. You will need to add the following constructors as well as a destructor:
1.myArray()
1.Default constructor
2.Should set size = 0 and initialize the arr member variable.
2.myArray(int _size, int num)
1.Alternate constructor
2.Should set size to _size and initialize all elements of arr to num.
3.myArray(double *_arr, int _size)
1.Alternate constructor
2.Should set size to _size and copy the elements of _arr into arr.
4.~myArray()
1.Destructor
2.Deletes memory allocation for your arr member variable.
A header will be provided in which all function prototypes will be given. You may add to this file, but do not change the portion that is provided!
Additional Specifications:
Your program should not use any pre-existing classes such as string or vector classes!
NO GLOBAL VARIABLES!
Header File
/***************************************************************************
* myArray class header file
***************************************************************************/
class myArray
{
public:
myArray();
myArray(int,double);
myArray(double*, int);
~myArray();
void insert(int, double);
void remove(int);
double get(int);
void clear();
int find(double);
bool equals(myArray&);
void print();
void init();
private:
int size;
double *arr;
};
Assignment#3 Code
#include <iostream>
using namespace std;
// Adds an element to the array at a given position and updates the size.
void insert(int index, double num, double *&arr, int &size)
{
if(index>size || index<0)
{
cout<<"Index is out of bounds!"<<endl;
}
else
{
double next = arr[index];
double prev = num;
for(int i=index; i<size; i++)
{
arr[i] = prev;
prev = next;
next = arr[i+1];
}
arr[size] = prev;
size++;
}
}
//Removes an element from the array at a given position and updates the size.
void remove(int index, double *&arr, int &size)
{
if(index>=size || index<0)
{
cout<<"Index is out of bounds!"<<endl;
}
else
{
for(int i=index; i<size-1; i++)
{
arr[i] = arr[i+1];
}
size--;
}
}
//Returns the element at the given position.
double get(int index, double *arr, int size)
{
if(index>=size || index<0)
{
cout<<"Index is out of bounds!"<<endl;
}
else
{
return arr[index];
}
}
//Clears all elements of the array and updates the size to be 0.
void clear(double *&arr, int &size)
{
for(int i=0; i<size; i++)
delete &arr[i];
size = 0;
}
//Returns the first index in which a given element is found in the array. If not found -1 is returned.
int find(double num, double *arr, int size)
{
for(int i=0; i<size; i++)
{
if(arr[i] == num)
}
return i;
return -1;
}
//Returns true if the contents of the two arrays are equal and false if they are not equal.
bool equals(double *arr1, int size1, double *arr2, int size2)
{
if(size1 == size2)
{
for(int i=0; i<size1; i++)
if(arr1[i] != arr2[i])
return false;
return true;
}
else return false;
}
//Populates the elements of the array with input from the user.
void init(double *arr, int size)
{
for(int i=0; i<size; i++)
{
cout<<" Enter data for index "<<i<<": ";
double num;
cin>>num;
arr[i] = num;
}
}
//Prints the elements of the array.
void print(double *arr, int size)
{
for(int i=0; i<size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
class myArray
{
public:
myArray();
myArray(int,double);
myArray(double*, int);
~myArray();
void insert(int, double);
void remove(int);
double get(int);
void clear();
int find(double);
bool equals(myArray&);
void print();
void init();
private:
int size;
double *arr;
};
myArray::myArray()// 1.Default constructor2.Should set size = 0 and initialize the arr member variable.
{
size = 0;
arr = new double();
}
myArray::myArray(int _size,double num)//Should set size to _size and initialize all elements of arr to num.
{
size = _size;
double array[size];//creating array...
arr = array;//pointing array
int i=0;
while(i<size)//all values initializing with num
{
*(arr+i)=num;
i++;
}
}
myArray::myArray(double *_arr,int _size)//Should set size to _size and copy the elements of _arr into arr.
{
size = _size;
double array[size];//creating array...
arr = array;//pointing array
int i=0;
while(i<size)//copying elements to arr
{
*(arr+i)=*(_arr+i);
i++;
}
}
myArray::~myArray()//destructor..Deletes memory allocation for your arr member variable.
{
int i=size-1;
while(i>=0)
{
free(arr+i);
i--;
}
size =0;
}
void insert(int index, double num, double *&arr, int &size)
{
if(index>size || index<0)
{
cout<<"Index is out of bounds!"<<endl;
}
else
{
double next = arr[index];
double prev = num;
for(int i=index; i<size; i++)
{
arr[i] = prev;
prev = next;
next = arr[i+1];
}
arr[size] = prev;
size++;
}
}
//Removes an element from the array at a given position and updates the size.
void remove(int index, double *&arr, int &size)
{
if(index>=size || index<0)
{
cout<<"Index is out of bounds!"<<endl;
}
else
{
for(int i=index; i<size-1; i++)
{
arr[i] = arr[i+1];
}
size--;
}
}
//Returns the element at the given position.
double get(int index, double *arr, int size)
{
if(index>=size || index<0)
{
cout<<"Index is out of bounds!"<<endl;
}
else
{
return arr[index];
}
}
//Clears all elements of the array and updates the size to be 0.
void clear(double *&arr, int &size)
{
for(int i=0; i<size; i++)
delete &arr[i];
size = 0;
}
//Returns the first index in which a given element is found in the array. If not found -1 is returned.
int find(double num, double *arr, int size)
{
for(int i=0; i<size; i++)
{
if(arr[i] == num)return i;
}
return -1;
}
//Returns true if the contents of the two arrays are equal and false if they are not equal.
bool equals(double *arr1, int size1, double *arr2, int size2)
{
if(size1 == size2)
{
for(int i=0; i<size1; i++)
if(arr1[i] != arr2[i])
return false;
return true;
}
else return false;
}
//Populates the elements of the array with input from the user.
void init(double *arr, int size)
{
for(int i=0; i<size; i++)
{
cout<<" Enter data for index "<<i<<": ";
double num;
cin>>num;
arr[i] = num;
}
}
//Prints the elements of the array.
void print(double *arr, int size)
{
for(int i=0; i<size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
int main()
{
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.