hi i am brand new to c++ so please bear with me
i am trying to make a program that reads in an array of numbers between 1 and 100, as they are read in it will print them unless they are a duplicate number of one already in the array... this is the code i have so far and nothing is working
if someone could show me the code that would do this i would be so grateful, thanks so much!
Your if statement will alway be false, since you're checking if a number IS NOT equal to itself, which is never true.
What you can do is fill the array, then print the first number. Then, for each element after that, check to see if it has already been printed. If it has been printed, skip it and move to the next one.
constint SIZE = 100;
int main( )
{
int array[SIZE];
cout << "Enter numbers:" << endl;
for (int i = 0; i < SIZE; i ++)
{
cin >> array[i];
}
//This first loop will simply fill the array
cout << array[0] << " "; // this will print the first element
for (int i = 1; i < SIZE; i++)
{
bool repeated(false);
for (int j = 0; j < i; j++)
{
if (array[i] == array[j])
{
repeated = true;
}
}
if (repeated == false)
{
cout << array[i] << " ";
}
}
return 0;
}
I hate to give out code, but this problem could be tricky. Use a boolean to test for repetition (you can either break out or not). And only print the array element if the boolean is false.
I used "NULL" for the prevention of repetition, and I couldn't think of any other thing to differentiate integers of the array, so, I used NULL there. But I think NULL's aren't meant for int's.