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

New Topic/Question
Reply




MultiQuote






|