11

I need to define two methods for returning the sum and average of an int array. The method defining is as follow:-

public int Sum(params int[] customerssalary)
{
           
         // I tried the following but it fails   return customerssalary.sum();
}

Another question is, how can I return the average of these int values?

peterh
  • 11,875
  • 18
  • 85
  • 108
John John
  • 1
  • 72
  • 238
  • 501

7 Answers7

51
customerssalary.Average();
customerssalary.Sum();
L.B
  • 114,136
  • 19
  • 178
  • 224
16

This is the way you should be doing it, and I say this because you are clearly new to C# and should probably try to understand how some basic stuff works!

public int Sum(params int[] customerssalary)
{
   int result = 0;

   for(int i = 0; i < customerssalary.Length; i++)
   {
      result += customerssalary[i];
   }

   return result;
}

with this Sum function, you can use this to calculate the average too...

public decimal Average(params int[] customerssalary)
{
   int sum = Sum(customerssalary);
   decimal result = (decimal)sum / customerssalary.Length;
   return result;
}

the reason for using a decimal type in the second function is because the division can easily return a non-integer result


Others have provided a Linq alternative which is what I would use myself anyway, but with Linq there is no point in having your own functions anyway. I have made the assumption that you have been asked to implement such functions as a task to demonstrate your understanding of C#, but I could be wrong.

musefan
  • 47,875
  • 21
  • 135
  • 185
  • 1
    `decimal result = sum / customerssalary.Length` will calculate the division as an integer and then store it in a decimal. You need to cast one of the two arguments as a decimal/float/whatever before performing the division. – Rawling Oct 18 '12 at 08:28
  • @Rawling: Eh?... dunno what you mean :P – musefan Oct 18 '12 at 08:30
  • if you have `int`s `a` and `b`, `a/b` performs integer divisions (e.g. `7/2` gives `3`). It doesn't matter if you store it in an `int` or a `decimal`, it still performs integer division. – Rawling Oct 18 '12 at 08:31
  • @Rawling... yeah my comment was a joke, perhaps you can see I had already changed it after your suggestion – musefan Oct 18 '12 at 08:33
  • 2
    Sarcasm detection obviously offline this morning :-/ sorry! – Rawling Oct 18 '12 at 08:36
14

Using ints.sum() has two problems:

  • The variable is called customerssalary, not ints
  • C# is case sensitive - the method is called Sum(), not sum().

Additionally, you'll need a using directive of

using System.Linq;

Once you've got the sum, you can just divide by the length of the array to get the average - you don't need to use Average() which will iterate over the array again.

int sum = customerssalary.Sum();
int average = sum / customerssalary.Length;

or as a double:

double average = ((double) sum) / customerssalary.Length;
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • (Sorry if this is the wrong place to ask this) When using the Average method, it returns a decimal but the recommendation here and on [MSDN](http://msdn.microsoft.com/en-us/library/bb354760.aspx) appears to be for double can I ask why? – Paul C May 28 '13 at 10:37
  • Ah, worked it out, it depends on the input, I will leave this comment here for anyone else pondering the same. – Paul C May 28 '13 at 10:39
  • 1
    +1 for mentioning `using System.Linq;` because if it is missing, Visual Studio does not add automatically. – tuncay altınpulluk Feb 22 '17 at 19:07
3

Though the answers above all are different flavors of correct, I'd like to offer the following solution, which includes a null check:

decimal sum = (customerssalary == null) ? 0 : customerssalary.Sum();
decimal avg = (customerssalary == null) ? 0 : customerssalary.Average();
pim
  • 12,019
  • 6
  • 66
  • 69
2

You have tried the wrong variable, ints is not the correct name of the argument.

public int Sum(params int[] customerssalary)
{
    return customerssalary.Sum();
}

public double Avg(params int[] customerssalary)
{
    return customerssalary.Average();
}

But do you think that these methods are really needed?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

If you are using visual studio 2005 then

public void sumAverageElements(int[] arr)
{
     int size =arr.Length;
     int sum = 0;
     int average = 0;

     for (int i = 0; i < size; i++)
     {
          sum += arr[i];
     }

     average = sum / size; // sum divided by total elements in array

     Console.WriteLine("The Sum Of Array Elements Is : " + sum);
     Console.WriteLine("The Average Of Array Elements Is : " + average);
}
shakz
  • 629
  • 3
  • 14
  • 38
  • yeah... this doesn't really help to *return* the values from *two* functions does it. Also, what about when the average is a non-integer value? – musefan Oct 18 '12 at 08:23
  • This method can be split into two right..I was just providing the logic. – shakz Oct 18 '12 at 08:24
0

i refer so many results and modified my code its working

foreach (var rate in rateing)
                {
                    sum += Convert.ToInt32(rate.Rate);
                }
                if(rateing.Count()!= 0)
                {
                    float avg = (float)sum / (float)rateing.Count();
                    saloonusers.Rate = avg;
                }
                else
                {
                    saloonusers.Rate = (float)0.0;
                }
Abdulla Sirajudeen
  • 1,269
  • 1
  • 16
  • 29