c++ - The following Code does not give any response on Stdout -
this code supposed calculate frequency of maximum number in array i.e number of times highest number in array has occured unfortunately code not display 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 = 0; < n; i++) seen[i] = 0; for(int = 0; < 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; < n; i++){ cin >> a[i]; } int result = birthdaycakecandles(n, a); cout << result << endl; return 0; }
your program never stops, because maximum finding loop n > 0 endless. loop in birthdaycakecandles should changed to:
while (j < n) { if (a[j + 1] > max) { max = a[j + 1]; } j++; } also consider using more readable coding style , please read this.
Comments
Post a Comment