473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

some advice please

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
1 1561
D_C
293 100+
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

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

Similar topics

4
by: Bob | last post by:
Hallo, I have to make a web application in Javascript/ASP for tenniscourt reservation (based on Access database). I would like to do everything with one page, because the user must be able to...
4
by: Marquisha | last post by:
If this is off-topic, please forgive me. But I thought this might be the perfect spot to get some advice about how to proceed with a project. Working on a Web site design for a nonprofit...
1
by: Arun | last post by:
Hi..I am arun and Thanks to all of you in advance for solving my problem... I am working as a quality co-op for Maytag and I have developed a defect tracking database using access and VBA. It...
2
by: Brian Basquille | last post by:
Hello all. Air Hockey game is due up in just over a month. And i've about 2 and a half weeks to work on it. So, i need some feedback and advice. ...
15
by: vishsid3 | last post by:
Hi guys , I am new to this forum . can anybody plz tell me - "how can I run a c executable in some directory ,while the executable is in some other directory " I am new to unix and c ,so any...
39
by: Digital Puer | last post by:
I'm not the world's greatest C++ programmer, so I had a hard time with these. Some help would be appreciated. 1. Comment on the declaration of function Bar() below: class Foo { static int...
10
by: D | last post by:
Hi, I've written some C++ analysis / machine learning software which I think is pretty nifty. I am wondering what to do with it. I think it is worth some £, $ or euros. Also I wondering how to...
1
by: =?Utf-8?B?RW1tYSBIb3Bl?= | last post by:
Hi All, I need some advice please. I have very good knowledge of MS Access, Excel etc, reasonable knowledge of VBA and some very basic knowledge of VB6 and virtually non-existant knowledge of...
2
by: jon121970 | last post by:
I'm new here.. I am hoping to get some Professional Advice. I want to start my own online Auction. I anticipate it will grow into a high volume/high traffic online auction. I need some...
14
by: Byung-Hee HWANG | last post by:
Hi there, What is different between Ruby and Python? I am wondering what language is really mine for work. Somebody tell me Ruby is clean or Python is really easy! Anyway I will really make...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.