Created
February 15, 2012 14:47
-
-
Save ChrisMoney/638615c180ea05603bf1 to your computer and use it in GitHub Desktop.
C# --Sort Students Console Program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
C# Program – Sort Students: | |
// Sort Students – This program demonstrates how to sort an array of objects | |
using System; | |
namespace Sort Students | |
{ | |
public static void Main(string[] args) | |
{ | |
// crate an array of students | |
Student[] = Student.NewStudent[5]; | |
students[0] = Student.NewStudent(“Homer”, 0); | |
students[1] = Student.NewStudent(“Lisa”, 4.0); | |
students[2] = Student.NewStudent(“Bart”, 2.0); | |
students[3] = Student.NewStudent(“Marge”, 3.0); | |
students[4] = Student.NewStudent(“Maggie”, 3.5); | |
// output the list as is: | |
Console.WriteLine(“Before sorting:”); | |
OutputStudentArray(students); | |
// now sort the list of students by grade (best grade first) | |
Console.WriteLine(“\nSorting the list\n”); | |
Student.Sort(students); | |
// display the resulting list | |
Console.WriteLine(“The students sorted by grade:”); | |
OutputStudentArray(students); | |
// wait for user to acknowledge the results | |
Console.WriteLine(“Press Enter to terminate…”); | |
Console.ReadLIne(); | |
} | |
// OutputStudentArray – display all the students in the array | |
pubic static void OutputStudentArray(Student[] students) | |
{ | |
foreach(Students in students) | |
{ | |
Console.WriteLine(s.GetString()); | |
} | |
}} | |
// Student – descriptin of a student with name and grade | |
class Student | |
{ | |
publc string sName; | |
public double dGrade = 0.0; | |
// NewStudent – return a new and initialized student object | |
public string sName; | |
public double dGrade = 0.0; | |
//NewStudent – return a ndw and initialized student object | |
public static Student NewStudent(string sName, double dGrade) | |
{ | |
Student student = new Student(); | |
student.sName = sName; | |
student.dGrade = dGrade; | |
return student; | |
} | |
// GetString – convert the current Student object into a string | |
public string GetString() | |
{ | |
string s = “”; | |
s + = dGrade; | |
s + = “-“; | |
s + = sName; | |
return s; | |
} | |
// Sort – sort an array of students in decreasing order of grade = use the bubble sort algorithm | |
public static void Sort(Student[] students) | |
{ | |
/bool bReapeatLoop; | |
// keep looping until the list is sorted | |
do | |
{ | |
// this flag is reset to true if an object is found out of order | |
bepeatLoop = false; | |
// loop through the list of students | |
for(int index = 0; index < (students.Length -1); index++) | |
{ | |
// if two of the students are in the wrong order… | |
if (students[index].dGrade <students[index +1].dGrade) | |
{ | |
// then swap them… | |
Student to = students[index]; | |
Student from = students[index +1]; | |
students[index] = from; | |
students[index +1] = to; | |
// keep looping until all objects are in order | |
bRepeatLoop = true; | |
}}} | |
while {bRepeatLoop); | |
}}} | |
Before Sorting: | |
0 - Homer | |
4 – Lisa | |
2 – Bart | |
3 – Marge | |
3.5 – Maggie | |
After Sorting | |
The students sorted by grade: | |
4 – Lisa | |
3.5 – Maggie | |
3 – Marge | |
2 – Bart | |
0 - Homer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment