Dec 1, 2009 at 11:54am UTC
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 !!
Dec 1, 2009 at 12:00pm UTC
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 ..
Dec 1, 2009 at 1:48pm UTC
So what would happen if it alternated between 4 and 2 multiple times kbw?
Dec 1, 2009 at 1:58pm UTC
Good point. You'll need to remember the values you've already processed.
Dec 1, 2009 at 2:00pm UTC
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
Dec 1, 2009 at 2:02pm UTC
you see that the number "2" showed first instead of "1"
nevermind the repeating number in the result
Dec 1, 2009 at 3:35pm UTC
You need to choose a suitable start value for last_seen
.
You need to rememeber what you've printed and not repeat it.
Dec 1, 2009 at 3:48pm UTC
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 ?
Dec 1, 2009 at 3:55pm UTC
You could save them in a conventional collection like another array.