Results 1 to 5 of 5

Thread: Simple Average Program w/Array

  1. #1
    .NET Framework
    .NET 4.0
    Join Date
    May 2018
    Posts
    2
    Rep Power
    0

    Question Simple Average Program w/Array

    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!

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


  2. #2
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,778
    Rep Power
    137
    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/lib...or=-2147217396

  3. #3
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,778
    Rep Power
    137
    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.

  4. #4
    .NET Framework
    .NET 4.0
    Join Date
    May 2018
    Posts
    2
    Rep Power
    0
    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!

  5. #5
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,778
    Rep Power
    137
    Quote Originally Posted by JadeRae View Post
    I'm sorry I should have said that I'm just using Notepad++!
    Why? You can get VS Code or VS 2017 Community for free.

Similar Threads

  1. Question Help for the simple add subtract program
    By sweetfriend9 in forum C# General Discussion
    Replies: 3
    Last Post: 07-25-2017, 3:21 PM
  2. average is incorrect
    By nickkeith707 in forum C# General Discussion
    Replies: 2
    Last Post: 05-24-2017, 8:51 PM
  3. Question Creating a language program / translate program
    By Fezza in forum C# General Discussion
    Replies: 0
    Last Post: 11-27-2016, 6:35 AM
  4. Question assign an item to a Class Array by reflection if class array is null
    By shiyas in forum C# General Discussion
    Replies: 1
    Last Post: 03-11-2014, 6:45 PM
  5. simple program
    By Boiko in forum Console Application
    Replies: 3
    Last Post: 01-07-2014, 2:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •