Linked Questions

Popular Questions

Enter student grades, average them and then rank the student by grades

Asked by At

Sample input:

Student 1 Grade: 75
Student 2 Grade: 85
Student 3 Grade: 90
Student 4 Grade: 85
Student 5 Grade: 90

Sample output:

Top 1: Student 3
          Student 5
Top 2: Student 2
          Student 4
Top 3: Student 1

Scanner sc = new Scanner(System.in);
int[] grade = new int[5];
int[] gradeClone;

for(int i = 1; i <= 5; i++){
    System.out.print("Enter student " + i + " grade: ");
    grade[i-1] = sc.nextInt();
}

gradeClone = grade.clone();

int failed = 0;

for(int x = 1; x <= 5; x++){

    System.out.print("Student " + x + " Remark = ");

    if(grade[x-1] >= 60){
        System.out.println(" Passed");
    }else{
        System.out.println(" Failed");
        failed++;
    }

}

int total = 0;

for(int a = 0; a < 5; a++){
    total += grade[a];
}

int temp;

for(int j = 0; j < 5; j++){

    for(int k = 1; k < 5; k++){

        if(grade[k-1] < grade[k] ){
            temp = grade[k-1];
            grade[k-1] = grade[k];
            grade[k] = temp;
        }

    }

}
String ranking = failed <= 4 ? " then the ranking is: ": " all failed";
System.out.println("The average is " + total/5 + ranking ); 


int topCount = 5-failed;

for(int a = 1; a < 5; a++){
    if(gradeClone[a-1] == gradeClone[a]){
        --topCount;
    }
}

for(int i = 0; i < 5; i++){
    System.out.println(gradeClone[i]);
}

I can't come up of a way to rank them. How should I go about doing this?

Related Questions