Array multiply even/odd indexes?

UnityOneLoveOk

New member
Joined
Oct 22, 2024
Messages
3
Programming Experience
Beginner
Hey guys!
C#:
int[] arraySupporter = {
  0,
  1,
  2
};
array[0] = indexEnter;
int temp = array[0];
int tempSecond = 0;
if (array.Length > 1) {
  array[1] = array[1 - 1] * 2;
  array[2] = array[2 - 2] * 2;
}
for (int i = 0; i < array.Length; i++) {
  if (i % 2 == 0 && i < 5) {
    array = array * temp;
    temp = array;
  } else {
    array = array + tempSecond;
    tempSecond = array;
  }
There's already registered value 0,1,2. I want even indexes to multiply even indexes. And i want odd indexes to addict odd indexes. GPT doesn't give me a solution, can you help me with code? Like it has to go like this if indexEnter is 10 : 10 20 20 40 400 80 1600
 
Last edited by a moderator:
Yes, please explain how you got succeeding array values 40 400 80 1600.
 
Alas, the OP has not come back...

The only pattern that I could discern was something like this pseudo-code:
Code:
var arr = new List<int>();
arr.Add(input);        // arr[0]
arr.Add(input * 2);    // arr[1]
arr.Add(input * 2);    // arr[2]
for(int i = 2; i * 2 < maxEntries - 3; i++)
{
    var odd = 0;
    var even = 1;
    for(j = 1; j <= i; j++)
    {
        var entry = arr[j];
        odd += entry;
        even *= entry;
    }
    arr.Add(odd);     // arr[3 + i - 2]
    arr.Add(even);    // arr[4 + i - 2]
}

But it's definitely not adding just the even indices, nor just multiplying just the odd indices. Instead for each iteration of half the size of the intended max array size, it gets the sum or product of the first iteration set of numbers excluding the number at index 0. It's the sums and products which end up going into the odd and even array indices.
 
Last edited:
Hey there! I mean odd numbers has to multiply the previous odd indexes, even has to just use +. But starting from 3rd index (not in for),cuz first indexes already has value. Example : 10[0], 20[0 * 2], 20 [ 0 *2], 40[1+1], 400[2*2], 80[3+3], 1600[4*4], 160[5+5] [] is indexes
 
Part of your confusion is likely because you are using indexing the wrong way. When you are writing arr[2 * 2] what you actually seem to mean is arr[2] * arr[2] starting from the 3rd index onwards.

But that still doesn't work because of your arr[6]. In both your post #1 and post #5, you say that you are expecting to see 1600, but computing the way you seem to be saying, the result would be 160000.

C#:
arr[0] = 10
arr[1] = arr[0] * 2 = 20
arr[2] = arr[0] * 2 = 20
arr[3] = arr[1] + arr[1] = 20 + 20 = 40
arr[4] = arr[2] * arr[2] = 20 * 20 = 400
arr[5] = arr[3] + arr[3] = 40 + 40 = 80
arr[6] = arr[4] * arr[4] = 400 * 400 = 160000        <=== NOT 1600
arr[7] = arr[5] + arr[5] = 80 + 80 = 160
 
but it has to go one i % 2 == 0; with more easy to solve exercise. It doesn't has to go and take all indexes, it has to automated

Part of your confusion is likely because you are using indexing the wrong way. When you are writing arr[2 * 2] what you actually seem to mean is arr[2] * arr[2] starting from the 3rd index onwards.

But that still doesn't work because of your arr[6]. In both your post #1 and post #5, you say that you are expecting to see 1600, but computing the way you seem to be saying, the result would be 160000.

C#:
arr[0] = 10
arr[1] = arr[0] * 2 = 20
arr[2] = arr[0] * 2 = 20
arr[3] = arr[1] + arr[1] = 20 + 20 = 40
arr[4] = arr[2] * arr[2] = 20 * 20 = 400
arr[5] = arr[3] + arr[3] = 40 + 40 = 80
arr[6] = arr[4] * arr[4] = 400 * 400 = 160000        <=== NOT 1600
arr[7] = arr[5] + arr[5] = 80 + 80 = 160
 
Yes it is easy enough to implement with code which checks for odd or even indexes. But to do that you need a good definition of what the problem to be solved is. So far you have not n given a good definition since even your expected output does not match what you describe as the formula to produce the output.

Should are[6] be 160000?
 
Assuming that the 160000 is correct, the pseudo code is pretty simple:
Code:
for index = 3 to arr.Length - 1 do
    let previousValue = arr[index - 2] 
    if (index is even)
        let newValue = previousValue * previousValue
    else
        let newValue = previousValue + previousValue
    set arr[index] = newValue
 

Latest posts

Back
Top Bottom