Results 1 to 10 of 10

Thread: [2.0] Finding the avarge [SOLVED]

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    [2.0] Finding the avarge [SOLVED]

    Hello,

    Im having a little trouble finding the avarage number from an application where the user would enter any 5 numbers. The application asks the user to enter 5 numbers and from them numbers my goal is to find the avarge number? and since im still learning C# im stuck and dunno what todo can some one please help me out. I've not yet written any code as asking the user is no problem how ever getting the avarge is .

    Thanks for the help
    Last edited by Rinokerpig; Jul 9th, 2006 at 01:22 PM.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Finding the avarge

    Averaging numbers (finding the statistical mean) is easy - didn't you do this at school? Find the sum of all numbers, divide by the count.

    Code:
    int[] numbers = {1, 2, 5, 7, 2, 4, 5};
    int sum = 0;
    
    for (int i = 0; i < numbers.Length; i++) {
      sum += numbers[i];
    }
    
    float average = sum / numbers.Length;

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Re: [2.0] Finding the avarge

    Quote Originally Posted by penagate
    Averaging numbers (finding the statistical mean) is easy - didn't you do this at school? Find the sum of all numbers, divide by the count.

    Code:
    int[] numbers = {1, 2, 5, 7, 2, 4, 5};
    int sum = 0;
    
    for (int i = 0; i < numbers.Length; i++) {
      sum += numbers[i];
    }
    
    float average = sum / numbers.Length;
    Hello

    Wow that was a fast repliy and thanks also i was never any good at math at school

    Thanks

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Finding the avarge

    You're welcome!

    [robot mode]
    Unless you have any further questions please go to the "Thread Tools" menu above the first post and click "Mark Thread Resolved", that will let other forum users know your question has been answered. Thanks and welcome to VBForums!

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Re: [2.0] Finding the avarge

    I do have one more small problem, one i did not think of I have the following code and I think im way over my head and would be greatful for some more of your help

    VB Code:
    1. static void Main(string[] args)
    2.         {
    3.             // Ask user for 5 numbers
    4.             Console.WriteLine("Hello Please enter 5 numers: ");
    5.             // Get users string and convert to integer
    6.             string userInput = Console.ReadLine();
    7.             int myConvertedString = Int32.Parse(userInput);
    8.             int converted = Convert.ToInt32(userInput);
    9.  
    10.             // Use converted numbers in array
    11.             int [] numbersEntered = { converted };
    12.             int sum = 0;
    13.  
    14.             for (int i = 0; i < numbersEntered.Length; i++)
    15.             {
    16.                 sum += numbersEntered[i];
    17.             }
    18.             // Find the average number
    19.             float average = sum / numbersEntered.Length;
    20.         }

    As you may or may not know from the above im trying to get user input instead of hardcoding the numbers like you did in the first example I want the user to enter five random numbers (see below), when I go to run the application im getting this:

    Code:
    Hello Please enter 5 numers:
    3 88 74 261 63
    
    Unhandled Exception: System.FormatException: Input string was not in a correct f
    ormat.
       at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffe
    r& number, NumberFormatInfo info, Boolean parseDecimal)
       at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo in
    fo)
       at System.Int32.Parse(String s)
       at Test.Program.Main(String[] args) in C:\learn\Test\Test\Program.cs:line 13
    Press any key to continue . . .
    any help you can provide would be greatfully recievied

    Once again thanks

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Finding the avarge

    You need to read the numbers into an array, you can do this using String.Split()

    Code:
    using System.Collections.Generic;
    
    static void Main(string[] args)
    {
      Console.WriteLine("Please enter 5 numbers separated by spaces:");  
      string userInput = Console.ReadLine();
      
      string[] numberStrings = userInput.Split(' ');
    
      // Since you're using .NET 2.0 we can use a generic list here,
      // so that we can use the Add() method
      List<int> numbers;
    
      // parse each number, only use valid numbers
      for (int i = 0; i < numberStrings.Length; i++) {
        numbers.Add(int.Parse(numberStrings[i]));
      }
    
      // find the average
      int sum = 0;
      for (i = 0; i < numbers.Length; i++) {
        sum += numbers[i];
      }
    
      float average = sum / numbers.Length;
    
      // print it out using a format string
      Console.WriteLine("Average value is {0}", average);
    }

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Re: [2.0] Finding the avarge

    Hello

    Thanks once again , I'm not 100% on all that code but I'm sure/hope the MSDN help files has the explination for that

    Once again thanks so much!

    EDIT:

    After trying this code I'm getting the following error

    Code:
    'System.Collections.Generic.List<int>' does not containe a definition for 'Length'
    That seems to be happening on this line:
    Code:
    float average = sum / numbers.Length;
    Under Length its got the blue scquigly line also if that helps any.

    Thanks
    Last edited by Rinokerpig; Jul 9th, 2006 at 11:10 AM.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Re: [2.0] Finding the avarge

    Hello,

    OK after a little time thinking about this its now solved using this:

    Code:
    static void Main(string[] args)
            {
                int[] numbers = new int[5];
                int sum = 0;
                double average = 0;
    
                for (int i = 0; i < numbers.Length; i++)
                {
                    if (i == 0)
                    {
                        Console.Write("Please, write the {0}st number: ", i + 1);
                    }
                    else if (i == 1)
                    {
                        Console.Write("Please, write the {0}nd number: ", i + 1);
                    }
                    else if (i == 2)
                    {
                        Console.Write("Please, write the {0}rd number: ", i + 1);
                    }
                    else if (i == 3)
                    {
                        Console.Write("Please, write the {0}th number: ", i + 1);
                    }
                    else
                    {
                        Console.Write("Please, write the {0}th number: ", i + 1);
                    }
                    numbers[i] = int.Parse(Console.ReadLine());
                    sum += numbers[i];
                }
                average = (double) sum / numbers.Length;
                Console.WriteLine("The average number of the {0} given numbers is: {1}", numbers.Length, average);
            }
    Once again thanks for your help

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Finding the avarge [SOLVED]

    Sorry I didn't see your edit. My mistake, should have been Count instead of Length

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Re: [2.0] Finding the avarge [SOLVED]

    Hi penagate,

    It's ok thanks for the great help though!

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