PDA

Click to See Complete Forum and Search --> : [2.0] Finding the avarge [SOLVED]


Rinokerpig
Jul 9th, 2006, 09:24 AM
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 :confused:.

Thanks for the help

penagate
Jul 9th, 2006, 09:30 AM
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.

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;

Rinokerpig
Jul 9th, 2006, 09:38 AM
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.

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 :blush:

Thanks

penagate
Jul 9th, 2006, 09:40 AM
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!

Rinokerpig
Jul 9th, 2006, 10:23 AM
I do have one more small problem, one i did not think of :blush: I have the following code and I think im way over my head :eek: and would be greatful for some more of your help :)

static void Main(string[] args)
{
// Ask user for 5 numbers
Console.WriteLine("Hello Please enter 5 numers: ");
// Get users string and convert to integer
string userInput = Console.ReadLine();
int myConvertedString = Int32.Parse(userInput);
int converted = Convert.ToInt32(userInput);

// Use converted numbers in array
int [] numbersEntered = { converted };
int sum = 0;

for (int i = 0; i < numbersEntered.Length; i++)
{
sum += numbersEntered[i];
}
// Find the average number
float average = sum / numbersEntered.Length;
}


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:

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

penagate
Jul 9th, 2006, 10:36 AM
You need to read the numbers into an array, you can do this using String.Split()

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

Rinokerpig
Jul 9th, 2006, 10:51 AM
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 :ehh:

'System.Collections.Generic.List<int>' does not containe a definition for 'Length'

That seems to be happening on this line:
float average = sum / numbers.Length;

Under Length its got the blue scquigly line also if that helps any.

Thanks

Rinokerpig
Jul 9th, 2006, 01:21 PM
Hello,

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

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

penagate
Jul 9th, 2006, 02:23 PM
Sorry I didn't see your edit. My mistake, should have been Count instead of Length :)

Rinokerpig
Jul 10th, 2006, 09:55 AM
Hi penagate,

It's ok thanks for the great help though! :)