In: Computer Science
This Array implementation allows duplicates.
Add a method that searches the array and remove all the values in the array that does not have a duplicate.
void removeNoDups( ) ( 12 points)
For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows only values with duplicates.
Add a main file to test your implementation of the function( 3 points )
#include <iostream>
using namespace std;
class CSIS3400Arrays
{
private:
int size;
int numberOfElements = 0;
int* intArray;
/*locating method for the array*/
int locationOf(int target);
public:
//default constructor and constructor
CSIS3400Arrays();
CSIS3400Arrays(int BUFFER_SIZE);
//destructor
~CSIS3400Arrays();
/*standard array methods*/
bool insert(int value);
bool remove(int value);
bool find(int target);
void display();
};
#include "Midterm3400Arrays.h"
//default constructor
CSIS3400Arrays::CSIS3400Arrays()
{
size = 100;
intArray = new int[size];
}
// constructor
CSIS3400Arrays::CSIS3400Arrays(int bufSize)
{
size = bufSize;
intArray = new int[size];
}
//destructor
CSIS3400Arrays::~CSIS3400Arrays()
{
if (intArray)
{
delete[] intArray;
}
}
/*locating target in the array*/
int CSIS3400Arrays::locationOf(int target)
{
/* finds the target index if exists then returns that index*/
for (int i = 0; i < numberOfElements; i++)
if (intArray[i] == target)
{
return i;
}
/*returns -1 if not located*/
return -1;
}
/*inserts value into the array*/
bool CSIS3400Arrays::insert(int value)
{
/*checks to see if array is full*/
if (numberOfElements < size)
{
/* adds the value into the next empty slot*/
intArray[numberOfElements++] = value;
return true;
}
else
{
/*prints that array is full*/
cout << "\nArray is Full and insert failed\n" <<
"\n";
return false;
}
}
/*removes the value and then shifts up the values to fill in the
empty spot(s)*/
bool CSIS3400Arrays::remove(int value)
{
int index = locationOf(value);
if (index != -1)
{
cout << "\nInteger: " << intArray[locationOf(value)]
<< " removed\n\n";
for (int i = index; i < numberOfElements - 1; i++)
{
intArray[i] = intArray[i + 1];
}
numberOfElements--;
return true;
}
else
{
cout << "\nValue not found in array\n" <<
"\n";
return false;
}
}
/*returns whether a value is found or not found in the
array*/
bool CSIS3400Arrays::find(int target)
{
if (locationOf(target) != -1)
{
cout << target << " Found\n\n";
return true;
}
else
{
cout << target << " Not Found\n\n";
return false;
}
}
/*prints out all the integers in the array*/
void CSIS3400Arrays::display()
{
cout << "\nCSIS3400Arrays : ";
for (int i = 0; i < numberOfElements; i++)
{
if (i == numberOfElements - 1)
{
cout << intArray[i];
}
else
{
cout << intArray[i] << ",";
}
}
cout << endl;
}
Please look at my code and in case of indentation issues check
the screenshots.
My changes are highlighted in Yellow.
--------------Midterm3400Arrays.h---------------
#include <iostream>
using namespace std;
class CSIS3400Arrays
{
private:
int size;
int numberOfElements = 0;
int *intArray;
/*locating method for the
array*/
int locationOf(int target);
public:
//default constructor and
constructor
CSIS3400Arrays();
CSIS3400Arrays(int
BUFFER_SIZE);
//destructor
~CSIS3400Arrays();
/*standard array methods*/
bool insert(int value);
bool remove(int value);
bool find(int target);
void display();
void removeNoDups();
};
--------------Midterm3400Arrays.cpp---------------
#include "Midterm3400Arrays.h"
//default constructor
CSIS3400Arrays::CSIS3400Arrays()
{
size = 100;
intArray = new int[size];
}
// constructor
CSIS3400Arrays::CSIS3400Arrays(int bufSize)
{
size = bufSize;
intArray = new int[size];
}
//destructor
CSIS3400Arrays::~CSIS3400Arrays()
{
if (intArray)
{
delete[] intArray;
}
}
/*locating target in the array*/
int CSIS3400Arrays::locationOf(int target)
{
/*finds the target index if exists then returns that
index*/
for (int i = 0; i < numberOfElements; i++)
if (intArray[i] == target)
{
return i;
}
/*returns -1 if not located*/
return -1;
}
/*inserts value into the array*/
bool CSIS3400Arrays::insert(int value)
{
/*checks to see if array is full*/
if (numberOfElements < size)
{
/*adds the value into the next
empty slot*/
intArray[numberOfElements++] =
value;
return true;
}
else
{
/*prints that array is full*/
cout << "\nArray is Full and
insert failed\n" << "\n";
return false;
}
}
/*removes the value and then shifts up the values to fill in the
empty spot(s)*/
bool CSIS3400Arrays::remove(int value)
{
int index = locationOf(value);
if (index != -1)
{
cout << "\nInteger: "
<< intArray[locationOf(value)] << " removed\n\n";
for (int i = index; i <
numberOfElements - 1; i++)
{
intArray[i] =
intArray[i + 1];
}
numberOfElements--;
return true;
}
else
{
cout << "\nValue not found in
array\n" << "\n";
return false;
}
}
/*returns whether a value is found or not found in the
array*/
bool CSIS3400Arrays::find(int target)
{
if (locationOf(target) != -1)
{
cout << target << "
Found\n\n";
return true;
}
else
{
cout << target << " Not
Found\n\n";
return false;
}
}
/*prints out all the integers in the array*/
void CSIS3400Arrays::display()
{
cout << "\nCSIS3400Arrays : ";
for (int i = 0; i < numberOfElements; i++)
{
if (i == numberOfElements -
1)
{
cout <<
intArray[i];
}
else
{
cout <<
intArray[i] << ",";
}
}
cout << endl;
}
/*method that searches
the array and remove all the values in the array that does not have
a duplicate*/
void
CSIS3400Arrays::removeNoDups()
{
int i =
0;
while(i <
numberOfElements)
{
bool isDuplicate = false; //keep a
flag to see if ith element has duplicates
for(int j = 0; j < numberOfElements;
j++) //loop through the array
{
if(i!=j &&
intArray[i] == intArray[j]) //check if it has
duplicate
isDuplicate = true;
//set the
flag to indicate duplicate found
}
if(!isDuplicate){
//if no duplicate
found
remove(intArray[i]);
//remove
the value from array
}
else{
i++;
//move to next
index
}
}
}
--------------main.cpp---------------
#include
<iostream>
#include
"Midterm3400Arrays.h"
using namespace
std;
int main()
{
CSIS3400Arrays
array; //create an object of class
CSIS3400Arrays
int
n;
cout <<
"How many values do you want to insert into array? ";
cin >>
n;
//read number of values user will input into
array
int
value;
for(int i = 0;
i < n; i++){ //read and insert
the values into array
cout << "Enter value to be inserted:
";
cin >> value;
array.insert(value);
}
array.display(); //display
array
array.removeNoDups(); //remove non duplicate
elements
array.display(); //display
array
return
0;
}
--------------Screenshots--------------
--------------------Output-----------------------
-----------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou