Thread: mode in 2-D array

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    20

    Post mode in 2-D array

    Hi coders,,,
    I have to find mean,mode and median of a 2-D array :
    2 5 2 5 9 5 8 3 6 10
    8 1 10 10 7 1 4 4 3 8
    4 3 1 2 4 10 3 9 8 5
    6 10 8 3 6 10 5 1 2 5
    2 2 9 2 5 5 1 1 7 5
    7 9 9 2 1 5 2 2 2 4
    3 6 1 9 3 4 10 7 4 6
    7 10 4 6 2 10 10 8 7 6
    7 1 3 6 2 4 6 7 8 9
    8 5 9 2 3 2 1 5 1 8

    I coded for mean but stuck for mode.
    Code:
    // Assign4_Q2.cpp : Defines the entry point for the console application.
    //
    
    
    #include "stdafx.h"
    #include<iostream>
    #include<iomanip>
    #include<conio.h>
    using namespace std;
    
    
    int i,j;
    
    
    void calc_mean(int arr[10][10],int c)
    {
    
    
    
    
       double sum=0,out=0;
       for(i=0;i<=9;i++)
    		{   
    			for(j=0;j<=9;j++)
    			{
    			
    	            sum=sum+arr[i][j];
    			}
    
    
    		}
                 
                 out=sum/c;
    			 cout<< "Mean is:" <<out<<endl;
    
    
    
    
    
    
    }
    
    
    void calc_mode(int arr[10][10],int freq[][]{
    
    
    // stucked here
    }
    
    
    int main(int argc, _TCHAR* argv[])
    {
    	int count=0,mean;
    	int arr[10][10]={2,5,2,5,9,5,8,3,6,10,8,1,10,10,7,1,4,4,3,8,4,3,1,2,4,10,3,9,8,5,6,10,8,3,6,10,5,1,2,5,2,2,9,2,5,5,1,1,7,5,7,9,9,2,1,5,2,2,2,4,3,6,1,9,3,4,10,7,4,6,7,10,4,6,2,10,10,8,7,6,7,1,3,6,2,4,6,7,8,9,8,5,9,2,3,2,1,5,1,8};
    	int freq[10]={}; 
    	for(i=0;i<=9;i++)
    		{   
    			for(j=0;j<=9;j++)
    			{
    			
    				cout<<arr[i][j]<<",";
    
    
    				count++;
    			}
    
    
    	    }	
    	cout<<endl;
    	cout<<"Array size is"<<count<<endl;
    	calc_mean(arr,count);
    	calc_mode(arr,freq);
      
    	_getch();
    	return 0;
    }
    I used the following for mode part(by following the technique of 1-D array) which didn't work,,

    Code:
    for(int i=0;j<=9;++i)
        {
          for(j=0;j<=9;j++)
    	  {
    
    
          ++freq[arr[i][j]];
    
    
    	  }
        }
    
    
      cout<<"Array value"<<setw(17)<<"Mode"<<endl;
      for(int k=0;k<=9;k++)
      {
      
      cout<<setw(6)<<k<<setw(17)<<freq[k]<<endl;
      
      }

    Any Guidance will be Appreciated

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > I used the following for mode part(by following the technique of 1-D array) which didn't work,,
    Presumably because the largest value in your array is 10.

    Given your freq[10], this would be an array overrun.

    The idea is sound, so long as your freq array is large enough.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    Quote Originally Posted by Salem View Post
    > I used the following for mode part(by following the technique of 1-D array) which didn't work,,
    Presumably because the largest value in your array is 10.

    Given your freq[10], this would be an array overrun.

    The idea is sound, so long as your freq array is large enough.

    BUT its not returning me the Mode despite the correct size of freq[]

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Mode is the most common value in the array. This is the step you're missing.

    How did you work it out for a 1D array?

    Because searching a 2D array should make NO difference at all to the calculation.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    Quote Originally Posted by Salem View Post

    How did you work it out for a 1D array?

    Here's what i saw it for 1-D in ditel & deitel......
    I tried to modify it for 2-D in my above code...
    Code:
    int main()
    8 {
    9 // define arraysizes
    10 const int responseSize = 20; // size of array responses
    11 const int frequencySize = 6; // size of array frequency
    12
    13 // place surveyresponses in array responses
    14 const int responses[ responseSize] ={ 1, 2, 5, 4, 3, 5, 2, 1, 3,
    15 1, 4, 3, 3, 3, 2, 3, 3, 2, 2, 5 };
    16
    17 // initialize frequency counters to 0
    18  int frequency[frequencySize ]= {};
    19
    20 // foreachanswer,select responses element and use that value
    21 // as frequencysubscript to determine elementtoincrement
    22 for ( int answer = 0;answer<responseSize; ++answer )
    23   ++frequency[responses[answer]] ;
    24
    25 cout << "Rating" << setw( 17 )<< "Frequency" << endl;
    26
    27 // output each array element's value
    28 for ( int rating = 1;rating<frequencySize; ++rating )
    29 cout << setw( 6 )<<rating << setw( 17 )<<frequency[ rating ]
    30 << endl;
    31 } // end main
    Last edited by Javed Iqbal; 05-03-2014 at 08:56 AM.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    Sample Output:
    Rating Frequency
    1 3
    2 5
    3 7
    4 2
    5 3

  7. #7
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    In mine case for 2-D its returning the frequency of '0' for each value of 'k'

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Post some code that will compile (and we can run).

    Nothing you've posted so far suggests that it should produce those sorts of answers.
    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
    Jul 2013
    Location
    Germany
    Posts
    499
    to find the median you will need a nested loop and a counter.

  10. #10
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    #include <iostream>
    #include <vector>
    
    void mode(std::vector<int> v)
    {
        int mode=0;
        int count=0;
        int result=0;
        
        
        std::sort(v.begin(), v.end());
        
        for (int i=0; i<v.size(); i++)
        {
            while (v[i]==v[i+1])
            {
                count++;
                i++;
            }
            if (count > result)
            {
                result=count;
                mode=v[i];
            }
            count=0;
        }
        
        std::cout<<"The mode is "<<mode<<"\n";
        
    }
    
    int main(int argc, const char * argv[])
    {
    
       
        std::vector<int> numbers {10,10,10,10,44,33,4,4,4,2,5,3,6,4,1,3,7,7,3,4,7,4,7};
        
        mode(numbers);
        
        
        return 0;
    }
    I felt a little silly for not reading the other posts and just saying to use a loop and a counter. This is what I meant by that.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jocdrew21
    I felt a little silly for not reading the other posts and just saying to use a loop and a counter. This is what I meant by that.
    Note that you have an out of bounds access related to your use of v[i+1].
    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. Mode of an array
    By yacek in forum C Programming
    Replies: 14
    Last Post: 12-10-2010, 11:23 PM
  2. finding the mode in an array
    By cseter in forum C Programming
    Replies: 2
    Last Post: 03-20-2010, 01:25 PM
  3. Mode of an array
    By MV1 in forum C Programming
    Replies: 13
    Last Post: 08-05-2009, 10:46 AM
  4. finding mode of an array
    By n00by in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 07:40 PM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM