T
The Conqueror
Guest
Define a structure called c ricket the will describe the following information -
i) Player's name, ii) Country name, iii) Batting average.
Write a program that will store information of 25 players using this structure, also arrange and display names of these players in descending order with respect to batting average.
Here is my code :
This is my code. Is there a way I can optimize it further? I lost 4 marks in class test because I did not use "gets()" function for string input and string.h library function. But it does work fine without both of them. I've read on forums that "gets" function is dangerous. So I'm confused what to do? Will using this program affect my grade in the University exams? (University of Mumbai)?
i) Player's name, ii) Country name, iii) Batting average.
Write a program that will store information of 25 players using this structure, also arrange and display names of these players in descending order with respect to batting average.
Here is my code :
Code:
#include <stdio.h>
#include <conio.h>
int main()
{
struct cricket
{
char name[25];
char country[25];
int bestscore;
float average;
};
struct cricket s[25],t;
int i; int j;
int n=25;
for(i=0; i<n ; i++)
{
printf("\n Enter Player's Name");
scanf("%s", s[i].name);
printf("\n Enter Country");
scanf("%s", s[i].country);
printf("\n Enter Best Score");
scanf("%d", &s[i].bestscore);
printf("\n Enter Batting Average");
scanf("%f", &s[i].average);
}
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(s[i].average<s[j].average)
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
printf("Names of players in DESCENDING order w.r.t batting averages \n\n");
for(i=0; i<n; i++)
{
printf("Name of Player : %s \t", s[i].name);
printf("Batting Average : %f \n", s[i].average);
}
getch();
return 0;
}
This is my code. Is there a way I can optimize it further? I lost 4 marks in class test because I did not use "gets()" function for string input and string.h library function. But it does work fine without both of them. I've read on forums that "gets" function is dangerous. So I'm confused what to do? Will using this program affect my grade in the University exams? (University of Mumbai)?