Thread: Trying to match the student's ID with the Highest Score along with the grade.

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    1

    Trying to match the student's ID with the Highest Score along with the grade.

    I am trying to correlate the student's highest score with that student's ID. The old code is in blue and the new code is in green which includes a sort. The particular information in question is in red.

    Any help would be nice,

    Ide1

    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
    
    
    /*Set up two arrays to include up to 50 elements each.*/
    
    int st_numval_id[50] = {0}, st_val_tstscr[50]= {0}, i = 0, j = 0, temp;
    
    int numels = 1, totalnumids = 0, sum_val_tstscrs = 0, above_ave = 0, index = 0, maxvalue = 0, maxvalue_stid = 0;
    
    int accum_A = 0, accum_B = 0, accum_C = 0, accum_D = 0, accum_F = 0;
    
    char grade[] = {'A', 'B', 'C', 'D', 'E', 'F'}, char_var = 0;
    
    float avetstscr = 0;
    
    
            printf("Enter the student's id and test score:\n\n");
            scanf("%i", &st_numval_id[numels]);
            
    
    while(st_numval_id[numels] != 0 )           /*The actual id number*/
    
        {
         
            scanf("%i", &st_val_tstscr[numels]);
    
    
            sum_val_tstscrs +=    st_val_tstscr[numels];
            
    
            if ( st_val_tstscr[numels] > 89 )
                {    char_var = 'A';
                    grade[numels] = char_var;
                    ++ accum_A;
                }
    
            else
             if ( st_val_tstscr[numels] > 79 && st_val_tstscr[numels] < 90 )
                {   char_var = 'B';
                    grade[numels] = char_var;
                    ++accum_B;
                }
    
            else
             if ( st_val_tstscr[numels] > 69 && st_val_tstscr[numels] < 80 )
                {    char_var = 'C';
                    grade[numels] = char_var;
                    ++accum_C;
    
                }
    
            else 
             if ( st_val_tstscr[numels] > 59 && st_val_tstscr[numels] < 70 )
                {    char_var = 'D';
                    grade[numels] = char_var;
                    ++accum_D;
                }
            
            else
                    if ( st_val_tstscr[numels] < 60 )
                {    char_var = 'F';
                    grade[numels] = char_var;
                    ++accum_F;
                }
                    
            
                    ++numels;
    
                    printf("\nEnter the student's id and test score:\n");
                    scanf("%i", &st_numval_id[numels]);
                    
              
    
        }    
            
        
            printf ("\n  Student ID          Test Score          Letter Grade \n");
            printf (" ------------         -----------         --------------\n");
            
        
        for (index = 1; index < numels ; ++index)    
        {
        
            printf ("     %i                 %i                  %c    \n",   st_numval_id[index], st_val_tstscr[index], grade[index] );
        }
            
        
            
             totalnumids = numels;
    
        if (totalnumids == 0)                                /* check for divide by zero */
            printf("Division by zero not allowed\n\n");
        
        else
        
            avetstscr  =  (float)sum_val_tstscrs / totalnumids;         /* type cast dividend to float */
            
    
    
            
                 for(i = 1; i < numels; ++i) 
                    
                {
                    if (st_val_tstscr[i] > avetstscr)
                        
                    ++above_ave;
                }        
    
           
        printf ("\n\n              ABOVE AVERAGE STUDENTS      \n\n");                    
            printf (" Ave Test Score    Students Above Ave   Number of: A's   B's   C's   D's   F's\n");
            printf ("  -----------          ----------       ---------  ---   ---   ---   ---   ---\n");
            printf ("      %.2f                 %i                       %i     %i     %i     %i     %i    \n",     avetstscr, above_ave, accum_A, accum_B, accum_C, accum_D, accum_F);
    
    
    maxvalue = st_val_tstscr[0];
    
            for(i = 1; i <= numels; ++i)
            {
                if(st_val_tstscr[i] > maxvalue)
                    maxvalue = st_val_tstscr[i];
             {
                if (maxvalue > 89)
                    char_var = 'A';
                else 
                    if(maxvalue > 79 && maxvalue < 90)
                        char_var = 'B';
                else
                    if(maxvalue > 69 && maxvalue < 80)
                        char_var = 'C';
                else 
                    if(maxvalue > 59 && maxvalue < 70)
                        char_var = 'D';
                else 
                    if(maxvalue < 60)
                        char_var = 'F';
            
                maxst_id = st_val_tstscr[i];
    
             }
            
            }
    
            printf ("\n    THE STUDENT WITH THE HIGHEST TEST SCORE      \n");                    
            printf ("  Student ID           Test Score          Letter Grade \n");
            printf ("  ---------            ----------          ------------\n");
            printf ("       %i                     %i                   %c    \n\n",   maxst_id, st_val_tstscr[i], char_var );
    
    
    return 0;
    }
    
    
    
    Enter the student's id and test score:
    
    1955
    92
    
    Enter the student's id and test score:
    1030
    40
    
    Enter the student's id and test score:
    1200
    72
    
    Enter the student's id and test score:
    0
    
      Student ID          Test Score          Letter Grade
     ------------         -----------         --------------
         1955                                92                            A
         1030                                40                             F
         1200                                72                              C
    
                          ABOVE AVERAGE STUDENTS
     Ave Test Score    Students Above Ave   Number of  A's   B's   C's   D's   F's
    
      -----------          ----------       ---------  ---   ---   ---   ---   ---
          51.00                             2                                          1       0       1      0       1
    
    
            THE STUDENT WITH THE HIGHEST TEST SCORE
      Student ID           Test Score          Letter Grade
      ---------            ----------          ------------
           0                                   0                                A
    
    
    
    
    
            for ( i = 0; i < numels-1; ++i)
                
                for( j = i +1; j < numels; ++j)
    
                    if(st_val_tstscr[i] > st_val_tstscr[j])
                        
                    {
    
                        temp = st_val_tstscr[i];
                        st_val_tstscr[i] = st_val_tstscr[j];
                        st_val_tstscr[j] = temp;
    
                        
                    }
    
    
                        maxvalue = st_val_tstscr[j];
                        maxvalue_stid = st_numval_id[j];
                        
                    
    
                    
            for( i = 0; i < numels; ++i)
            
                
             {
                if (maxvalue > 89)
                {
                    char_var = 'A';
                    maxvalue_stid = st_numval_id[i];
                }
    
                else 
                    if(maxvalue > 79 && maxvalue < 90)
                        {
                            char_var = 'B';
                            maxvalue_stid = st_numval_id[i];
                        }
                else
                    if(maxvalue > 69 && maxvalue < 80)
                        {
                            char_var = 'C';
                            maxvalue_stid = st_numval_id[i];
                        }
                else 
                    if(maxvalue > 59 && maxvalue < 70)
                        {
                            char_var = 'D';
                            maxvalue_stid = st_numval_id[i];
                        }        
                else 
                    if(maxvalue < 60)
                        {
                            char_var = 'F';
                            maxvalue_stid = st_numval_id[i];
                        }
                
    
             }
            
        
    
            printf ("\n    THE STUDENT WITH THE HIGHEST TEST SCORE      \n");                    
            printf ("  Student ID           Test Score          Letter Grade \n");
            printf ("  ---------            ----------          ------------\n");
            printf ("       %i                     %i                   %c    \n\n",   maxvalue_stid, maxvalue, char_var );
          
    
        return 0;
    }
    
    Enter the student's id and test score: /*After the following input, then enter 0 to end the input*/
    
    
    
      Student ID          Test Score          Letter Grade
     ------------         -----------         --------------
         1653                                77                              C
         1945                                 72                              C
         1020                                50                              F
         1955                                 92                               A
         1900                                 81                                B
    
    
                          ABOVE AVERAGE STUDENTS
    
     Ave Test Score    Students Above Ave   Number of: A's   B's   C's   D's   F's
      -----------          ----------       ---------  ---   ---   ---   ---   ---
          62.00                          4                      1       1        2        0      1
    
    
            THE STUDENT WITH THE HIGHEST TEST SCORE
      Student ID           Test Score          Letter Grade
      ---------            ----------          ------------
           1900                              0                               F





    Press any key to continue...

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Might just be me; but, I do NOT see a real clear question to help you with.
    I also suggest using multiple code sections and NOT using color in them.

    Tim S.
    Last edited by stahta01; 11-25-2013 at 06:49 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 11-16-2013, 06:18 PM
  2. Help with finding the highest average grade
    By Rockie in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2011, 11:55 AM
  3. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 06:24 PM
  4. Input int score then get grade???
    By chubbs1900 in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2007, 03:40 AM
  5. Student Grade Program
    By Vinod Menon in forum C Programming
    Replies: 1
    Last Post: 05-31-2004, 12:32 AM

Tags for this Thread