Thread: Get The Highest Score Using Struct

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    2

    Get The Highest Score Using Struct

    I am doing C programming after a long time and stuck into few basic things using struct and array. Though I've two requirements but the sample input and output as follows:

    Inputs:1) Id Score
    1002 100
    1004 200

    2) Id Score
    1002 100
    1004 100

    Outputs:


    1) Highest Scorer: 1004 200

    2) Highest Scorer: 1002 100
    Highest Scorer: 1004 100 //As both have the same score

    The two requirements are - 1) Two loops will be there, not nested 2) The first loop should store the highest score, then the second loop should check which matches the highest score. Pretty simple, so I did the following:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Student
    {
      int id;
      float score;
    } student1[2], dest[100];
    
    int
    main ()
    {
      int i, j, n, highest = 0;
      n = sizeof (student1) / sizeof (*student1);
      for (i = 0; i < n; ++i)
        {
          scanf ("%d %f", &student1[i].id, &student1[i].score);
        }
    
      for (i = 0; i < n; i++)
        {
          dest[i] = student1[i]; //Keeping the copy of array student1
        }
    
      //Tried to store the highest score here
      for (i = 0; i < n; i++)
        {
          if (dest[highest].score < dest[i].score)
        highest = i;
        }
    
      for (i = 0; i < n; i++)
        {
          if (dest[i].score = dest[highest].score) //Finally checking the highest score here
        printf ("Highest Scorer: %d %f\n", dest[highest].id,
            dest[highest].score);
        }
    
      return 0;
    }
    I've two things that doesn't match my criteria. The above code doesn't get me the second output given in the sample input two. When both the id has same score, it doesn't give any output. Secondly, Whenever I've the highest score, unfortunately the result is the following:

    Highest Scorer: 1004 200
    Highest Scorer: 1004 200
    Last edited by Salem; 04-24-2020 at 11:12 AM. Reason: Snipped crappy URL

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You don't need the dest array. Rather, make use of the first loop to find the highest score. Then, in the second loop, print all the records that have the highest score.

    You might want to store the score as an int instead of a float if the score isn't going to need to be floats. This will simplify checking the records to see if they have the highest score.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    2
    Hello laserlight! Thanks for your suggestion and I was able to make it work as you wrote. Here is the code that worked finally and let me know if I did it in the right way:

    Code:
    #include <stdio.h>
    
    //Array of struct 
    struct Student
    {
      int id, score;
    } student1[2];
     
    int main ()
    {
      int i, j, n, highest = 0;
      n = sizeof (student1) / sizeof (*student1); //No of elements
      
      //Iterate the inputs with the below loop
      for (i = 0; i < n; i++)
      {
         scanf ("%d %d", &student1[i].id, &student1[i].score);
          
         if(student1[i].score > student1[highest].score) //Get the highest score
                highest = i;
      }
     
      //Iterate the below loop to print the student id who has the highest score 
      for (j = 0; j < n; j++)
      {
          if (student1[j].score == student1[highest].score) //Finally checking the highest score here
          {
            printf ("Highest Scorer: %d %d\n", student1[j].id, student1[highest].score);
          }
      }
     
      return 0;
    }
    Last edited by Salem; 04-24-2020 at 11:11 AM. Reason: Snipped crappy URL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get Highest Value from an Array?
    By saashap in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2017, 08:56 PM
  2. Replies: 5
    Last Post: 04-16-2014, 11:09 AM
  3. Replies: 1
    Last Post: 11-25-2013, 06:45 PM
  4. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 06:24 PM
  5. highest int
    By dhuan in forum C++ Programming
    Replies: 2
    Last Post: 08-31-2010, 12:01 AM

Tags for this Thread