JadeRae
New member
- Joined
- May 9, 2018
- Messages
- 2
- Programming Experience
- 1-3
Hello!
I am very new to C# and am trying to improve my skills with arrays by creating a simple program which asks for input from the user, stores it in an array, and then outputs the average of all numbers.
My program compiles, but I'm running into an error after my loop runs the first time: System.IndexOutOfRangeException error saying that the 'Index was outside the bounds of the array at sumNumbersArray.Main()'
My program is below, if anyone would mind taking a look at it and giving me some input I would really appreciate it!
I am very new to C# and am trying to improve my skills with arrays by creating a simple program which asks for input from the user, stores it in an array, and then outputs the average of all numbers.
My program compiles, but I'm running into an error after my loop runs the first time: System.IndexOutOfRangeException error saying that the 'Index was outside the bounds of the array at sumNumbersArray.Main()'
My program is below, if anyone would mind taking a look at it and giving me some input I would really appreciate it!
C#:
//Assignment 1-9//Create an array with room for 100 whole numbers. Ask users for numbers until array is full or if user enters '999'.
//After leaving array creation user should be presented with the average of all numbers entered.
using System;
class sumNumbersArray
{
public static void Main()
{
int [] averageArray = new int [100];
int inputInt, count=0, total=0;
string input;
Console.Out.Write("Welcome to the Average Spitter. Circa 1990. ");
Console.Out.Write("\nI can hold up to 100 numbers. \nWhen you are ready to see your average please enter 999. ");
do
{
Console.Out.Write("\n\nPlease enter a number: ");
input = Console.ReadLine();
inputInt = Convert.ToInt32(input);
averageArray[count]=inputInt;
total = total + inputInt;
count = count + 1;
Console.Out.Write("You have entered " + count + " number(s).");
} while (inputInt!=999 && averageArray[100]==0);
Console.Out.Write("You have exited the Average Spitter. Press any key to see the average of your numbers. ");
Console.ReadLine();
Console.Out.Write("The average of numbers entered is " + averageSpitter(count,total) +". ");
Console.Out.Write("/nThanks for using Average Spitter! ");
}
public static int averageSpitter(int count, int total)
{
int myAverage;
myAverage = total/count;
return myAverage;
}
}