knuckles the man
knuckles the man

Reputation: 113

Need help altering modulus equation

I currently need to produce an array consisting of the following elements:

-0.5, 0, 0.5, 0, -0.5, 0, 0.5, 0...

and so on. This is my current code:

// Sequence: -0.5, 0, 0.5, 0, -0.5, 0, 0.5...
public static double[] generateI(int n) {
    if (n < 0) {
        return null;
    } else if (n == 0) {
        return new double[0];
    }
    double[] arr = new double[n];
    for (int i = 0; i < n; i++) {
        arr[i] = ((0.5 * i) % 2) - 0.5; \\need help here.
    }
    return arr;
}

However, my output is:

[-0.5, 0.0, 0.5, 1.0, -0.5, 0.0, 0.5, 1.0, -0.5, 0.0, 0.5, 1.0].

Hence, my math equation is wrong.

(note that I have tried using the -Math.cos(i * Math.PI/2)/2 equation, and although it is a valid solution, I was tasked to do it using a modulus function in a single line of code.)

Another sequence (for those interested) is:

-0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5...

Any help is appreciated!

Upvotes: 4

Views: 136

Answers (3)

solid.py
solid.py

Reputation: 2812

Use the otherSequence flag to change which of your two patterns you want to activate.

For the second pattern you've mentioned, you need to take the mod of i and 4 (to return [0, 1, 2, 3]) and then add the result of the integer division between i and 4 in order to make the sequence continue to the next 4 numbers (e.g. i = 6 (6 / 4 = 1 -> [1, 2, 3, 4])

class Main {
    public static double[] generateI(int n, boolean other_sequence) {
        if (n < 0) {
            return null;
        } else if (n == 0) {
            return new double[0];
        }
        double[] arr = new double[n];

        if (other_sequence) {
            Arrays.setAll(arr, i -> ((i % 4) + (i / 4)));
        } else {
            Arrays.setAll(arr, i -> ((i % 4) - 1) * ((i + 1) % 2) * 0.5);
        }
        return arr;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(generateI(8, false)));
        System.out.println(Arrays.toString(generateI(16, true)));
    }
}

Output:

[-0.5, 0.0, 0.5, 0.0, -0.5, 0.0, 0.5, 0.0]
[0.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 4.0, 2.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 6.0]

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109597

// Sequence: -0.5, 0, 0.5, 0, -0.5, 0, 0.5...
public static double[] generateI(int n) {
    if (n < 0) {
        return null;
    } else if (n == 0) {
        return new double[0];
    }
    double[] arr = new double[n];
    //for (int i = 0; i < n; i++) {
    //    arr[i] = ((i % 3) - 1) * 0.5;
    //}
    Arrays.arr.setAll(i -> ((i % 3) - 1) * 0.5);
    // Correction, should be ((i%4)-1)*((i+1)%2)*0.5
    return arr;
}

There are 3 values, hence modulo 3. Then think how 0, 1, 2 should be mapped onto -0.5, 0, 0.5.

Nice is the Arrays class.

The other sequence I leave to your own efforts.

Upvotes: 2

Ali Ben Zarrouk
Ali Ben Zarrouk

Reputation: 2018

There's much easier ways I think

private static final double[] doubleArray = new double[] {-0.5, 0, 0.5, 0};
    
    public static double[] generateI(int n) {
        var resArray = new double[n];

        for (int j = 0; j<n; j++) {
            resArray[j] = doubleArray[j % 4];
        }
        
        return resArray;
    }

Upvotes: 0

Related Questions