Thread: noob question

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    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

    thanks in advance

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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';
      }
    }
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    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
    Last edited by unclebob; 09-24-2006 at 08:16 AM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    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. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 09:28 PM