1

I'm trying to make a graph between instant charge against time, and I'm getting this error:

Index was outside the bounds of array

My code is below:

namespace WindowsFormsApplication19
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double max = 24000000, min = 23999999.85;
            double[] q = new double[9];
            int t = 0;
            for (t = 1; t <= 10; t++)
            {
                q[t] = (24 * Math.Pow(10, 6)) * Math.Exp(-t / (2000 * Math.Pow(10, 6)));
                chart1.Series[0].Points.AddXY(t, q[t]);
            }

            chart1.ChartAreas[0].AxisY.Maximum = max;
            chart1.ChartAreas[0].AxisY.Minimum = min;
            chart1.Series[0].ChartType = SeriesChartType.FastLine;
            chart1.Series[0].Color = Color.Red;
        }
    }
}

I tried this but am still having the same problem:

private void button1_Click(object sender, EventArgs e) {

    double[] q = new double[9];
    int t = 0;
    for (int i = 0; i <= 9; i++)
    {
        for (t = 1; t <= 10; i++)
        {
            q[i] = (24 * Math.Pow(10, 6)) * Math.Exp(-t / (2000 * Math.Pow(10, 6)));
            chart1.Series[0].Points.AddXY(t, q[i]);
        }
    }

}
1
  • 1
    Array indexes are 0 based. Start from 0 to 8. Note that q length is 9. So you should start from 0 to 8 NOT 1 to 10 Commented Nov 11, 2015 at 23:30

3 Answers 3

1

Inside the for loop it is much more standard to go from 0 to < q.Length.

That way you can change the length of your array and the for loop will still work.

        double[] q = new double[9];
        for (int t = 0; t < q.Length; t++)
        {
            q[t] = (24 * Math.Pow(10, 6)) * Math.Exp(-t / (2000 * Math.Pow(10, 6))); 
            chart1.Series[0].Points.AddXY(t, q[t]);               
        }
Sign up to request clarification or add additional context in comments.

Comments

0

Index out of bounds means that your code is trying to write to an index that is higher than the highest index.

You have 2 problems in your code that cause this - you need to fix both before it will work.

1. Fix q

In your code you have an array q which has 9 indexes, from 0 to 8, but it looks like you want 10: new double[10].

2. Fix the loop

In your for look you loop from 1 to 10, but arrays start with index 0 (you have 10 indexes, from 0 to 9). You need t to start with 0, not 1.

namespace WindowsFormsApplication19
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double max = 24000000, min = 23999999.85;
            double[] q = new double[10]; // Fix q: length of 10
            for (int i = 0; i < q.Length; i++) // Fix loop: start at 0
            {
                int t = i + 1; // Fix loop: t and i need to have different values
                q[i] = (24 * Math.Pow(10, 6)) * Math.Exp(-t / (2000 * Math.Pow(10, 6)));
                chart1.Series[0].Points.AddXY(t, q[i]);
            }

            chart1.ChartAreas[0].AxisY.Maximum = max;
            chart1.ChartAreas[0].AxisY.Minimum = min;
            chart1.Series[0].ChartType = SeriesChartType.FastLine;
            chart1.Series[0].Color = Color.Red;
        }
    }
}

11 Comments

thanks but still having an error of "Index was outside the bounds of array" at t = 10. graph is showing only value from t = 1 to 9 but at t= 10 it is showing an error
Have you forgotten to change one of the q[t] to q[i]? Or is t supposed to start at 0 as well?
no i've changed all q[t] to q[i] and t is not supposed to start at 0 it should start from 1 to 10
while debugging it i saw that it is showing error of " Index was outside the bounds of the array" in my formula at t = 10
Hm.. Not sure if this is your problem, but I see a mistake in my for loop. for (int i = 0; t < q.Length; i++), it should of course be for (int i = 0; i < q.Length; i++)
|
0

your array can contain 10 elements since:

double[] q = new double[9];

when your for loop hits 10; it means you want to reach q[10] so it will result in Index was outside the bounds of array error.

you shouldn't increment your loop index more than 9.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.