Ernesto Banderas
Ernesto Banderas

Reputation: 25

Counting occurrences of the largest number

I have been attempting to code for a program that stores input into an array and then allows me to print it out. It also lets me know which number is the largest. What I am trying to figure out is how can I get my program to tell me the amount of times (occurrences) the largest number in array is input. Here is my code so far. As of now, this code outputs the numbers I enter to the array, the largest element in the array, and the occurrence of every number I input( The occurrences of the numbers are incorrect). In all the the amount of occurrences for every number turns out to be 0. Which is obviously incorrect. Again, I need my program to display the largest number (which it does) and the occurrences of ONLY the largest number. All advice, tips, or thoughts are welcome. Thank you.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>



int main()
{
int arrayNum[15];
int a;
int max=0;
int location;


for( a=0; a < 15; a++)
    {   
        printf("Enter element %d:", a);
        scanf("%d",&arrayNum[a]);
    }

for(a=0; a < 15; a++)
    {
        printf("%d\n", arrayNum[a]);
    }

for (a = 1; a < 15; a++)
  {
    if (arrayNum[a] > max)
    {
       max  = arrayNum[a];
       location = a+1;
    }
    }
printf("Max element in the array in the location %d and its value %d\n", location, max);

for(a=0; a<15; a++)
    {
        if(arrayNum[a+1] == arrayNum[a])
            continue;
        else
            printf("Number %d: %d occurences\n", arrayNum[a]);
    }
return 0;





}

Upvotes: 0

Views: 8179

Answers (7)

Nishant reddy
Nishant reddy

Reputation: 1

A simple solution with time complexity of O(n)

int maxoccurence(int a[],int ar_size)
{
    int max=a[0],count=0,i;

    for(i=0;i<ar_size;i++)
    {
        if(a[i]==max)//counting the occurrence of maximum element 

        count++;

        if(a[i]>max)//finding maximum number
        {
            max=a[i];

            count=1;
        }
    }

    printf("Maximum element in the array is %d\n",max);

    return count;
}

Upvotes: -1

dreamcrash
dreamcrash

Reputation: 51493

You can do what you want in just one loop iteration:

int count = 1;
int position = 0;
int max = arrayNum[0];
int N = 15;
int p;

for (p = 1; p < N; ++p)
{
    if (arrayNum[p] > max) // Find a bigger number
    {
       max  = arrayNum[p]; 
       pos = p;
       count = 1;
    }
    else if ( arrayNum[p] == max) // Another occurrences of the same number
          count++;
}

Upvotes: 1

Gopi
Gopi

Reputation: 19874

Just before you begin the below loop max is still 0 make

  max = a[0];

  for (a = 1; a < 15; a++)
  {
    if (arrayNum[a] > max)
    {
       max  = arrayNum[a];
       location = a+1;
    }
  }

Later

int n=0;
for(i=0;i<15;i++)
{
   if(max == a[i])
   n++;
}

printf("Number of times max appears in the array is %d\n",n);

Upvotes: 1

Rob
Rob

Reputation: 1974

All you need to do is introduce a new variable to keep track of the number of occurrences of max. When a new value of max is found, set that count to zero. When a subsequent value is found equal to the max, increment the counter.

Incidentally, your code doesn't properly find the maximum in its current form. Try one test case where your array elements are all negative. Try another test case in which all the values are positive, and the first value entered (arrayNum[0]) is the maximum. You will find, in both cases, that your function will not actually find the maximum.

Upvotes: 1

Diego Sevilla
Diego Sevilla

Reputation: 29021

I spot some problems in your code. First, the third for loop starts at 1, but it does not update the max as the value of arrayNum[0].

Then, for the problem at hand, I would have two variables:

int max; // The maximum value
int max_count; // The count of the maximum value

Then, the logic to find the greatest, and the count, is the following:

For each element, compare it with the maximum seen. If it is equal, increment max_count. If it is bigger, update max with the value, and set the max_count to 1. If it is smaller, ignore it. Something like:

max = arrayNum[0];
max_count = 1;
for (int a = 1; a < 15; ++a)
{
    if (arrayNum[a] == max)
       max_count++;
    else if (arrayNum[a] > max)
    {
        max_count = 1;
        max = arrayNum[a];
    }
}

Upvotes: 1

kinezana
kinezana

Reputation: 83

For your third for-loop, the one where you find out the largest number in your array, I would suggest to set max to arrayNum[0], that way it will work even with negative numbers.

Then, to know how many occurrence of the highest number there is, you need a count variable that you increment (count++) each time a number of the array is equal to max. To do that you need another for-loop.

Good luck.

Upvotes: 0

Vagish
Vagish

Reputation: 2547

Replace last for loop with below code

NoOfOccurances = 0;
for(a=0; a<15; a++)
    {
        if(max == arrayNum[a])
        {
             NoOfOccurances++;
        }  

    }

 printf("Number %d: %d occurences\n", max,NoOfOccurances);

Upvotes: 0

Related Questions