Basic operations with arrays

2.2k Views Asked by At

I am working in a personalized Math class, I intend to implement basic operations methods so that they add, subtract, multiply or divide in a simple way without having to be calling the operators + - * / every time.

    public class XMath
    {
        public static double Sum(params double[] values)
        {
            double result = 0;

            if (values.Length < 1 || values is null) result = 0;
            else if (values.Length == 1) result = values[0];
            else foreach (double value in values) result += value;

            return result;
        }

        public static double Subtract(params double[] values)
        {
            double result = 0;

            if (values.Length < 1 || values is null) result = 0;
            else if (values.Length == 1) result = values[0];
            else foreach (double value in values) result -= value;

            return result;
        }

        public static double Multiply(params double[] values)
        {
            double result = 0;

            if (values.Length < 1 || values is null) result = 0;
            else if (values.Length == 1) result = values[0];
            else foreach (double value in values) result *= value;

            return result;
        }

        public static double Divide(params double[] values)
        {
            double result = 0;

            if (values.Length < 1 || values is null) result = 0;
            else if (values.Length == 1) result = values[0];
            else foreach (double value in values) result /= value;

            return result;
        }
    }

My problem is that the Sum method works perfectly, but the others they throw wrong results

So, if += it works, because -=, *= and /= no

3

There are 3 best solutions below

2
dee-see On BEST ANSWER

The main problem is that you initialize your result variable with 0.

Assume the array double[] values = new [] { 1.0, 2.0, 3.0 }, your Sum method computes 0 + 1 + 2 + 3, Subtract computes 0 - 1 - 2 - 3, Multiply computes 0 * 1 * 2 * 3 etc.

The extra 0 makes no difference for the sum, but it affects the other operations.

You should initialize your operations with the first element of the array instead of 0.

You can also look into the Aggregate method which would simplify your code to

values.Aggregate((x, y) => x + y);
// ...
values.Aggregate((x, y) => x - y);
// ...
values.Aggregate((x, y) => x * y);
// ...
values.Aggregate((x, y) => x / y);
1
Kevin On

This is one instance where a for loop would be better than a foreach loop

    public static double Sum(params double[] values)
    {
        if (values.Length < 1 || values is null) 
           return 0;
        double result = values[0];
        for(int i = 1; i < values.Length; i++)
           result += values[i];
        return result;
    }
0
AudioBubble On

You can use Enumerable.Aggregate<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>) form System.Linq

Aggregate uses an IEnumerable but creates a double in your case.

Example (and arrays are IEnumerable):

using System.Linq;

double[] values = new [] {5, 9, 3};

values.Aggregate((x, y) => x + y);