By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,288 Members | 3,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,288 IT Pros & Developers. It's quick & easy.

some advice please

P: 3
i have an excercise which i am having trouble with, its is regarding a vector array and searching for dupliacte entries

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <ios>
  5. #include <string>
  6. #include <vector>
  7. #include <iomanip>
  8.  
  9. int main()
  10. {
  11.     std::vector<double> numbers;
  12.  
  13.     double x;
  14.  
  15.     std::cout << "please enter a sequence of numbers" << std::endl;
  16.  
  17.     while(std::cin >> x)
  18.     {
  19.         numbers.push_back(x);
  20.     }
  21.  
  22.     sort(numbers.begin(), numbers.end());
  23.  
  24.     std::vector<double>::size_type size = numbers.size();
  25.  
  26.     int count = 0;
  27.  
  28.     for (int x=0; x!=size; ++x)
  29.     {
  30.  
  31.         if(numbers[x] == numbers[x+1])
  32.         {
  33.             ++count;
  34.             ++x;
  35.         }else{
  36.             ++x;
  37.         }
  38.     }
  39.  
  40.     std::cout << "the number of duplicate entries is "
  41.         << count << std::endl;
  42.  
  43.     return 0;
  44.  
  45. }
  46.  
  47.  
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 understand however 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

any help is much appreciated
Sep 24 '06 #1
Share this Question
Share on Google+
1 Reply


100+
P: 293
D_C
You need two loops. The first has 0 <= index1 < size-1, while the second has index1 < index2 < size.

Then compare if(array[index1] == array[index2]). Just one caution. Suppose you have 1,2,1,4,1,2,1 as input. There are 4 ones, 2 twos, and 1 four.

1 finds 3 duplicates
2 finds 1 duplicate
1 finds 2 duplicates
4 finds 0 duplicates
1 finds 1 duplicate
2 finds 0 duplicates
1 finds 0 duplicates // no numbers to compare against, it's hopeless

You could also do a square search instead of a triangular one. In that case, 0 <= index1, index2 < size. You still need two loops for that one, yet the bounds are the same. It check all entries against the current one, each one will get at least one match (when index1 == index2). Further, each number will report the same number of matches.
Sep 24 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.