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;


}

I need the complete answer I don't get it.

Solutions

Expert Solution

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>
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();
       //Adding function prototype
       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;
}
//Implementation of removeDups
void CSIS3400Arrays::removeNoDups(){
   //Required variables
   int count = 0,index = -1,noDupsArraySize = 0;
   //Create temporary array with numberOfElements as size
   int *noDups = new int[numberOfElements];
   //Loop through number of elements
   for(int i=0;i<numberOfElements;i++){
       //Set count to 0
       count = 0;
       //Loop through number of elements again for each element
       for(int j = 0;j<numberOfElements;j++){
           //If any element repeated
           if(intArray[i] == intArray[j]){
               //Set index and increment count by 1
               index = i;              
               count++;
           }          
       }      
       //If count is more than 1 (checked for more than 1 because the element itself will be 1 time counted)
       //For ex: if we search 0th index of [100,200,300,100] in 100 200 300 100 it returns 2,because one time is at 0th index
       //second time is at 3rd index, to make sure the number repeated index 0 is not counted
       if(count > 1){          
           //Add ith index of intArray to noDups array and increment its size;
           noDups[noDupsArraySize++] = intArray[i];
       }
   }  
   //Set number of elements of intArray to noDups array's size
   numberOfElements = noDupsArraySize;
   //Set intArray to noDups array
   intArray = noDups;
}

#####################################################################

main.cpp

#include "Midterm3400Arrays.cpp"

using namespace std;

int main(){
   CSIS3400Arrays obj(8);
   obj.insert(100);
   obj.insert(200);
   obj.insert(100);
   obj.insert(100);
   obj.insert(200);
   obj.insert(400);
   obj.insert(500);
   obj.insert(300);  
  
   cout << "Original array: " << endl;
   obj.display();
   obj.removeNoDups();
   cout << "After removing non-duplicate value: " << endl;
   obj.display();
}

##################################################################

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 :)


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