Unhandled Exception - And don't know why
-
:) I am getting a strange problem when I try to run a program for my C# class. I need to input 10 numbers each into an array, and use two arrays. Then I need to extract each of the numbers and their corresponding numbers in the second array. These two get added together and stored into the third array. I can get my numbers into the two arrays. When I press Enter, I end up getting a message that there was an exception and it wants to e-mail it to Microsoft. Below is my code for displaying the results. At one time I could at least, get it to do a Console Write Line. Can anyone see where I’m making my mistake? I am only looking to find out why it is giving me the error message, and trying to send it to Microsoft. Any help would be appreciated.
public static void DisplayNumbers(double anArray1, double anArray2, double anArray3) { string results = " "; int counter = 0; for (int x = 0; x < anArray3; x++) { anArray3 = anArray1 + anArray2; Console.WriteLine(); Console.WriteLine("{0}\t" + "+" + "\t{1}\t" + "=" + "\t{2}", anArray1, anArray2, anArray3); while (counter < 10) { counter++; results += "{0}\t" + " + " + "\t{1}\t" + " = " + "\t{2}" + anArray1 + anArray2 + anArray3; } string caption = "Array Methods Illustrated"; string outputMsg = "Array Numbers Shown\n\n" + "First\t" + "Second\t" + " " + " Sum\n"; MessageBox.Show(outputMsg, caption); }
This is what the exception looks like along with what is on line 18. :doh: “Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array at Exer4Chpt7.Program.main<> in G:\Programs\Exer4Chpt7\ Exer4Chpt7\program.cs: Line 18 Line 18: double[] anArray1 = new double[10]; From what I've been taught in class I figure that this should work. Thanks again. :-D -
:) I am getting a strange problem when I try to run a program for my C# class. I need to input 10 numbers each into an array, and use two arrays. Then I need to extract each of the numbers and their corresponding numbers in the second array. These two get added together and stored into the third array. I can get my numbers into the two arrays. When I press Enter, I end up getting a message that there was an exception and it wants to e-mail it to Microsoft. Below is my code for displaying the results. At one time I could at least, get it to do a Console Write Line. Can anyone see where I’m making my mistake? I am only looking to find out why it is giving me the error message, and trying to send it to Microsoft. Any help would be appreciated.
public static void DisplayNumbers(double anArray1, double anArray2, double anArray3) { string results = " "; int counter = 0; for (int x = 0; x < anArray3; x++) { anArray3 = anArray1 + anArray2; Console.WriteLine(); Console.WriteLine("{0}\t" + "+" + "\t{1}\t" + "=" + "\t{2}", anArray1, anArray2, anArray3); while (counter < 10) { counter++; results += "{0}\t" + " + " + "\t{1}\t" + " = " + "\t{2}" + anArray1 + anArray2 + anArray3; } string caption = "Array Methods Illustrated"; string outputMsg = "Array Numbers Shown\n\n" + "First\t" + "Second\t" + " " + " Sum\n"; MessageBox.Show(outputMsg, caption); }
This is what the exception looks like along with what is on line 18. :doh: “Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array at Exer4Chpt7.Program.main<> in G:\Programs\Exer4Chpt7\ Exer4Chpt7\program.cs: Line 18 Line 18: double[] anArray1 = new double[10]; From what I've been taught in class I figure that this should work. Thanks again. :-DJMOdom wrote:
public static void DisplayNumbers(double anArray1, double anArray2, double anArray3)
Your passing the datatype double, it should have been an array of type double
public static void DisplayNumbers(double[] anArray1, double[] anArray2, double[] anArray3)
JMOdom wrote:
for (int x = 0; x < anArray3; x++)
You need to pass the length of the array, since your looping from 0 till the end of the array. Since anArray1 would contain the list, you should be using anArray1.Length-1
for (int x = 0; x < anArray1.Length-1; x++)
JMOdom wrote:
anArray3 = anArray1 + anArray2;
While working with arrays you would have to use the index of the array to store or retrieve data.
= anArray1[x] + anArray2[x];JMOdom wrote:
Console.WriteLine("{0}\t" + "+" + "\t{1}\t" + "=" + "\t{2}", anArray1, anArray2, anArray3);
Same way over here you need to access the variables using the index of the array.
Console.WriteLine("{0}\t" + "+" + "\t{1}\t" + "=" + "\t{2}", anArray1[x], anArray2[x], anArray3[x]);
Since your calculating the value of anArray3 inside the function and its not a reference variable, you can just declare it inside the function. The logic your using is also not correct, I dont understand why you have used the while loop. Your code should effectively be
public void DisplayNumbers(double[] anArray1, double[] anArray2, double[] anArray3)
{
Console.WriteLine("Array Numbers Shown\n\n" + "First\t" + "Second\t" + " " + " Sum\n");
for (int x = 0; x < anArray3.Length; x++)
{
= anArray1[x] + anArray2[x];
Console.WriteLine();
Console.WriteLine("{0}\t" + "+" + "\t{1}\t" + "=" + "\t{2}", anArray1[x], anArray2[x], anArray3[x]);
}
}I suggest you read about arrays[^], follow the links on the left to learn more concepts and as always browse through the articles in code project.
-
JMOdom wrote:
public static void DisplayNumbers(double anArray1, double anArray2, double anArray3)
Your passing the datatype double, it should have been an array of type double
public static void DisplayNumbers(double[] anArray1, double[] anArray2, double[] anArray3)
JMOdom wrote:
for (int x = 0; x < anArray3; x++)
You need to pass the length of the array, since your looping from 0 till the end of the array. Since anArray1 would contain the list, you should be using anArray1.Length-1
for (int x = 0; x < anArray1.Length-1; x++)
JMOdom wrote:
anArray3 = anArray1 + anArray2;
While working with arrays you would have to use the index of the array to store or retrieve data.
= anArray1[x] + anArray2[x];JMOdom wrote:
Console.WriteLine("{0}\t" + "+" + "\t{1}\t" + "=" + "\t{2}", anArray1, anArray2, anArray3);
Same way over here you need to access the variables using the index of the array.
Console.WriteLine("{0}\t" + "+" + "\t{1}\t" + "=" + "\t{2}", anArray1[x], anArray2[x], anArray3[x]);
Since your calculating the value of anArray3 inside the function and its not a reference variable, you can just declare it inside the function. The logic your using is also not correct, I dont understand why you have used the while loop. Your code should effectively be
public void DisplayNumbers(double[] anArray1, double[] anArray2, double[] anArray3)
{
Console.WriteLine("Array Numbers Shown\n\n" + "First\t" + "Second\t" + " " + " Sum\n");
for (int x = 0; x < anArray3.Length; x++)
{
= anArray1[x] + anArray2[x];
Console.WriteLine();
Console.WriteLine("{0}\t" + "+" + "\t{1}\t" + "=" + "\t{2}", anArray1[x], anArray2[x], anArray3[x]);
}
}I suggest you read about arrays[^], follow the links on the left to learn more concepts and as always browse through the articles in code project.
I recommend to leran how to use the debug functions in the IDE. It is very useful that you can step through the code and discuver all errors and the actual behaviour of the application