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

Instructions: This exercise will continue your review of arrays in C++, but now

ID: 3881707 • Letter: I

Question

Instructions: This exercise will continue your review of arrays in C++, but now as a class.

Implement the following class:

class IntArray {
private:
int *mArray;
int mSize;

public:
/* Copy array's contents to an internal array, (length = size). */
/* Do a deep copy! */
IntArray(int *array, int size);
/* Return the current length of the array */
int getLength();
/* Returns the index in the array where value is found.
* Return -1 if value is not present in the array.
*/
int indexOf(int value);
/* Removes an item at position index by shifting later elements left.
* Returns true iff 0 <= index < size.
*/
bool remove(int index);
/* Returns an Array of all integers that are in common with self and ary.
* Return an empty Array if there are no intersections.
*/
IntArray* findIntersections(IntArray &ary);
/* Return true if the array ary is contained sequentially in self. */
bool isSubsequence(IntArray &ary);
/* Delete any used memory when this variable goes out of scope. */
~IntArray();
}

Here are the source files premade and given:

IntArray.cpp:

#include "IntArray.hpp"
/* Copy array’s contents to an internal array, (length = size).
* Do a deep copy!
*/
IntArray::IntArray(int *array, int size) {
}
int IntArray::get(int index) {
return mArray[index];
}
//destructor
IntArray::~IntArray() {
delete[] mArray;
}
int IntArray::getLength() {
return mSize;
}

IntArray.hpp:

#ifndef INTARRAY_HPP
#define INTARRAY_HPP
class IntArray {
private:
int *mArray;
int mSize;
public:
/* Copy array’s contents to an internal array, (length = size). */
IntArray(int *array, int size);
/* Return the current length of the array */
int getLength();
/* Returns the index in the array where value is found.
* Return -1 if value is not present in the array.
*/
int indexOf(int value);
/* Removes an item at position index by shifting later elements left.
* Returns true iff 0 <= index < size.
*/
bool remove(int index);
/* Returns an Array of all integers that are in common with self and ary.
* Return an empty Array if there are no intersections.
*/
IntArray* findIntersections(IntArray &ary);
/* Return true if the array ary is contained sequentually in self. */
bool isSubsequence(IntArray &ary);
/* Delete any used memory when this variable goes out of scope. */
~IntArray();
};
#endif

main.cpp:

#include
#include
#include "IntArray.hpp"
using namespace std;
int main(int argc, char *argv[]) {
int array[] = {1, 2, 3, 4, 5, 6};
IntArray DUT(array, 6);
IntArray *DUT2 = new IntArray(array, 6);
/* You may write manual tests here. */
delete DUT2;
}

Explanation / Answer

IntArray::IntArray(int *array, int size) {

int *newArray;

newArray = new int [size];

for(int i=0;i<size;i++){

newArray[i]=array[i];

}

}

IntArray::int getLength(int *array){

return (sizeof(array)/sizeof(array[0]));

}

IntArray::int indexOf(int *array,int value){

int position=0;

for(int i=0;i<n;i++){

if(array[i]==value){

  

position=i+1;

}

}

if(position==0)

return -1;

else

return position;

}

IntArray:: bool remove(int *array,int index){

  

int length=(sizeof(array)/sizeof(array[0]));

if (index > length-1){

return false;

}

for (int i = index; i < (sizeof(array)/sizeof(array[0])); ++i)

array[i] = array[i + 1];

  

return true;   

}   

  

  

IntArray::int * findIntersections(int *ary, int *self ){

int length1=(sizeof(ary)/sizeof(ary[0]));

int length2=(sizeof(self)/sizeof(self[0]));  

if(length1>length2){

length3=length2;

}

else{

  

length3=length1;

  

}  

int *newArray;

newArray = new int [length3];

int k=0;

for(int i=0;i<length1;i++){

for(j=0;j<length;j++){

  

if(ary[i]==self[j]){

newArray[k]=ary[i];

k++;

}

}

}

return newArray;

}

  

  

  

IntArray::bool isSubsequence(int *array){

int length=(sizeof(array)/sizeof(array[0]));

for(i=0;i<length-1;i++){

if(array[i]!=array[i+1])

return false;

}

return true;

}