1

Just wanted to ask any C# experts out there for some help. I've been struggling with this for a while and just can't figure it out. Basically, I have an array from a text file with 50 numbers (integers). I need to take those 50 numbers, multiply them by a constant and get the average. Trouble is I cannot for the life of me work out how to get the average of the calculated numbers and not just the numbers from the array. Any help is greatly appreciated!

Here is my code so far:

int[] hours = new int[50];
// populate values code goes here
    int total = 0;
double average = 0;

for (int index = 0; index < hours.Length; index++)
{
    total = total + hours[index];
}
//average = total / numbers.Length; // Integer division int / int = int
average = (double)total / hours.Length;
Console.WriteLine("Total = " + total);
Console.WriteLine("Average = " + average.ToString("N2"));

Full code here.

8
  • 1
    numbers.Select(x => x * constantValue).Avg() Commented Sep 6, 2018 at 9:03
  • 1
    For the record, remember that you can also just multiply the average by that constant and you get the same result. Commented Sep 6, 2018 at 9:05
  • 3
    @Rand numbers.DefaultIfEmpty(0).Average(x => x * constantValue) :) Commented Sep 6, 2018 at 9:06
  • 1
    @John damn always forget about that Avg/Max/Min and stuff crash on empty list Commented Sep 6, 2018 at 9:07
  • 1
    Especially on Sum! On Avg, you have 0/0, which would throw an exception anyway. ;) Commented Sep 6, 2018 at 9:10

4 Answers 4

3

You can use LINQ to average the value:

var avg = hours.DefaultIfEmpty(0).Average(x => x) * constantValue;

.DefaultIfEmpty(0) stops .Average() from throwing an exception on an empty list (it will now return 0 in that case).

Sign up to request clarification or add additional context in comments.

2 Comments

Note that this is a inefficient. You can calculate your avg using just x and them multiply the avg once by the constant value. multiplying average === multiplying each element and then averaging
@Prodigle and that's why I shouldn't copy and improve on another answer without fully paying attention lol.
1
    const int sizeOfNums;   
    int[] hours = new int[sizeOfNums];
    const float amountToMultiply =123.44f;

    //Load up you numbers from text file into hours

    float multipliedAverage = 0.0f;

    for(int i=0; i< sizeOfNums; i++)
    {
        multipliedAverage += hours[i];
    }
    multipliedAverage = (multipliedAverage/ sizeOfNums) * amountToMultiply;

5 Comments

Having a variable called average store something that's not an average (only after the last line) is not something I'd recommend. I'd also avoid repeating hardcoded values.
Im getting the error at the last line "Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)" The amountToMultiply in my case is a decimal. - multipliedAverage = (multipliedAverage / 50) * HOURLY_RATE; any ideas?
change multipliedAverage to a float
I'm not that familiar with float. What would that look like? I Tried "float multipliedAverage = 0;" but it didn't work.
for floats you have to attach f to the end e.g 0.0f. Float is the common decimal variable type in C#. It's 32 bits whereas a double (another decimal type) is 64 bits. and then decimals are bigger yet again. You can use whatever you like though, I just always default to float unless i need accuracy
0

Other way: Convert hour array to list and use LINQ:

var hrsList = hours.ToList();
var avr = hrsList.Select(x => x * constantValue).Average();

1 Comment

What makes you believe that LINQ doesnt work with arrays?
-1

also try:

int[] numbers = { 1, 2, 3, 4, 5 };
double average = numbers.Average();

1 Comment

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.