Thread: Fining the mode in a vector of numbers: Logic errors

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    32

    Fining the mode in a vector of numbers: Logic errors

    Hi,

    Could you please help point out the logic error in the following codes, & give a hint for fixing the error? This is an exercise for control loop and vectors, so I'm not allowed to use a function with an argument as a vector.

    Thanks a lot.

    Code:
    // Exercise 16 - Chapter 4
    // Control statements, if statements & vectors
    
    #include "std_lib_facilities.h"
    
    int main()
    {
    	int x = 0;
    	unsigned int i = 0;
    	unsigned int j = 0;
    	vector<int> numbers;
    	cout << "Enter a series of positive integers: ";
    	while (cin>>x) {
    		numbers.push_back(x);
    	}
    
    	cout << "You have entered: ";
    	for (i=0; i<numbers.size(); ++i)
    		cout << numbers[i] << ' ';
    	
    	sort(numbers.begin(), numbers.end());
    	cout << "\nIn ascending sorted order: ";
    	for (i=0; i<numbers.size(); ++i)
    		cout << numbers[i] << ' ';
    	
    	int prev_count = 0;
    	int curr_count = 0;
    	int mode = 0;
    	for (i=0; i<numbers.size(); ++i) { // for each element of the vector "numbers"
    		for (j=0; j<numbers.size(); ++j) { // compare it to all its other elements
    			if (numbers[j]==numbers[i]) // if 2 elements equal, increment the count (curr_count)
    				++curr_count;
    		}
    		if (curr_count>prev_count) { // if the current count greater than previous count
    			mode = numbers[i]; // the mode is set to equal that element of the vector
    			prev_count = curr_count; // set previous count == current count & repeat above steps
    			curr_count = 0; // set current count to zero after finishing the check on each of the elements
    		}
    	}
    	
    	cout << "The mode is " << mode << '\n';
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are not resetting curr_count to zero every time through the loop. You only set it to zero when that if statement evaluates to true.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    32
    Quote Originally Posted by bithub View Post
    You are not resetting curr_count to zero every time through the loop. You only set it to zero when that if statement evaluates to true.
    Thanks so much for your help! With the solution given, things look so much, much clearer. Before it, it took me days to work on this error, and I couldn't locate the logic error. Thanks a lot, buddy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Finding mode in array of numbers
    By Snowcat in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2006, 09:58 PM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM