Array multiply even/odd indexes?

UnityOneLoveOk

New member
Joined
Oct 22, 2024
Messages
1
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 < maxEntries - 3; i +=2)
{
    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]
}
[/icode]

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.
 
Back
Top Bottom