3

What's the problem when I try to print the average of the values in int array and it prints out something totally different as many times as I have values.

int[] numbers;
numbers = new int[5];
Console.WriteLine("give five integer numbers:");
numbers[0] = Int32.Parse(Console.ReadLine());
numbers[1] = Int32.Parse(Console.ReadLine());
numbers[2] = Int32.Parse(Console.ReadLine());
numbers[3] = Int32.Parse(Console.ReadLine());
numbers[4] = Int32.Parse(Console.ReadLine());
int sum = 0;

foreach (int x in numbers) {
    sum += x;
    int aver = sum / numbers.Length;  
    Console.WriteLine("average: {0}",aver);
}
5
  • 5
    Use LINQ numbers.Average() Commented Jun 22, 2016 at 11:42
  • 8
    You must calculate average outside foreach Commented Jun 22, 2016 at 11:42
  • 2
    make sum be a floating point value: double sum = 0.0; Commented Jun 22, 2016 at 11:43
  • 1
    move the line 'int aver = sum...' out of the foreach loop Commented Jun 22, 2016 at 11:43
  • 1
    To fix your code, move the int aver line outside the foreach loop. Commented Jun 22, 2016 at 11:43

5 Answers 5

10

Average should be outside the loop:

foreach (int x in numbers) 
{
   sum += x;
}

int aver = sum / numbers.Length;  
Console.WriteLine("average: {0}",aver);

Or using Linq Extension Methods:

Console.WriteLine("average: {0}", numbers.Average());
Sign up to request clarification or add additional context in comments.

Comments

5

You could rephrase the loops as

int sum = 0;
foreach (int x in numbers)
{
    sum += x;
}
int aver = sum / numbers.Length;

or simply do the calculation as

int aver = numbers.Average();

by using Linq.

1 Comment

I think it should be double aver = numbers.Average() regarding to Average() 's return type
3

Or with linq : var aver = numbers.Average();

https://msdn.microsoft.com/en-us/library/bb399409(v=vs.110).aspx

I ll complete my answer with what is the hood under Average() so ;)

  public static double Average(this IEnumerable<int> source) {
       if (source == null) throw Error.ArgumentNull("source");
       long sum = 0;
       long count = 0;
       checked {
          foreach (int v in source) {
              sum += v;
              count++;
          }
       }
       if (count > 0) return (double)sum / count;
       throw Error.NoElements();
   }

1 Comment

"Don't think, use linq" - is this really an answer?. Under the hood Linq does the foreach for you; there's nothing wrong with using Linq, but I hope the OP is also trying to understand the problem.
0

Change

foreach (int x in numbers) {
    sum += x;
    int aver = sum / numbers.Length;  Console.WriteLine("average: {0}",aver);
}

To

foreach (int x in numbers) {
    sum += x;
}
int aver = sum / numbers.Length;  Console.WriteLine("average: {0}",aver);

Comments

0

One of the best way and clean code for calculating average of array elements using below code:

int[] numbers = new int[5];
int sum = 0;
int average = 0;

Console.WriteLine("give five integer numbers:");

for(int i = 0; i < numbers.length; i++)
    numbers[i] = Int32.Parse(Console.ReadLine());

Array.ForEach(numbers, x => sum += x);
average = sum / numbers.Length;
Console.WriteLine("Average: {0}", average);

With Array.ForEach you can access to all of elements in Array and using any operand for it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.