Hey Guys, I saw examples on the site for removing duplicates within an Array but when I use my code I can't get my inner-for-loop to check the last value in the array and so the array holding 5 7 2 5 1 turns into 5 7 2 1 1 instead of 5 7 2 1.
#include <iostream>
usingnamespace std;
//Constants
constint SIZE = 5;
//functions
void arrFiller(int orgArr[], int SIZE);
//Fills the Array with user Numbers
int main(){
int userArr[SIZE];
arrFiller(userArr, SIZE);
int numCurrEle = SIZE;
//Itterate through userArr using i = index and increment the index position by 1
for (int i = 0; i < SIZE; i++)
{
//Itterate through the userArr again using j = i + 1 so if 'i' is equal to 1 then 'j' equals 2 and increment the
for (int j = i+1; j < SIZE; j++)
{
//check to see if the value in position userArr[i] is equal to the value in userArr[j]
if (userArr[i] == userArr[j])
{
//Assign the value that's at position userArr[j + 1] to the value that's in userArr[j]
for (int k = j; k < numCurrEle - 1; k++)
{
userArr[k] = userArr[j + 1];
}
}
}
}
for (int i = 0; i < SIZE; i++)
{
cout << "\n" << userArr[i];
}
system("pause");
return 0;
}
void arrFiller(int orgArr[], int SIZE)
{
int temp;
cout << "You must input 10 numbers " << endl;
for (int index = 0; index < SIZE; index++)
{
cout << "\nPlease enter the next number : ";
cin >> temp;
orgArr[index] = temp;
}
}
It's correct only that you need to ignore the last element. You will probably need a non-const variable to keep track of the size so that you can subtract it when you find a duplicate.
#include <iostream>
usingnamespace std;
//Constants
constint SIZE = 5;
//functions
void arrFiller(int orgArr[], int SIZE);
//Fills the Array with user Numbers
int main(){
int userArr[SIZE];
arrFiller(userArr, SIZE);
// int numCurrEle = SIZE;
int size = SIZE;
// our size counter must be mutable!
//Itterate through userArr using i = index and increment the index position by 1
for (int i = 0; i < size; i++) // size changes when we find an element
{
cout << "\ninsde i-loop i = " << i << "and size = " << size;
//Itterate through the userArr again using j = i + 1 so if 'i' is equal to 1 then 'j' equals 2 and increment the
for (int j = i + 1; j < size; j++)
{
cout << "\ninside j-loop: " << j << "and size = " << size;
//check to see if the value in position userArr[i] is equal to the value in userArr[j]
if (userArr[i] == userArr[j])
{
cout << "\ninside comparision loop " << "i = " << i << " and j = " << j << "and size = " << size;
//Assign the value that's at position userArr[j + 1] to the value that's in userArr[j]
for (int k = j; k < size - 1; k++)
{
cout << "\ninside shift k-loop and k = " << k << "and size = " << size;
userArr[k] = userArr[j + 1];
}
--size;
--j;
}
}
}
/*
for (int i = 0; i < size; i++)
{
cout << "\n" << userArr[i];
}
*/
system("pause");
return 0;
}
void arrFiller(int orgArr[], int SIZE)
{
int temp;
cout << "You must input 10 numbers " << endl;
for (int index = 0; index < SIZE; index++)
{
cout << "\nPlease enter the next number : ";
cin >> temp;
orgArr[index] = temp;
}
}