import java.io.*;
class q9
{
public static void main(String args[]) throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String n[]=new String[40];
int t[]=new int[5];
int temp, temp2;
for(int i=0;i<5;i++)
{
System.out.print("Enter the name of a student : ");
n[i]=in.readLine();
System.out.print("Enter total marks of the student : ");
t[i]=Integer.parseInt(in.readLine());
}
System.out.println("Name Marks");
for(int i=0;i<5;i++)
System.out.println(n[i]+" "+t[i]);
for(int i=0;i<4;i++)
{
for(int j=0;j<4-i;j++)
{
if (t[j]<t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
}
}
System.out.println("After sorting...");
System.out.println("Name Marks");
for(int i=0;i<5;i++)
System.out.println(n[i]+" "+t[i]);
}
}
2 replies to this topic
#1
Posted 06 February 2012 - 02:59 AM
I want to sort 5 students' marks in decending order. But, I'm unable to display marks according to their name after sorting. please help me to do
|
|
|
#2
Posted 06 February 2012 - 04:09 AM
Just swap the names as well after the marks:
temp = marks[j];
marks[j] = marks[j + 1];
marks[j + 1] = temp;
temp2 = names[j];
names[j] = names[j + 1];
names[j + 1] = temp2;
#3
Posted 06 February 2012 - 04:16 AM
Thnx for reply... done
wim DC said:
Just swap the names as well after the marks:
temp = marks[j]; marks[j] = marks[j + 1]; marks[j + 1] = temp; temp2 = names[j]; names[j] = names[j + 1]; names[j + 1] = temp2;
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









