HakerRank Write a Modular C Programming code to solve TOP ELEVEN, HELP THE CAPTAIN TO SELECT THE TOP 11 HIGHEST BATTING AVERAGE SCORED PLAYERS.
THE CAPTAIN OF INDIAN CRICKET TEAM WANTS TO SELECT THE TOP 11 OUT OF 20 PLAYERS.
CAPTAIN WANTS TO LIST PLAYERS ACCORDING TO THE HIGHEST TO LOWEST BATTING AVERAGE OBTAINED IN THE PREVIOUS CRICKET MATCHES.
HELP THE CAPTAIN TO SELECT THE TOP 11 HIGHEST BATTING AVERAGE SCORED PLAYERS.
MODULAR TASK:
Following functions to be defined:
- read_average() function
- display_average() function
- top_eleven() function
Input Format
Read value of N – number of players.
Read batting average scored by each player.
Constraints
N – number of players should be a minimum 11.
The batting Average scored by each player should be positive.
Output Format
Average of top 11 players, highest batting average first.
Sample Input 0
15
34.56 54.33 56.43 87.66 75.42 76.11 34.56 67.86 45.66 34.55 67.66 45.55 12.34 56.22 21.90
Sample Output 0
Top 11 batting average players:
87.66 76.11 75.42 67.86 67.66 56.43 56.22 54.33 45.66 45.55 34.56
Sample Input 1
5
Sample Output 1
Number of players should be atleast 11.
Refer : C Programming HackerRank all solutions for Loops | Arrays | strings
CODE:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> void read_average(float a[30],int n) { int i; for(i=0;i<n;i++) { scanf("%f",&a[i]); if(a[i]<0) { printf("Batting average should be positive\n"); exit(0); } } } void display_average(float a[30],int n) { int i,j; float b; for (i = 0; i < n; ++i) { for (j = i + 1; j< n; ++j) { if (a[i] < a[j]) { b = a[i]; a[i] = a[j]; a[j] = b; } } } printf("Top 11 batting average players:\n"); for (i=0;i<11;i++) { printf("%0.2f ",a[i]); } } int main() { int n; float a[30]; scanf("%d",&n); if(n<11) { printf("Number of players should be atleast 11.\n"); exit(0); } else { read_average(a,n); display_average(a,n); } return 0; }
OUTPUT
Congratulations, you passed the sample test case. Click the Submit Code button to run your code against all the test cases. Input (stdin) 15 34.56 54.33 56.43 87.66 75.42 76.11 34.56 67.86 45.66 34.55 67.66 45.55 12.34 56.22 21.90 Your Output (stdout) Top 11 batting average players: 87.66 76.11 75.42 67.86 67.66 56.43 56.22 54.33 45.66 45.55 34.56 Expected Output Top 11 batting average players: 87.66 76.11 75.42 67.86 67.66 56.43 56.22 54.33 45.66 45.55 34.56
Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:
Top 100+ C Programming codes – KLE Technological University