bool distinct = false;
int size = 10; //change this to size you want array to be
int* array = newint[size]
//use for loop to enter in values
std::cout<<"\nEnter "<<size<<" numbers";
for (int i = 0; i < size; i++)
{
std::cin>>array[i];
}
std::cout<<"\nThe distinct numbers are: ";
for (int i = 0; i < size; i++) //loop to check if each number is distinct
{
for (int j = 0; j < size; j++)
{
if(arr[i] == arr[j]) //compares number with all other numbers
{
if (j != i) //needed so that array value isn't compared against itself
{
distinct = false;
}
}
}
if(distinct == false)
{
std::cout<<arr[i]<<" ";
}
distinct = true;
}
#include <iostream>
int main()
{
constint NUMBERS_SIZE = 10;
int numbers[NUMBERS_SIZE] = {0};
std::cout << "Enter " << NUMBERS_SIZE << " numbers: ";
for( int i = 0; i < NUMBERS_SIZE; ++i ) std::cin >> numbers[i];
std::cout << "The distinct numbers are: ";
for( int i = 0; i < NUMBERS_SIZE; ++i )
{
bool first_occurrence = true ;
for( int j = 0 ; j < i ; ++j ) // j < i : check with numbers occurring earlier than i
{
if( numbers[i] == numbers[j] ) // if this number was seen earlier
{
first_occurrence = false ; // it is not the first occurrence of the number
break ; // there is no need to check with more numbers
}
}
// if this is the first occurrence of the number, print it out
if(first_occurrence) std::cout << numbers[i] << ' ' ;
}
std::cout << '\n' ;
}