Thread: Finding the most occurring element in column 1 of a 2-d array

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    6

    Finding the most occurring element in column 1 of a 2-d array

    This is for a 2-d array, but if i understand how to do it for a 1-d array, I can do it for a 2-d. So to keep it simpler, I will just pretend its for a 1-d array. I am working on a project where i must enter the month number of a storm and then tell the user which month had the most storms. So I have an array and the user determines the size of it by entering the number of storms. Then, using a for loop, the user enters the month number for each storm and the data is used to fill the array. This is to be used in a part of a much larger piece of code. I made a sample code below to mimic what i want to do:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    
    
    int main(int argc, char *argv[]) {
        
        /*Declare variables, i is a counting index used in for loop if necessary*/
        int i;
        int nStorms=10;
        
        /*Here is an array with pre-made data, where October(10) has the most storms*/
        int monthArray[nStorms]= {10, 10, 9, 11, 10, 9, 6, 6, 8, 7};
        
        /*Now, I need some commands to display back to the user "The month with the most storms is: 10"*/
        
        
        system ("PAUSE");
        return 0;
    }
    I believe this involves making some temporary array but i have no idea where to start
    Last edited by Al3x_Payn3; 10-07-2014 at 02:13 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Since the possible month values are limited to a small range (1-12), you can simply have an array of NUM_MONTHS elements that stores the count of those months directly. Initialize it to 0, and when the user enters a month, increment that spot in the array. Then, you simply have a "find the max" problem, which you can probably handle.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    Ah, good idea. Thats what I did and it works great. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-04-2014, 10:09 AM
  2. problems finding the average of an array column
    By mrgeoff in forum C Programming
    Replies: 4
    Last Post: 04-18-2005, 11:49 PM
  3. Finding largest element in array
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 09:18 PM
  4. finding the element number in an array
    By tommy69 in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 04:26 AM
  5. Finding greatest element of an array
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-10-2001, 04:30 PM