# Index Out Of Range Exception was unhandled

Page 1 of 1

## 4 Replies - 9675 Views - Last Post: 17 February 2011 - 02:40 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=216877&amp;s=de078af65cacfa1de072fb85383ea541&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 plasticmonkey007

Reputation: 0
• Posts: 19
• Joined: 17-February 11

# Index Out Of Range Exception was unhandled

Posted 17 February 2011 - 01:20 PM

Hello,

This is my first post here. I am working on a program that has to do the following..

Create an array that stores 20 prices. Prompt a user to enter 20 values, then display the
sum of the values. Next, display all values of less than \$5.00. Finally, calculate the average
of the prices, and display all values that are higher than the calculated average.

Here is what i have so far.

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TrollCh5_8Prices
{
class Program
{
static void Main(string[] args)
{
//Declare Variables

double[] dblPrices = new double[19];
int x = 0;
double dblTotal=0.00,dblInput,dblAverage;

while (x <= 19)
{
Console.Write("Enter the price for #{0}: ", x);
dblPrices[x] = dblInput;

++x;
}
x = 0;

// calculating the sum of the prices in the array
while(x <= 19)
{
dblTotal = dblTotal+dblPrices[x];
}

//Displaying all prices of less than 5.00
x = 0;
while (x <= 19)
{
if (dblPrices[x] <= 5)
{
Console.WriteLine("{0}", dblPrices[x].ToString("C"));
}
++x;
}
x=0;

while (x <= 19)
{
dblAverage = dblTotal / 19;
Console.WriteLine("The average of the Prices is {0}", dblAverage);
if (dblPrices[x] < dblAverage)
{
Console.WriteLine("{0}", dblPrices[x].ToString("C"));
++x;
}
}

}
}
}

```

When i run the code through the debugger in Visual Studio 2010 i get the error "Index Out Of Range Exception was unhandled"
on line 23
I have played with the code and adjusted things and i cant get it to work properly. I just know its something i am overlooking
but its been a long day and Ive been so frustrated.
A friend suggested i post the code here to get some help so i thought i would try it.

Any and all assistance is so greatly appreciated!

Thank you,
Plasticmonkey007

Is This A Good Question/Topic? 0

## Replies To: Index Out Of Range Exception was unhandled

### #2 Kilorn

• XNArchitect

Reputation: 1361
• Posts: 3,534
• Joined: 03-May 10

## Re: Index Out Of Range Exception was unhandled

Posted 17 February 2011 - 01:24 PM

Declare your array as having 20 elements, not 19. Currently, counting from 0 which arrays do, you have 0 through 18, and your while loop checks for anything less than or equal to 19. Once it hits this 19, the error is thrown.

EDIT: Specifically, change line 19 to double[] dblPrices = new double[20];

This post has been edited by Kilorn: 17 February 2011 - 01:24 PM

### #3 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5102
• Posts: 9,283
• Joined: 08-June 10

## Re: Index Out Of Range Exception was unhandled

Posted 17 February 2011 - 01:25 PM

```(x <= 19)
```

Arrays are indexed starting at zero, but their lengths are counted normally. So:

```double[] list = new double[19];
```

Would actually result in an array with 19 values. If you want a 20 value array, you have to use double[20].

But that means the valid indexes (indices?) for that array are 0 - 19.

The most common way you'll see people do this would be like this:

```int arraySize = 20;
double[] list = new double[arraySize];
...
for(int i=0; i < arraySize; i++){
//do for loop stuff here
}
```

On that note, I'd also suggest using For loops instead of While loops, since they make more sense in the context of arrays.

### #4 plasticmonkey007

Reputation: 0
• Posts: 19
• Joined: 17-February 11

## Re: Index Out Of Range Exception was unhandled

Posted 17 February 2011 - 01:39 PM

Oh Wow that was fast! and so helpful too.
I am glad that you all didn't just post the answer but to give a little info so that i could learn as well

Thank you so very much!

### #5 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5102
• Posts: 9,283
• Joined: 08-June 10

## Re: Index Out Of Range Exception was unhandled

Posted 17 February 2011 - 02:40 PM

That's the way we roll here. We actually look down on giving students answers. Our goal is to help you become a good programmer, not just to give you a quick answer.