conditional loop

here i'm trying to make a program that has an output like this

input data into array:
Data 1: 2
Data 2: 4
Data 3: 4
Data 4: 4
Data 5: 2
Data 6: 7
Data 7: 8
Data 8: 1
Data 9: 10
Data 10: 11

the number that show up are:
2 4 7 8 1 10 11

there you can see that the number only show up once ..
how to do that ?

this is my trial ..

int main ()
{
int a[10];
for (int i=1;i<=10;i++)
{
cout << "\nData " << i << " :";
cin >> a[i];
}
cout << "\nthe number that show up are:\n";
for (int loop1 = 1; loop1 <= 10; loop1++)
for (int loop2 = 1; loop2 <= 10; loop2++)
if ( a[loop1] == a[loop2] )
{
cout << a[loop1]<< " ";
break;
}
return 0;
}

but its just rewrite the data all over again ..


NEED A SUGGESTION !!


here is the algo
if the data are same ..
print the data
then skip to the next one


the one that comes in mind is to use the break and continue ..
but dont have any idea where to put it ..
You're almost there.

You need just one loop and you need to store the value last seen. Then your if statement does:
1
2
3
4
5
if (a[i] != last_seen)
{
    // display a[i]
    // assign a[i] to last seen
}
So what would happen if it alternated between 4 and 2 multiple times kbw?
Good point. You'll need to remember the values you've already processed.
here is the result

for (int loop1 = 1; loop1 <= 10; loop1++)
if (a[loop1] != last_seen)
{
cout << a[loop1]<< " ";
last_seen = a[loop1];
}


its only 'not print' the last value that was printed ..

example :
Data 1: 1
Data 2: 1
Data 3: 2
Data 4: 1
Data 5: 1
Data 6: 2
Data 7: 1
Data 8: 1
Data 9: 2
Data 10: 1

the number that show up are:
2 1 2 1 2
you see that the number "2" showed first instead of "1"
nevermind the repeating number in the result
You need to choose a suitable start value for last_seen.

You need to rememeber what you've printed and not repeat it.
and how ?
to remember and not repeat ?

i tried to sort it first ..
that way it will not repeat ..
but the last data (data 10) couldnt be printed .. dont know why

here you go

int main ()
{
int done;
int a[10];
for (int i=1;i<=10;i++)
{
cout << "\nData " << i << " :";
cin >> a[i];
}
cout << "\nthe number that show up are:\n";

int hold;
int size = 10;
for ( int s = 1; s <= size; s++ )
for ( int j = 1; j <= size; j++ )
if ( a[ j ] > a[ j + 1 ] )
{
hold = a[ j ];
a[ j ] = a[ j + 1 ];
a[ j + 1 ] = hold;
}
for ( int k = 1; k <= size; k++ )
cout << a[ k ] << " ";

for (int loop1 = 1; loop1 <= 10; loop1++)
if (a[loop1] != done)
{
cout << a[loop1]<< " ";
done = a[loop1];
}

return 0;
}

where should i fix it ?
You could save them in a conventional collection like another array.
Topic archived. No new replies allowed.