Thread: Homework Help - Reinforce the use of data structures, finding highest score

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    27

    Homework Help - Reinforce the use of data structures, finding highest score

    We're writing a program in class using data structures and arrays and I'm stuck on writing a loop to find the students' highest test score. I only return the first student's ID, test score, and grade. For some reason unknown to me I'm not actually looping through all 12 or so records. Can you please advise? Thank you.

    Code:
    #include <stdio.h>
    #define MAX_ENTRIES 50
    
    struct records
    {    //declare my student records structure
        int ID;            //the student's ID number
        int test_score;        //the student's test score
        char grade;        //the student's letter grade of aforementioned test score
    };
    
    struct records high_ID_func (struct records students[], int count);
    int main (void)
    {
      struct records students[MAX_ENTRIES];
      struct records high_student;                /*the student with the highest test 
                                                      score*/
      int count;
      int i;
     high_student=high_ID_func(students, count);
         printf("Student %i received the high score of %i, %c\n", students[i].ID, students[i].test_score, students[i].grade);
        
      return 0;
    }    //end main
    
    struct records high_ID_func (struct records students[], int count)
    {
       int i = 0;            //use this to loop array elements
       int index = 0;
       float high_score;
    
    //Set up the highest test score
    high_score = students[0].test_score;
        while (i < count)
        {
            if( high_score < students[i].test_score )
            high_score = students[i].test_score;
            index = i;
               ++i;
        }
    
        return students[i];
    }    //end function to find the highest test score
    Here's the input data:
    1653
    77
    1945
    72
    1020
    50
    1955
    92
    1030
    40
    1200
    72
    1500
    91
    1100
    90
    1900
    81
    1005
    95
    1790
    43
    1800
    85
    0

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In "main()", what value does "count" have when you pass it to the function?

    In your "high_ID_func()", it looks like you have two lines of code under your "if()" that should be executed if the argument is true. However, since those lines of code are not enclosed in braces, then only the first line is executed if true.

    Additionally, in that function, since "i" is just your element "counting" variable and "index" seems to be the element where the highest score is found, don't you want to be returning "student[index]"?

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    Ooops, "count" was part of another of the functions in the whole program. I should have removed that for this question.

    Thank you for the help. I feel silly for not seeing that. Here's the amended code in case someone else has a similar issue:

    Code:
    #include <stdio.h>
    #define MAX_ENTRIES 50
     
    struct records
    {    //declare my student records structure
        int ID;            //the student's ID number
        int test_score;        //the student's test score
        char grade;        //the student's letter grade of aforementioned test score
    };
     
    struct records high_ID_func (struct records students[], int count);
    int main (void)
    {
      struct records students[MAX_ENTRIES];
      struct records high_student;                /*the student with the highest test 
                                                      score*/
      int i;
     students[index]=high_ID_func(students, count);
         printf("Student %i received the high score of %i, %c\n", students[index].ID, students[index].test_score, students[index].grade);
    
         
      return 0;
    }    //end main
     
    struct records high_ID_func (struct records students[], int count)
    {
       int i = 0;            //use this to loop array elements
       int index = 0;    //this will be my high_score element
       float high_score;
    
    //Set up the highest test score
    high_score = students[0].test_score;
        while (i < count)
        {
            if( high_score < students[i].test_score )
            {
              high_score = students[i].test_score;
              index = i;
            }
               ++i;
        }
    
        return students[index];
    }    //end function to find the highest test score
    Last edited by boygenuis; 04-16-2014 at 10:43 AM. Reason: I'm an idiot

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Why is "high_score" declared as a float, when "test_score" is an int?

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    No particular reason other than I was just playing around with formatting the output.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Ah, okay. Experimentation is good
    If you're interested, here's a good read: http://byuh.doncolton.com/courses/tut/printf.pdf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-25-2013, 06:45 PM
  2. Homework: Finding Next Day Using Structures
    By Cameron Taylor in forum C Programming
    Replies: 13
    Last Post: 07-21-2013, 09:15 PM
  3. Replies: 4
    Last Post: 04-09-2012, 05:44 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. finding highest average
    By Vontrapp in forum C Programming
    Replies: 6
    Last Post: 06-07-2010, 06:38 AM

Tags for this Thread