Question

In: Computer Science

This Array implementation allows duplicates. Add a method that searches the array and remove all the...

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;


}

Solutions

Expert Solution

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


Related Solutions

This Array implementation allows duplicates. Add a method that searches the array and remove all the...
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...
This all has to be in javascript A common way to remove duplicates from an array...
This all has to be in javascript A common way to remove duplicates from an array is to convert it to a set and then convert it back to an array. We will do that here. Please use these variables and data structures: var input = [3, 4, 2, 2, 4, 5, 3, 1, 3, 6]; var set = new Set(); 1. Get each element from the input array and add it to the set. 2. Get the elements from...
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice...
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It doesn't matter what you leave beyond the returned...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new element to an array.​ Change an element with the new one.​ Search for a particular element in the array.​ ​The code must have four separate methods for each task stated above.​ Do not use any pre-defined Java functions.​ You are free to use int or String data-type for the array.​
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove,...
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove, and list. This assignment is to extend the functionalities of the bag with other operations average, min, and max, You need to extend the Bag class (under Wk 2, BagLinked_List.cpp) with the following methods: -int Bag::min( ), is to find minimum of items of the bag. -int Bag::max( ), is to find maximum of items of the bag -float Bag::ave( ), is to find...
What is wrong with my add and remove method (in bold) under the SongList that it...
What is wrong with my add and remove method (in bold) under the SongList that it is creating a NullPointerException? public class Song { // instance variables private String m_artist; private String m_title; private Song m_link; // constructor public Song(String artist, String title) { m_artist = artist; m_title = title; m_link = null; } // getters and setters public void setArtist(String artist) { m_artist = artist; } public String getArtist() { return m_artist; } public void setTitle(String title) { m_title...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
write a program using the main method where it searches through an array of positive, non-zero...
write a program using the main method where it searches through an array of positive, non-zero integer values and prints out the maximum even and odd values. The program should use the array provided in the code, and you may assume that it has already been populated with values. The results should be printed out to the console in the following format: “Max Even: ”<<max even value>> “Max Odd: ”<<max odd value>> Values denoted in “<< >>” represent variable values,...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to the ensureCapacity() method to print out a message including how many elements are copied to the new array on resizing Array List Implementation: public class MyArrayList<E> implements MyList<E> { public static final int INITIAL_CAPACITY = 16; private E[] data = (E[])new Object[INITIAL_CAPACITY]; private int size = 0; // Number of elements in the list public MyArrayList() { }    public MyArrayList(E[] objects) { for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT