0

Ok so I have two arrays of integers, and I have to return the average rating of the two days. This is the code that I have come up with thus far, but apparently it does not meet the expectations. Result:

Message: Expected: 5.0d But was: 3.0d

Unit:

public double WeekendAverage(int[] saturday, int[] sunday)
    {
        int[] n = { saturday.Length, sunday.Length };
        int sum = 0;

        for (int i = 0; i < n.Length; i++)
        {
            sum = saturday[i] + sunday[i];
            return sum / n.Length;
        }
        return sum;
        }

UnitTest:

[TestCase(new[] { 1, 2, 3, 4, 5, 7, 8, 5, 10 }, new[] { 9, 9, 9, 8, 9, 8, 9, 9, 9, 10, 10 }, 7)]

public void WeekendAverage(int[] saturday, int[] sunday, double expected)
    {
        var actual = warmups.WeekendAverage(saturday, sunday);
        Assert.AreEqual(expected, actual);
    }
2
  • 3
    There are multiple problems in your code. Array n in your code will have only two values, length of Saturday array and length of Sunday array. It will not have the actual values from Saturday and Sunday. Also you are returning from for loop so the loop will be executed only once. You need make the logic clear and debug the code and see if it runs as per the logic. And correct it accordingly Commented May 31, 2019 at 2:06
  • running your test case code gives: Message: Expected: 7.0d But was: 5.0d Commented May 31, 2019 at 2:17

4 Answers 4

4

You can do it with System.Linq

var saturday = new[] { 1, 2, 3, 4, 5, 7, 8, 5, 10 };
var sunday = new[] { 9, 9, 9, 8, 9, 8, 9, 9, 9, 10, 10 };

var average = saturday.Concat(sunday).DefaultIfEmpty(0).Average();
Sign up to request clarification or add additional context in comments.

10 Comments

Can be simplified to saturday.Concat(sunday).Average()
@AlphaDelta saturday.Concat(sunday).DefaultIfEmpty(0).Average() is probably safer :)
@John Solid point, prevents an exception if both collections contain no items
Can also be extended with params and SelectMany to average an arbitrary amount of collections, see here: dotnetfiddle.net/FDnxiy
Thank you. I updated the answer to include your suggestions.
|
2

Without Linq. Just calculate the sum and divide with the length of the combined array.

public double WeekendAverage(int[] saturday, int[] sunday)
{
    double sum = 0;
    for (int i = 0; i < saturday.Length; i++)
    {
        sum += saturday[i];
    }
    for (int i = 0; i < sunday.Length; i++)
    {
        sum += sunday[i];
    }
    return sum / (saturday.Length + sunday.Length);
}

Comments

0

The length of n is 2 as an array. You want an int that stores the total of the two array lengths, not an array of integers representing the lengths of your parameters.

Change that to n = saturday.length + sunday.length and i < n in your loop.

1 Comment

How would I return sum?
0

Using Linq (adapted from Theodor's answer) :

public double WeekendAverage(int[] saturday, int[] sunday)
{
    double sum = saturday.Sum() + sunday.Sum();
    return sum / (saturday.Length + sunday.Length);
}

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.