-1

I want to find the largest cgpa which is only one cgpa but it's shows 2 or more when I enter same cgpa as input.

How do I find only one cgpa which is largest?

How do I find the exact answer?

      **I want to find the largest cgpa which is only one cgpa but it's shows 2 or more when I enter same cgpa as input.** 

How do I find only one cgpa which is largest?

input: Enter the number of student: 3

       Enter records for Student 1
       Enter Student ID : 2
       Enter student name: s
       Enter CGPA : 3

       Enter records for Student 2
       Enter Student ID : 201311
       Enter student name: A
       Enter CGPA : 4

       Enter records for Student 3
       Enter Student ID : 201312
       Enter student name: B
       Enter CGPA : 4

     output:
     201311 A has got the highest CGPA: 4.00
     201312 B has got the highest CGPA: 4.00

code


  #include<stdio.h>
  struct student
  {
  int ID;
  char name[50];
  float cgpa;
  };
  int main()
  {
 struct  student s[5000];
 int i,num;
 float large;
 printf("Enter the number of student: ");
 scanf("%d",&num);
 for(i=0; i<num; i++)
  {
    printf("Enter records for Student %d\n",i+1);
    printf("Enter Student ID : ");
    scanf("%d",&s[i].ID);
    getchar();
    printf("Enter student name: ");
    gets(s[i].name);
    printf("Enter CGPA : ");
    scanf("%f",&s[i].cgpa);
    getchar();
   }
  large=s[0].cgpa;
  for(i=0; i<num; i++)
   {
    if(s[i].cgpa>large)
    {
        large=s[i].cgpa;
    }
   }
   for(i=0; i<num; i++)
   {
    if(s[i].cgpa==large)
    {
        printf("%d %s has got the highest CGPA: %.2f\n",s[i].ID,s[i].name,s[i].cgpa );


     }
 }

}

  • 2
    Well both students have the same cgpa. So what exactly is the expected result in that case? If you only want to print the first one found then just add `break` or `return` after the `printf`. – kaylum Sep 15 '20 at 06:55
  • 2
    Start formatting your code properly. Poorly formatted code is hard to debug. – Jabberwocky Sep 15 '20 at 07:05
  • regarding: `gets(s[i].name);` `gets()` has been depreciated for years and completely removed from the C language for several years. Strongly suggest using: `fgets()`. syntax; `char *fgets(char *s, int size, FILE *stream);` description: *fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.* – user3629249 Sep 15 '20 at 07:19
  • Please see [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) (using `scanf()` for character arrays without a *field-width* modifier is just as bad) – David C. Rankin Sep 15 '20 at 07:57

3 Answers3

2

It's printing multiple answers when more than one is tied for the largest. However, if you want it to stop after printing the first one, all you need to do is add a break statement after the printf:

if(s[i].cgpa==large)
{
    printf("%d %s has got the highest CGPA: %.2f\n",s[i].ID,s[i].name,s[i].cgpa );
    break;
}
Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
0

If you want to print only the first one then use break or return just after the statement which is printing the output.

0

You don't need to compare them again after you've found the largest already.

This code below should do the trick.

#include<stdio.h>

struct student {
    int ID;
    char name[50];
    float cgpa;
};

int main() {
    struct student s[5000];
    int i, num;
    int largestIdx = 0;
    printf("Enter the number of student: ");
    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        printf("Enter records for Student %d\n", i + 1);
        printf("Enter Student ID : ");
        scanf("%d", &s[i].ID);
        getchar();
        printf("Enter student name: ");
        gets(s[i].name);
        printf("Enter CGPA : ");
        scanf("%f", &s[i].cgpa);
        getchar();
    }
    for (i = 1; i < num; i++) {
        if (s[i].cgpa > s[largestIdx].cgpa) {
            largestIdx = i;
        }
    }
    printf("%d %s has got the highest CGPA: %.2f\n", s[largestIdx].ID, s[largestIdx].name, s[largestIdx].cgpa);
}

And do remember: never compare equal with two float variables, you'll get surprised by the output of the following code..

float a = 3.3, b=3.0;
printf("a equals %f? %d\n", b+0.3, a==(b+0.3));
Jason
  • 81
  • 1
  • 4