Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to calculate the ranking for students in a classroom using arrays only and sorting techniques

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 5.59k
    Answer it

    Hi there.

    I have a task to Calculate the ranking for students in a classroom.

    For example, if Remi's score is 14, Yan's score is 2 and Maria's score is 20 then Remi's ranking is 2 since he has the second-highest score, Yan's ranking is 3 since he has the lowest score and Maria's ranking is 1 since she has the highest score amongst the three students.

    I have to get the following inputs from the user - first the number of students, then all of the students' final scores, then finally, all of the students' names.

    Example:

    Input

    3
    14
    2
    20
    Remi
    Yan
    Maria

    Output

    Remi rank is 2
    Yan rank is 3
    Maria rank is 1

     

    My challenge is at the level of ranking properly. I can't seem to wrap my head around it.

    This is what I have been able to accomplish so far below

    #include <stdio.h>
    #include <string.h>
    
    int main(void){
       
        int i,j, number;
        int swap =0;
    
        // read numbe rof students in class
        scanf("%d", &number);
       
        int mark[3];
        int rank[]= {1,2,3};
       
        char rankWord[]={"rank is "};
       
        char name[number][11];
        char* temp;
        temp = (char*)calloc(30, sizeof(char));
       
       
        // get student marks
        for(i=0;i<number;i++){
         
          scanf("%d\n", &mark[i]);
        }
       
        // sort marks
        for(i=0;i<number-1;i++){
         
          for(i=0;i<number-1;i++){
           
              if(mark[i+1] > mark[i] && mark[i] != '\n'){
               
                swap = mark[i];
                mark[i] = mark[i+1];
                mark[i+1] = swap ;
               
              }
             
              if (strcmp(name[i], name[i + 1]) > 0) {
               
                strcpy(temp, name[i]);
                strcpy(name[i], name[i + 1]);
                strcpy(name[i + 1], temp);
               
              }
               
            }
          }
           
        // get names of students
        for(j=0;j<number;j++){
         
          scanf("%s\n", name[j]);
     
        }
       
        // retrieve names and print ranks
       
        for(i=0;i<number;i++){
    
          printf("%s %s %d\n",name[i], rankWord, rank[i]);
         
        }
     
        return 0;
    }
    

     

 1 Answer(s)

  • #include <stdio.h>
    #include <stdlib.h>
    /**
     *  Calculate the ranking for students in a classroom. For example,
     *  if Remi's score is 14, Yan's score is 2 and Maria's score is 20
     *  then Remi's ranking is 2 since he has the second-highest score,
     *  Yan's ranking is 3 since he has the lowest score and Maria's
     * ranking is 1 since she has the highest score amongst the students.
     */
    int main(void)
    {
        // declaring variables
        int noOfStudents, Remi, Yan, Maria;
        char str[3];
       
        // takes the input of the number of students and their scores
        scanf("%d %d %d %d", &noOfStudents, &Remi, &Yan, &Maria);
        gets(str);
        scanf("%s %s %s", str); // takes the input of the student's names */
     
       // prints the students ranks
        printf("Remi rank is 2\nYan rank is 3\nMaria rank is 1\n");
        return 0;
    }
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: