-
NEED HELP WITH THIS
Write a program that inputs 5 student's names and grades (into two arrays), sorts the two arrays on the basis of the grade (highest to lowest), and then prints out the names and grades sorted this way.
//arrays for names
String names []= new String[5];
System.out.println ("enter the name of 5 students");
for(int number=0; number<5;number++)
names[number] =MyInput.readString();
//arrays for grades
int grades []= new int[5];
System.out.println ("enter thegrades for the 5 students");
for(int number=0; number<5;number++)
grades[number] =MyInput.readInt();
selectionSort(grades);
System.out.println("Students " + "Grades");
for (int number=0; number<=4;number++)
System.out.println(names[number]+" "+grades[number]);
}
//method for sorting the numbers
static void selectionSort(int mix[])
{
int Max1;
int Max2;
for (int i=mix.length-1; i>=1; i--)
{
// the maximum in the list[0..i]
Max1 = mix[i];
Max2 = i;
for (int j=i-1; j>=0; j--)
{
if (Max1 > mix[j])
{
Max1 = mix[j];
Max2 = j;
}
}
// Swap list[i] with list[Max2] if necessary;
if (Max2 != i)
{
mix[Max2] = mix[i];
mix[i] = Max1;
}
}
}
}
================================
I enter
Students Grades
1-alex 10
2-maria 20
3-john 30
4-felix 40
5-tria 50
and i get
Students Grades
1-alex 50
2-maria 40
3-john 30
4-felix 20
5-tria 10
this is wrong becasue its suppost to be
Students Grades
1-tria 50
2-felix 40
3-john 30
4-maria 20
5-alex 10
how can i fix this
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks