Q:

Add variable number of array elements together at each loop iteration

I'm looking for a looping structure to acomplish the below task:
At each loop iteration I need to add a loop+1 number of indices together up to maxArrSize(5 in this case) then --maxArrSize number of indices together from there until maxArrSize==0.
This is cross correlation of two arrays, I have everything up to the addition of each array shift handled.
Here's a scaled down example to help explain what I'm looking for:
// ------------------------- DON'T FOCUS ON THIS ------------------------
int8 a[5] = { 1, 5, 4, 7, 8 };
int8 b[5] = { 2, 0, 1, 3, 3 };
int maxArrSize = sizeof(a) >= sizeof(b) ? sizeof(a) : sizeof(b);
int8 twoDtemp[5][5];
int8 temp[28] = { };
int8 xCorr[5*2-1] = { };
int x = 5;

// Fill the multiplication matrix
for (int loop = 0; loop < maxArrSize; loop++)
{
    for (int nested = 0; nested < maxArrSize; nested++)
    {
        twoDtemp[loop][nested] = b[iterate] * a[nested];
    }
    iterate--;
}
// Copy into a single dimension array
memcpy(temp, twoDtemp, sizeof(a)*sizeof(a));

// ------------------------- END DON'T FOCUS ON THIS --------------------

// ---------------------------- FOCUS ON THIS ---------------------------
// Below is where I want create a loop that will do the below for me.
// So I don't have to write the entire addition table.

// Cross-Correlate
//xCorr[0] = temp[0]; // +sizeof(b)-1 right, +1 down
//xCorr[1] = temp[1]+temp[5];
//xCorr[2] = temp[2]+temp[6]+temp[10];
//xCorr[3] = temp[3]+temp[7]+temp[11]+temp[15];
//xCorr[4] = temp[4]+temp[8]+temp[12]+temp[16]+temp[20];
//xCorr[5] = temp[9]+temp[13]+temp[17]+temp[21]; // +sizeof(b)-1 right, +sizeof(b) down
//xCorr[6] = temp[14]+temp[18]+temp[22];
//xCorr[7] = temp[19]+temp[23];
//xCorr[8] = temp[24];
// ------------------------ END FOCUS ON THIS ---------------------------

// ****************
// **** UPDATE ****
// ****************
// --------------------------- SOLUTION ---------------------------------
for (int split = 0; split < 2 * x - 1; ++split)
{
    int z = (split < x) ? 0 : split - x + 1;

    for (int j = z; j <= split - z; ++j)
    {
        xCorr[split] += twoDtemp[j][split - j];
    }
}
// ------------------------ END SOLUTION --------------------------------

Your help is appriciated, I will explain more as needed.

A:

As mentioned in Vimal's answer, what you want to sum up the diagonals of the 2D matrix.  This is more apparent if you change your current code to set xCorr based on the 2D array twoDtemp instead of the 1D array temp:
xCorr[0] =  twoDtemp[0][0];
xCorr[1] =  twoDtemp[0][1] + twoDtemp[1][0];
xCorr[2] =  twoDtemp[0][2] + twoDtemp[1][1] + twoDtemp[2][0];
xCorr[3] =  twoDtemp[0][3] + twoDtemp[1][2] + twoDtemp[2][1] + twoDtemp[3][0];
xCorr[4] =  twoDtemp[0][4] + twoDtemp[1][3] + twoDtemp[2][2] + twoDtemp[3][1] + twoDtemp[4][0];
xCorr[5] =  twoDtemp[1][4] + twoDtemp[2][3] + twoDtemp[3][2] + twoDtemp[4][1];
xCorr[6] =  twoDtemp[2][4] + twoDtemp[3][3] + twoDtemp[4][2];
xCorr[7] =  twoDtemp[3][4] + twoDtemp[4][3];
xCorr[8] =  twoDtemp[4][4];

Note that one of the two indexes starts at either 0 or 4 (i.e. the limits of the array dimensions), and that one goes down and one goes up until the limit is hit on the other side.
So you need an outer loop to go through each diagonal, and an inner loop that goes up/down each index.
You can do this as follows:
int matrix[len][len];    // len is an int defined elsewhere

...

// populate matrix

...

int i, j
for (i=0; i < len*2 - 1; i++) {
    int min =  (i - (len-1) > 0) ? i - (len-1) : 0;
    xCorr[i] = 0;
    for (j=0; j<=i-min && j<len-min; j++) {
        xCorr[i] += matrix[j+min][i-min-j];
    }
} 

The min variable dictates the starting X index.  On the first len diagonals it is 0, after that it is the diagonal number minus len.
The inner loop starts the X index at min and the Y index at the diagonal number minus min.  Then the X index goes up and the Y index goes down until either the X index hits len-1 or the Y index hits 0.

