I am getting index out of bound exception

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<pre class="prettyprint using System;

class GradeBook
{
private static int[,] grades;
public string CourseName { get; set; }

public GradeBook(string name, int[,] gradesArray)
{
CourseName = name;
grades = gradesArray;
}//end of constructor GradeBook

public void DisplayMessage()
{
Console.WriteLine("Welcome to GradeBook Course is: {0}", CourseName);
}//end method DisplaMessage

public void ProcessGrades()
{
OutputGrades();

Console.WriteLine("n{0} {1}n{2} {3}n", "Lower grade in the grade book", GetMinimum(), "Highest grade in the grade book", GetMaximum());

OutputBarChart();
}//end method ProcessGrades()

public int GetMinimum()
{
int lowGrade = grades[0, 0];

foreach (int grade in grades)
if (grade < lowGrade)
lowGrade = grade;

return lowGrade;
}//end metho GetMinimum

public int GetMaximum()
{
int highGrade = grades[0, 0];

foreach (int grade in grades)
if (grade > highGrade)
highGrade = grade;

return highGrade;
}//end method GetMaximum

public double GetAverage(int student){
int amount = grades.GetLength(student);
int total = 0;

for(int exam = 0; exam < amount; exam++)
<span style="text-decoration:underline total += grades[student, exam];

return (double)total / amount;
}//end GetAverage method

public void OutputBarChart()
{
Console.WriteLine("Overall grade distribution");

int[] frequency = new int[11];

foreach (int grade in grades)
++frequency[grade / 10];

for (int count = 0; count < frequency.Length; count++)
{
if (count == 10)
Console.Write(" 100: ");
else
Console.Write("{0:D2}-{0:D2): ", count * 10, count * 10 + 9);
for (int stars = 0; stars < frequency[count]; stars++)
Console.Write("+");

Console.WriteLine();
}//end for
}//end method OutputBarChart

public void OutputGrades()
{
Console.WriteLine("The Grades are:n ");
Console.Write(" ");

for (int test = 0; test < grades.GetLength(1); test++)
Console.Write("Test {0} ", test + 1);

Console.WriteLine("Average");

for (int student = 0; student < grades.GetLength(1); student++)
{
Console.Write("Student {0, 2}", student + 1);

for (int grade = 0; grade < grades.GetLength(1); grade++)
Console.Write("{0, 8}", grades[student, grade]);

Console.WriteLine("{0,9:F}", GetAverage(student));
}//end for
}//end method OutputGrades
}//end class GradeBook[/code]
I got Index out of bound exception as
System.IndexOutOfRangeException was unhandled<br/>
Message=Index was outside the bounds of the array.<br/>
Source=GradeBookUsingRectangularArray<br/>
StackTrace:<br/>
at GradeBook.GetAverage(Int32 student) in C:UsersHomeedocumentsvisual studio 2010ProjectsSudhirGradeBookUsingRectangularArrayGradeBookUsingRectangularArrayGradeBook.cs:line 55<br/>
at GradeBook.OutputGrades() in C:UsersHomeedocumentsvisual studio 2010ProjectsSudhirGradeBookUsingRectangularArrayGradeBookUsingRectangularArrayGradeBook.cs:line 99<br/>
at GradeBook.ProcessGrades() in C:UsersHomeedocumentsvisual studio 2010ProjectsSudhirGradeBookUsingRectangularArrayGradeBookUsingRectangularArrayGradeBook.cs:line 21<br/>
at GradeBookUsingRectangularArray.Program.Main(String[] args) in C:UsersHomeedocumentsvisual studio 2010ProjectsSudhirGradeBookUsingRectangularArrayGradeBookUsingRectangularArrayProgram.cs:line 26<br/>
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)<br/>
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)<br/>
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()<br/>
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)<br/>
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)<br/>
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)<br/>
at System.Threading.ThreadHelper.ThreadStart()<br/>
InnerException:
the line where the exception occured is
total += grades[student, exam];
in method <span style="text-decoration:underline GetAverage(int student)

I am unable to track down the error please help <hr class="sig --------| B.SUDHIR| --------|

View the full article
 
Back
Top