4 Replies - 5301 Views - Last Post: 17 February 2011 - 02:40 PM Rate Topic: -----

#1 plasticmonkey007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
                dblInput = Convert.ToDouble(Console.ReadLine());
                dblPrices[x] = dblInput;
                
                ++x;
            }
            x = 0;
           
            // calculating the sum of the prices in the array
            while(x <= 19)
            {
            dblTotal = dblTotal+dblPrices[x];
            }
            Console.WriteLine("Your Total is {0}.", dblTotal.ToString("C"));

            //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;
                }
            }



            Console.ReadKey();
        }
    }
}



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  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4454
  • View blog
  • Posts: 7,760
  • Joined: 08-June 10

Re: Index Out Of Range Exception was unhandled

Posted 17 February 2011 - 01:25 PM

(x <= 19)

is your problem.

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.
Was This Post Helpful? 1
  • +
  • -

#4 plasticmonkey007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4454
  • View blog
  • Posts: 7,760
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1