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;
}
I need the complete answer I don't get it.
Here is the answer for your question in C++ Programming Language.
Kindly upvote if you find the answer helpful.
NOTE : I have used dev-c++ to run the code,hence I had to use , #include "Midterm3400Arrays.cpp" in main.cpp file if, #include "Midterm3400Arrays.h" works for your as per your compiler please use that only.
#########################################################################
CODE :
Midterm3400Arrays.h
#include <iostream> class CSIS3400Arrays }; |
###############################################################
Midterm3400Arrays.cpp
#include "Midterm3400Arrays.h" // constructor //destructor /*locating target in the array*/ /*inserts value into the array*/ |
#####################################################################
main.cpp
#include "Midterm3400Arrays.cpp" using namespace std; int main(){ |
##################################################################
SCREENSHOTS :
Please see the screenshots of the code below for the indentations of the code.
Midterm3400Arrays.h
#####################################################
Midterm3400Arrays.cpp
########################################################################
main.cpp
##################################################################
OUTPUT :
Any doubts regarding this can be explained with pleasure :)