hello,

I've been pondering on a thing here for while now. The thing is that I want to read in several of integers, which I've fixed. And the problem now is that I want to output the integers I have input. The thing is that I only want to output unique numbers.

For example, I input:
1, 2, 3, 3, 4, 5, 1, 10, 9, 8

the output should be:
1, 2, 3, 4, 5, 10, 9, 8

-------------------------
Any tip would be helpful.

well this is quite easy and can be done in multiple ways;
the first is to use a bool array with as many integers as your highest input and toggle it to true if a number occurs, however, this is very inefficient. A better method would be to create another array with the total ammount of inputs you have and set them all to 0. Cycle through your inputs with a for loop and insert every unique number into the second array, checking it each to to see if it already has a number. Then just output your second array and it should be all unique numbers.'

int input[10], unique[10];
bool insert;

for(x = 0; x < 10; x++) {
cin>>input[x]; //input your numbers
unique[x] = 0; //set array to 0
}
for(x = 0; x < 10; x++) {
     insert = true;            //defaults flag to true
     for(y = 0; y < 10; y++)
          if(unique[x]==input[x])
             insert = false

     if(insert)
        unique[x] = input[x]; //insert if it is unique

}

//because this array is the same size as the ammount of your inputs, just cout a number if it is not equal to 0, which is your default

for(x = 0; x < 10; x++)
     if(unique[x] != 0)
       cout<<unique[x];

thanks, I was thinking something like that also. But I had it hard time to break it up where to start. Maybe I struggled to much with getting it work with pointers instead also... -p-

btw, that was an excercise from a book, and the solution they had for it was written like this. Could I bother you to explain if you know what the commented lines does?

#include <iostream>
using namespace std;

main()
{
  int tab[1000], *p = tab;
  cout << "skriv in ett antal heltal" << endl;
  while (cin >> *p++)
    ;
  
  p--;
  cout << "Talen är: " << endl;
  for (int *q = tab; q < p; q++)
  {
    int *r;
    for (r = tab; r < q && *r != *q; r++); // What does this for-loop do?
    if (r == q) // what does this if does?
      cout << *q << ' ';
  }
}

bleh i get it now, it is saying go make integer r go from the first element of your array to the current element, provided that none of the elements match (*r != *q), r will equal q when the loop breaks. Otherwise, r will be lower than q because of the && *r != *q; portion. *r would be my unique[x] and *q would be input[x]

Thanks,
Just a question shouldn't it be the opposite?
that *q is the unique[x], and *r be the input[x]?
Since *r = tab ?

Ah, yes you are correct, but the order of the two in my code are interchangeable in the if statements whereas your books snippet explicitly defines them, good catch :).

urgh, that code from the book is quite complicated I think. Anyway I was trying to get your solution to take ZERO as a number as well. The only idea how to implement it I could come to was to init the array with some high value e.g. 9999.
Then rewrite the if-statement which print the array check an interval of acceptable integers. It is not a pretty solution, but can I bother and ask how you would have done it? ^^;

Well again there are multiple ways to approach it, one would be "casting" or converting between variable types i.e in to char, but this gets much more complicated when you begin casting two digit numbers and up. Now normally, I would make a check for 0 as an input to prevent it from being used, but if you really insist on including 0, the simple way to do it is use -1 instead of 0. So are you going to ask me what to do if you want to have negative numbers? :p

Your book is quite creative in my opinion, the solution is much more intricate and efficient to be honest.

no need I think I got it down now, since init -1 would be like init the array with some other starting number ; )
well thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.