The problem
The problem: It crashes on me!!! The problem highlighted in red color, it tells me "Index was outside the bounds of the array", I know what it means, but i cant find the problem....
My Solution:
static void Main(string[] args)
{
School[] schools = new School[5];
string schoolName;
int schoolStudentAmount;
Console.WriteLine("Please enter information about 5 school: ");
for (int index = 0; index < 10; index++)
{
Console.WriteLine("Details for each school: ", index + 1);
Console.WriteLine(" Enter school name : ");
schoolName = Console.ReadLine();
Console.WriteLine("Enter amount of students in the school: ");
schoolStudentAmount = Convert.ToInt32(Console.ReadLine());
schools[index] = new School(schoolName,schoolStudentAmount);
}
Console.WriteLine("The schools and amount of students in it");
for (int index = 0; index < 10; index++)
Console.WriteLine(schools[index]);
Array.Sort(schools);
Console.WriteLine("The schools and enrolled students sorted from smallest to largest");
for (int index = 0; index < 6; index++)
Console.WriteLine(schools[index]);
====================
private string schoolName;
private int schoolStudentsAmount;
public string SchoolName
{
get { return schoolName; }
set { schoolName = value; }
}
public int SchoolStudentsAmount
{
get { return schoolStudentsAmount; }
set { schoolStudentsAmount = value; }
}
public School(string schoolName, int schoolStudentsAmount)
{
this.schoolName = schoolName;
this.schoolStudentsAmount = schoolStudentsAmount;
}
public override string ToString()
{
return String.Format("School name {0}, amount of students {1}.", schoolName, schoolStudentsAmount);
}
public int CompareTo(School other)
{
return this.schoolStudentsAmount.CompareTo(other.schoolStudentsAmount);
}
Loading
VulpesPosted Feb 4, 2012, 6:19 PM
VulpesPosted Feb 5, 2012, 5:49 AM
T is a type parameter which can represent any type. So, you can have Lists of strings, doubles, ints etc.
When writing a program, you simply replace T with whatever type you want your List to contain:
List
list.Add("Prime b");
As you say, a List automatically adjusts its size as new elements are added. It's therefore more versatile than an array though not quite as quick.
Prime bPosted Feb 4, 2012, 7:20 PM
VulpesPosted Feb 4, 2012, 7:12 PM
Prime bPosted Feb 4, 2012, 6:57 PM
Modify the program created in Exercise 8a so that after
the School objects are displayed in order, the program
prompts the user to enter a minimum enrollment fi gure.
Display all School objects that have an enrollment at
least as large as the entered value.
This is what I was thinking to do, but it won't allow me to use operator with type 'int' and 'SchoolDemo.School'.....
What you think vulpes?
for (int index = 0; index < schools.Length; index++)
{
if (userInput > schools[index])
{
}
}
Prime bPosted Feb 4, 2012, 6:54 PM
Prime bPosted Feb 4, 2012, 6:45 PM
Prime bPosted Feb 4, 2012, 4:26 PM