CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2010
    Posts
    1

    Question Having trouble writing program to find average of numbers

    Hello all.

    I have an assignment in which I'm required to find the average of a sequence of nonnegative numbers entered by the user, where the user enters a negative number to terminate the input. I've gotten the termination to work successfully, but I'm having trouble calculating the average.

    As you can see, my code takes the next number in the sequence and divides it by the number of terms that have been used at that point. Unfortunately that isn't what I want. If only there was some way I could set the first entered number equal to a certain variable, the second to another, and so on. Can anyone give me a suggestion? Below is my code.

    Code:
    static void Main(string[] args)
            {
                double input = 0, average = 0; 
                int numberOfTerms = 1;
    
                Console.WriteLine("To end this program, enter a negative number and press any key.");
    
                while (input >= 0)
                {
                    Console.Write("Enter a number in your sequence: ");
                    input = Double.Parse(Console.ReadLine());
    
                    if (input > 0)
                    {
                        average = input / numberOfTerms;
                    }
    
                    numberOfTerms = numberOfTerms + 1;
    
                    Console.WriteLine("The average so far is {0}.", average);
                }
                Console.ReadKey(true);
            }
        }
    Thanks!

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Having trouble writing program to find average of numbers

    You need to keep the current total and divide that by the number of terms. So near the top, you need to define another variable:

    double total = 0.0;

    Then in the loop, after the user has entered the input, you need something like:

    total += input;

    Then to calculate the average, you need to change the line you have to:

    average = total / numberOfTerms;

    Try that, you're almost there.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Having trouble writing program to find average of numbers

    you would also what to change the line where you check input >0 to total >0
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Having trouble writing program to find average of numbers

    DataMiser, I think that he is just checking for "end of user input" in that statement. He states that a -ve number indicates that the user has finished entering numbers, so I think that line is ok... However the two checks "> 0" and ">= 0" that he has, should be the same, depending on exactly what is required.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #5
    Join Date
    May 2010
    Posts
    8

    Re: Having trouble writing program to find average of numbers

    static void Main(string[] args)
    {
    double input = 0;
    double total = 0;
    int numberOfTerms = 1;

    Console.WriteLine("To end this program, enter a negative number and press any key.");

    while (input >= 0)
    {
    Console.Write("Enter a number in your sequence: ");
    input = Double.Parse(Console.ReadLine());

    if (input > 0)
    {
    total += input;
    numberOfTerms += 1;
    }

    double average = total / numberOfTerms;

    Console.WriteLine("The average so far is {0}.", average);
    }
    Console.ReadKey(true);
    }
    }

  6. #6
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Having trouble writing program to find average of numbers

    Does it work now?
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured