ou are required to write one main program that will read in any number of intege
ID: 3546793 • Letter: O
Question
ou are required to write one main program that will read in any number of integers and store it in an array of ints (not an ArrayList). What you need to do is to ask the user how many integers to be entered as input, and then ask the user to input that many integers. Your program will store these integers into an array of integers first. Then your program is supposed to remove the duplicates out of this array by first sorting the array and then removing any duplicates. Your program must not copy these elements into another array to remove the duplicates, the duplicates should be removed in place which means in the same array. Also, you must sort the array first before you start duplicate removal. Once your array (the same array you started with) has only unique numbers, your program should print out the array followed by a list of all the numbers that were duplicated. The numbers that were duplicated can be stored into another array. -------- The following would be a sample scenario of running your program: Enter the number of integers: 10 Enter the 10 integers: 7 9 8 1 9 24 23 24 9 8 The resulting array is: 1 7 8 9 23 24 The numbers 8, 9, 24 were duplicated in the input.
Explanation / Answer
/*
Enter the number of integers: 10
Enter the 10 integers:
7 9 8 1 9 24 23 24 9 8
The resulting array is: 1 7 8 9 23 24
The numbers 8, 9, 24 were duplicated in the input.
*/
#include<iostream>
using namespace std;
void insertion_sort(int array[],int size)
{
for (int i = 1; i < size; i++)
{
int value = array[i];
int j = i;
while(j>0 && array[j-1]>value)
{
array[j] = array[j-1];
j--;
}
array[j] = value;
}
}
void remove_duplicates(int array[],int& size, int duplicate_array[],int& duplicate_size)
{
duplicate_size = 0;
for(int i=0; i<size; i++)
{
for(int j=i+1; j<size; j++)
{
if(array[i]==array[j])
{
if(duplicate_size==0)
duplicate_array[duplicate_size++] = array[j];
if(duplicate_array[duplicate_size-1]!=array[j])
duplicate_array[duplicate_size++] = array[j];
for(int k=j; k<size-1; k++)
array[k] = array[k+1];
size--;
i--;
} //end if
} // end for
}
}
int main()
{
int no_of_integers;
cout <<"Enter the number of integers: ";
cin >> no_of_integers;
cout << endl;
int *integer_array = new int[no_of_integers];
int *duplicate_array = new int[no_of_integers];
int duplicate_size = no_of_integers;
cout <<"Enter the " << no_of_integers<< " integers: ";
for(int i=0; i<no_of_integers; i++)
cin >> integer_array[i];
cout << endl;
insertion_sort(integer_array,no_of_integers);
cout <<"The resulting array is: ";
for(int i=0; i<no_of_integers; i++)
cout << integer_array[i] << " ";
cout << endl;
remove_duplicates(integer_array,no_of_integers,duplicate_array,duplicate_size);
cout <<"The resulting array is: ";
for(int i=0; i<no_of_integers; i++)
cout << integer_array[i] << " ";
cout << endl;
cout <<"The numbers ";
for(int i=0; i<duplicate_size; i++)
{
if(i!=duplicate_size-1)
cout << duplicate_array[i] << ", ";
else cout << duplicate_array[i] << " ";
}
cout <<"were duplicated in the input. "<< endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.