# noob question

This is a discussion on noob question within the C++ Programming forums, part of the General Programming Boards category; can anyone help me with a small exercise i have been given from a book. i have to check if ...

1. ## noob question

can anyone help me with a small exercise i have been given from a book. i have to check if numbers enetered into an array have duplicate entries and the following is what i have come up with so far. it compiles and runs but has logic errors which i describe further down.

i know there is probably an easy way to complete this task with better algorithms or function calls however my c++ knowledge is limited so please if possible, offer advice that is simple and uses rudimentry c++ functions etc.

Code:
```#include <iostream>
#include <algorithm>
#include <ios>
#include <string>
#include <vector>
#include <iomanip>

int main()
{
std::vector<double> numbers;

double x;

std::cout << "please enter a sequence of numbers" << std::endl;

while(std::cin >> x)
{
numbers.push_back(x);
}

sort(numbers.begin(), numbers.end());

std::vector<double>::size_type size = numbers.size();

int count = 0;

for (int x=0; x!=size; ++x)
{

while(x!=numbers.size())

{

if numbers[0] == numbers[x]
[
++count;
++x;
}

else { ++x;
}

if(numbers[x] == numbers[x+1])
{
++count;
++x;
}else{
++x;
}

++x;

}

std::cout << "the number of duplicate entries is "
<< count << std::endl;

return 0;

}```

i am attempting to search the array and check if there a values that are the same for example if the input stream is

1, 2, 3, 3, 4, 5, 2

i hope to get 2 as an answer since the value 2 and 3 are entered twice.

i know that my code is wrong in 2 places but am having trouble on how best to move forward and overcome it.

the first problem if the if statement itself,

if(numbers[x] == numbers[x+1]

is not logical as only 2 numbers adjacent to each other would be compared and the first index would not be compared with an index 4 places down the line.

the other problem is an error that i recieve, its array out of bounds

the code above will compile though, with run time errors however

2. >please if possible, offer advice that is simple and uses rudimentry c++ functions etc.
Well, you're already using std::sort, so I would suggest taking it one step further and using std::unique as well:
Code:
```#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
std::vector<double> numbers;
double x;

std::cout<<"please enter a sequence of numbers" <<'\n';

while ( std::cin>> x )
numbers.push_back ( x );

sort ( numbers.begin(), numbers.end() );
std::vector<double>::iterator last = unique ( numbers.begin(), numbers.end() );

if ( last < numbers.end() ) {
std::cout<<"the number of duplicate entries is "
<< numbers.end() - last <<'\n';
}
}```

3. > if numbers[0] == numbers[x]
> [
This isn't even CLOSE to being correct syntax, so I don't know what you're compiling to get "the code above will compile though", because this isn't it.

I'm guessing you just typed this in from memory. It does't work like that, you have to post the exact code you last ran, not something dragged up from memory.

4. if its not too much to ask, could you please explain what the various function calls do?

i have a general idea, but would very much appreciate sa more descriptive reply

this post is in regard t prelude reply

sorry salem, i am in a library reading a bok, i actually try to do my asnwers on paper before using visual c++ and my college being cheap doesnt have visuall c++ in library

if u change the [ to { it should compile
cheers

5. >if its not too much to ask, could you please explain what the various function calls do?
Generally, we expect you to use one of the many available references to find out for yourself. However, since you're new to the forum, I'll refrain from telling you to google it. What std::unique does is take every subsequent duplicate value and copy it to the end of the sequence. When it's done, it returns the new end of the sequence, which if there are duplicates, will be less than the iterator returned by end(). So by comparing that iterator and end(), you can find out how many duplicates were copied out.

6. Well, you're already using std::sort, so I would suggest taking it one step further and using std::unique as well
We could change the sample data to:

1, 2, 3, 3, 4, 5, 2, 3

If the answer should be 3 (i.e. 2, 3, and 3), then what you propose is fine. If it should be 2 (i.e. 2 and 3), then I believe it will not work.

Another approach might be to use a std::map<double, int>.

7. >If it should be 2 (i.e. 2 and 3), then I believe it will not work.
Not if used as is. Of course, we don't do people's work for them, do we? We offer insight into the solution so that they're in a better position to solve it for themselves.

8. > i actually try to do my asnwers on paper before using visual c++
I suggest you try compiling some answers before posting.

Lots of desk work is a good idea just to make sure you have all the issues sorted out, but the compiler is a useful tool at checking for valid code, so use it.

9. hehe u are sneaky :P

i think i see what laser means

if the sequence of numbers contains an odd pairing, it will be a logic error

2, 3, 6, 2, 1, 5, 2, 5

if i understand what relude is saying this is what unique does

make a copy of the values appearing more than once? 2, 2, 5 in the above example

which if we compare would = 3 obviouslyy wrong when it should be 2.

i will have a look at the map feature u suggested, theres gotta be a book around here with some info

thanks

10. i will have a look at the map feature u suggested, theres gotta be a book around here with some info
Take a look at:
C/C++ Reference
SGI's STL Programmer's Guide