Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Sort A List : Print the sorted list of ages of the students
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public class Student { public string Name { get; set; } public int Age {get; set;} } public static List<Student> StudentList; public static void Main(string[] args) { //Prepare the list of students LoadStudents(); //Create an array of 100 elements int[] studentCount = new int[100]; //Each element represents an age category //Ex. First element will contain the count of students who are 1 year old //Ex. 100th element will contain the count of students who are 100 years old //Loop through the list of students to count the students per age value and update the array foreach(Student l in StudentList){ //Get the index in the array that represents the age of the student int index = l.Age - 1; //Update the value of that element by incrementing the value by 1 studentCount[index] = studentCount[index] + 1; } //Print a sorted list of all students ages int currentAge = 0; foreach(int i in studentCount) { //Determine the age represented by the current element currentAge++; //If the value of the element is not 0, then print Age as many times as stated in the value //If in the 5th element the value is 3, then that means there are 3 students who are aged 5 //So we print "5" three times if(i!=0) for(int r=0; r < i; r++) Console.WriteLine(currentAge); } } public static void LoadStudents() { Student s = new Student(){ Name = "Ivy", Age = 33}; Student s1 = new Student(){ Name = "Ryan", Age = 34}; Student s2 = new Student(){ Name = "Ron", Age = 23}; Student s3 = new Student(){ Name = "Melvin", Age = 2}; Student s4 = new Student(){ Name = "Sara", Age = 33}; Student s5 = new Student(){ Name = "Mars", Age = 43}; Student s6 = new Student(){ Name = "Pluto", Age = 50}; Student s7 = new Student(){ Name = "Neptune", Age = 6}; Student s8 = new Student(){ Name = "Nero", Age = 5}; Student s9 = new Student(){ Name = "Augustus", Age = 34}; StudentList = new List<Student>(); StudentList.Add(s); StudentList.Add(s1); StudentList.Add(s2); StudentList.Add(s3); StudentList.Add(s4); StudentList.Add(s5); StudentList.Add(s6); StudentList.Add(s7); StudentList.Add(s8); StudentList.Add(s9); } } }
run
|
edit
|
history
|
help
0
Convert string to int
SingleDelegate
un poco de strings
General.cs
Test equality...2
nba
DateTime
c# kare çizimi
dfg
Struct Ejemplo