returning mode value from array

This is a discussion on returning mode value from array within the C++ Programming forums, part of the General Programming Boards category; I have an idea of how I want to tackle this but its hard for me to implement in code. ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    61

    returning mode value from array

    I have an idea of how I want to tackle this but its hard for me to implement in code. I want the function to check each element and if it spots another one just like it, to do an incrementation. I need a nudge. I'm not sure how to take that first step. Any help as always is appreciated.

    Code:
          #include <iostream>
    using namespace std;
    int mode(int a[],int size );//prototype
    int main()
    {
    
     
     const int size=10;
    int a[size]={1,2,-3,-4,-5,-6,7,8,-9,10};
    
    
     mode(a,size);//call
    
      return 0;
    
    }
    
    
     int mode(int a[], int size) //returns mode element of array
     {
        int m=0;
    	 for(int i=0;i<size;i++){
          if(i                               //????????
    		 a[i];
    	 }
    	 
    }

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>check each element and if it spots another one just like it, to do an incrementation.
    You mean check for duplicates? And what do you mean by "do an incrementation"? You mean if another element is the same as the one you're checking, make the other element 1 bigger?

    **EDIT**
    Judging from your function name, you're trying to find the element that has the biggest number of repetitions? Personally I would use a std::map to do it, but I assume you're not into that yet. You can create another array, which is used to store the counts of each element, and then print the element with the highest count:
    Code:
    int array1[10] = {1,2,2,3,4,5,5,5,6,1};
    int counts[10] = {0,0,0,0,0,0,0,0,0,0};
     
    //Count the repetitions of each element
    for(int i = 0; i < 10; ++i)
    {
       for(int j = 0; j < 10; ++j)
       {
    	 if(array1[j] == array1[i])
    		 ++counts[i];
       }
    }
     
    //Find the element with the highest count
    int highestIndex = 0;
    for(i = 1; i < 10; ++i)
    {
       if(counts[i] > counts[highestIndex])
    	 highestIndex = i;
    }
     
    cout << array1[highestIndex]; //print out the number with the most repetitions
    Last edited by Hunter2; 07-08-2004 at 09:59 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    I just mean I want the function to count how many times each element is present in the array. The element that has the highest count would be the mode which is what I want the function to return.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah yeah, I figured that out a second later... I edited my post, take a look at it and see if you can tell what it does (it's definitely not the most efficient way to do it, but it is one way).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Couldn't another way also involve using a switch statement?

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I don't believe so, although if you can figure something out that would be great.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Ok im almost done just keep getting this one error. grrrrr.
    Code:
    #include <iostream>
    using namespace std;
    int mode(int freq[],int a[],int size );//prototype
    int main()
    {
    
     
     const int size=10;
    int a[size]={1,2,-3,-4,-5,-6,7,8,-9,10};
    
    
     mode(freq,a,size);//call
    
      return 0;
    
    }
    
    
     int mode(int freq[], int a[], int size) //reverse order of elements in array
     {
    
    	 int largest=0;
    	 int modeValue=0;
    	 
    	 for(int i=0;i<size;i++)
    		 freq[i]=0; //initializing frequency to 0
     
    	  //summaring frequencies
    
    	 for (int j=0; j<size; j++)
    		 ++freq[a[j]];
    
    	 //output results
    
    	 for (int rating=1;rating<size;rating++){
    		 cout<<rating<<freq[rating]<<endl;
    
    		 if(freq[rating]>largest) {
    			 largest=freq[rating];
    			 modeValue=rating;
    		 }
           return modeValue;
    	 }
    
    
     }

    Ok i only have one error left to take care of which is this:
    Code:
    mode.cpp(17) : error C2065: 'freq' : undeclared identifier
    Not sure why i keep getting this, any help would be appreciated.
    Last edited by dantestwin; 07-08-2004 at 10:46 PM.

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Dantestwin - you seem to be posting a lot of threads like this. If I may teach a quick lesson in debugging:

    mode.cpp(17) - the file in which the error occurred (which you will need to know when you start building larger projects consisting of multiple files), and the line number. Look at the location specified here, as you will oftentimes notice that you just made a simple typo. It's really useful to concentrate on one area, and since the compiler tells you exactly where there's a problem, it's made easy.

    error C2065 - a lot of compilers distribute a list of error codes and what they mean. In this case, you've got a bit of a summary at the end.

    'freq' : undeclared identifier - just try and think about what this is trying to tell you, although it can be hard since it uses a lot of PC buzz words that newbies aren't likely to know. In this case, let me talk you through how I would do it. First of all, it's talking about 'freq', which you know is a variable that you made. "Undeclared identifier" - it's simply a variable that hasn't been declared properly. The actual cause for this could be that the variable has gone out of scope or wasn't declared.

    edit: To be specific, in this case you simply didn't declare or initialize freq, like you did for size, etc...

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    thanks sean, i fixed the bug now. It compiles fine and everything but there is one logical error. The biggest element in the array is 10 and it the program prints out the current frequency for every other element but 10. for some reason, it always adds 1. like if 10 occurred 3 times, it would print out 4. and so forth. any help as to what glitch in my code is sparking this error would be greatly appreciated.


    Code:
    #include <iostream>
    using namespace std;
    int mode(int freq[],int a[],int size );//prototype
    int main()
    {
    
     
     const int size=10;
    int a[size]={1,10,3,5,5,10,7,7,7,10};
    int freq[size]={0};
    
    cout<< mode(freq,a,size);//call
    
      return 0;
    
    }
    
    
     int mode(int freq[], int a[], int size) //reverse order of elements in array
     {
    
    	 int largest=0;
    	 int modeValue=0;
    	 
    	 for(int i=0;i<size;i++)
    		 freq[i]=0; //initializing frequency to 0
     
    	  //summarizing frequencies
    
    	 for (int j=0; j<size; j++)
    		 ++freq[a[j]];
    
    	 //output results
    
    	 for (int rating=1;rating<=size;rating++){
    		 cout<<rating<<' '<<freq[rating]<<endl;
    
    		 if(freq[rating]>largest) {
    			 largest=freq[rating];
    			 modeValue=rating;
    		 }
          
     
    	 }
           return modeValue;
        
     }

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    for (int rating=1;rating<=size;rating++){
    		 cout<<rating<<' '<<freq[rating]<<endl;
    
    		 if(freq[rating]>largest) {
    			 largest=freq[rating];
    			 modeValue=rating;
    		 }
    'rating' should loop from 0 to size-1. Here is the correct code:
    Code:
    for (int rating=0;rating<size;rating++){
    		 cout<<rating<<' '<<freq[rating]<<endl;
    
    		 if(freq[rating]>largest) {
    			 largest=freq[rating];
    			 modeValue=rating;
    		 }
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 06:48 AM
  2. Sensibility of returning a 2D array from a Class
    By goatslayer in forum C++ Programming
    Replies: 9
    Last Post: 09-08-2007, 08:23 PM
  3. problem returning array from function(among others)
    By Calef13 in forum C++ Programming
    Replies: 30
    Last Post: 10-30-2006, 04:26 PM
  4. returning an array from a function
    By BubbleBoy in forum C Programming
    Replies: 1
    Last Post: 02-20-2003, 12:41 PM
  5. returning an array of pointers to structures
    By dharh in forum C Programming
    Replies: 9
    Last Post: 02-06-2003, 03:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21