0

This code is supposed to calculate the frequency of maximum number in an array I.E the number of times the highest number in the array has occured unfortunately this code does not display any output:-

#include<iostream>
#include <bits/stdc++.h>

using namespace std;

int birthdayCakeCandles(int n, int a[]){
int j=0,max,count=0;
max = a[j];
while(j<n){
if(a[j+1]> max){
    max = a[j+1];
    j++;
}

}
int seen[n];
for(int i = 0; i < n; i++)
    seen[i] = 0; 

for(int i = 0; i < n;i++) {
    if(seen[i] == 0) {
        int count = 0;
        for(int j = i; j < n;j++)
            if(a[j] == a[i] && a[i] == max)
                count += 1;
                seen[j] = 1;

    }
    }
return count;
} 

int main() {
int i,n;
cin >> n;
int a[n];
for(i = 0; i < n; i++){
   cin >> a[i];
}
int result = birthdayCakeCandles(n, a);
cout << result << endl;
return 0;
}
  • 5
    Variable length arrays are not allowed in C++ standard. You should [not include](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) the above `bits/stdc++.h` header. Stay away from the so called _competitive programming_ sites. – Ron Sep 12 '17 at 14:59
  • @Ron Competitive programming is fine for its own sake (people can and do make a competition out of anything, and this isn’t a bad thing). However, it’s not a good way to learn practical programming, or even competitive programming. – Daniel H Sep 12 '17 at 19:21
  • Also notice you read past the array when you do while(j max) If n is 5 for example, when j is 4 it'll do a[j + 1], which is the same as a[5], because arrays are zero indexed, this is actually the sixth element and one past the end of the array. – Zebrafish Sep 12 '17 at 20:13

2 Answers2

1

Your program never stops, because your maximum finding loop is for n > 0 endless. Your loop in birthdayCakeCandles should be changed to:

while (j < n)
{
    if (a[j + 1] > max)
    {
        max = a[j + 1];
    }

    j++;
}

Also consider using more readable coding style and please read this.

vasek
  • 2,759
  • 1
  • 26
  • 30
1

In addition to the bug found by vasek, you made at least another mistake in the (overcomplicated) following loops, where you are trying to count the occurences of the maximum value.

// I've kept OP's indentation on purpose... 
int seen[n];                 // <-- Variable Length Arrays are not standard in C++
for(int i = 0; i < n; i++)
    seen[i] = 0; 

for(int i = 0; i < n;i++) {
    if(seen[i] == 0) {
        int count = 0;
        for(int j = i; j < n;j++)
            if(a[j] == a[i] && a[i] == max)
                count += 1;
                seen[j] = 1;   // <-- misleading indentation, this is always executed
                               // no matter what the condition is 
    }
    }

While all you need to do, once you have found the maximum value, is:

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

As a matter of fact (unless you want to create a function operating on an array for other reasons), you don't need any array (or std::vector) at all to complete your assignment. This code will perform the same task:

#include <iostream>
#include <limits>

int main()
{

    int n;
    std::cin >> n;

    int x,
        max = std::numeric_limits<int>::min();

    int count = 0;

    for ( int i = 0;
          i < n  &&  std::cin >> x;
          ++i )
    {
        if ( x >= max )
        {
            if ( x > max )
            {
                max = x;
                count = 1;
            }
            else
            {
                ++count;
            }
        }
    }

    std::cout << count << '\n';
}
Bob__
  • 12,361
  • 3
  • 28
  • 42