Skip to main content
added 8 characters in body
Source Link
user34073
user34073

One comment I can make is that the first method is easily changed to support the multiplication of two separate values, while the firstsecond isn't:

static int SquareMultiple(int val1, int val2)
{
    int newVal = 0;

    if (val1 >= 0 && val2 >= 0 || val1 < 0 && val2 < 0)
    {
        val1 = Math.Abs(val1);
        val2 = Math.Abs(val2);

        for (int i = 0; i < val2; i++)
        {
            newVal += val1;
        }
    }
    else
    {
        if (val1 < val2)
        {
            int ttmp = val1;
            val1 = val2;
            val2 = t;tmp;
        }

        for (int i = val2; i < 0; i++)
        {
            newVal -= val1;
        }
    }

    return newVal;
}

The first relies on a special algorithm to calculate squares.

One comment I can make is that the first method is easily changed to support the multiplication of two separate values, while the first isn't:

static int Square(int val1, int val2)
{
    int newVal = 0;

    if (val1 >= 0 && val2 >= 0 || val1 < 0 && val2 < 0)
    {
        val1 = Math.Abs(val1);
        val2 = Math.Abs(val2);

        for (int i = 0; i < val2; i++)
        {
            newVal += val1;
        }
    }
    else
    {
        if (val1 < val2)
        {
            int t = val1;
            val1 = val2;
            val2 = t;
        }

        for (int i = val2; i < 0; i++)
        {
            newVal-= val1;
        }
    }

    return newVal;
}

The first relies on a special algorithm to calculate squares.

One comment I can make is that the first method is easily changed to support the multiplication of two separate values, while the second isn't:

static int Multiple(int val1, int val2)
{
    int newVal = 0;

    if (val1 >= 0 && val2 >= 0 || val1 < 0 && val2 < 0)
    {
        val1 = Math.Abs(val1);
        val2 = Math.Abs(val2);

        for (int i = 0; i < val2; i++)
        {
            newVal += val1;
        }
    }
    else
    {
        if (val1 < val2)
        {
            int tmp = val1;
            val1 = val2;
            val2 = tmp;
        }

        for (int i = val2; i < 0; i++)
        {
            newVal -= val1;
        }
    }

    return newVal;
}

The first relies on a special algorithm to calculate squares.

Multiplynegative numbers
Source Link
user34073
user34073

One comment I can make is that the first method is easily changed to support the multiplication of two separate values, while the first isn't:

static int sqrSquare(int val1, int val2)
{
    int newVal = 0;

    if (val1 >= 0 && val2 >= 0 || val1 < 0 && val2 < 0)
    {
        val1 = Math.Abs(val1);
        val2 = Math.Abs(val2);

        for (int i = 0; i < val2; i++)
        {
            newVal += val1;
        }
    }
    else
    {
        if (val1 < val2)
        {
            int t = val1;
            val1 = val2;
            val2 = t;
        }

        for (int i = val2; i < 0; i++)
        {
            newVal-= val1;
        }
    }

    return newVal;
}

The first relies on a special algorithm to calculate squares.

One comment I can make is that the first method is easily changed to support the multiplication of two separate values, while the first isn't:

static int sqr(int val1, int val2)
{
    int newVal = 0;

    for (int i = 0; i < val2; i++)
        newVal += val1;

    return newVal;
}

The first relies on a special algorithm to calculate squares.

One comment I can make is that the first method is easily changed to support the multiplication of two separate values, while the first isn't:

static int Square(int val1, int val2)
{
    int newVal = 0;

    if (val1 >= 0 && val2 >= 0 || val1 < 0 && val2 < 0)
    {
        val1 = Math.Abs(val1);
        val2 = Math.Abs(val2);

        for (int i = 0; i < val2; i++)
        {
            newVal += val1;
        }
    }
    else
    {
        if (val1 < val2)
        {
            int t = val1;
            val1 = val2;
            val2 = t;
        }

        for (int i = val2; i < 0; i++)
        {
            newVal-= val1;
        }
    }

    return newVal;
}

The first relies on a special algorithm to calculate squares.

Source Link
user34073
user34073

One comment I can make is that the first method is easily changed to support the multiplication of two separate values, while the first isn't:

static int sqr(int val1, int val2)
{
    int newVal = 0;

    for (int i = 0; i < val2; i++)
        newVal += val1;

    return newVal;
}

The first relies on a special algorithm to calculate squares.