Simple Average Program w/Array

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!

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



    }

 
Have you debugged your code, i.e. set a breakpoint and stepped through it? If not, that should be first thing you do if you read the code and can't immediately see the issue. That way, you can predict what is going to happen before a step and then check whether what you predicted actually does happen. It generally becomes obvious where the code behaves other than as you expect. Even if you still can't resolve the issue, you can at least provide us with far more relevant information. Learn how to debug here:

https://msdn.microsoft.com/en-us/library/y740d9d3.aspx?f=255&MSPPError=-2147217396
 
Also, even without stepping through the code, the IDE will point you to the line of code on which an unhandled exception was thrown. If it's an IndexOutOfRangeException then the obvious first step is to find out what the index was and what the valid range was. You can possibly just mouse over the code to work that out but, if not, you've still got the Watch and Immediate windows in which to evaluate any expressions that will help you diagnose the issue. Don't ignore the debugger.
 
I'm sorry I should have said that I'm just using Notepad++! I did find the problem though - my array was referencing 100 but it only went up to 99. Changed that and it's working great! :) Thank you!
 
Back
Top Bottom