Reverse array every second time a configuration is encountered

Bazza

Member
Joined
Nov 19, 2020
Messages
7
Programming Experience
Beginner
Hi Guys,

Hoping you can help.

I have an application that uses a specific equation depending on the configuration. The application may have two or more of one particular configuration to calculate. However, on the even number of instances I want it to reverse an array so it gives me a different result every second pass.

The first time the code sees the configuration it does this:

fillDielectricPosMicrostrip(layersArray, ref diel_pos);


And, the second time it encounters this configuration it reverses the array:

fillDielectricPosMicrostrip(layersArray, ref diel_pos);
Array.Reverse(diel_pos);

The third time it will fill the array, the fourth time reverse it etc.

How should I do this?
 
In pseudo-code:
C#:
fillDielectricPosMicrostrip(layersArray, ref diel_pos);
if (instanceNumber % 2 == 0)
    Array.Reverse(diel_pos);
 
Thanks for your suggestion Skydiver but unfortunately it does not appear to reverse the array.
This is what I have:

C#:
if (AppGlobal.CheckPressedThickness)
{
    layer_lr_y = (upperPlaneCopperThickness / 2 +
                  upperDielectric.TotalThickness +
                  signalCopperThickness +
                  lowerDielectric.TotalThickness +
                  lowerPlaneCopperThickness / 2) * s_umTom;
    trc_y[0] = (upperPlaneCopperThickness / 2 + upperDielectric.TotalThickness + signalCopperThickness / 2) * s_umTom;

    fillDielectricPosStripline(layersArray, (upperPlaneCopperThickness / 2 * s_umTom), ref diel_pos);
    fillDielectricConst(layersArray, 0, ref diel_const);

    int instanceNumber = 0;

    if (instanceNumber % 2 == 0)
    {
        Array.Reverse(diel_pos);
        Array.Reverse(diel_const);
    }
}
 
Last edited by a moderator:
If that is really your code, then of course it will not work. Between lines 21 and 23, you have nothing that determines what your instance number is. So you always have an instance number of zero.
 
Thanks for your suggestion Skydiver but unfortunately it does not appear to reverse the array.
This is what I have:
C#:
if (AppGlobal.CheckPressedThickness)

{



layer_lr_y = (upperPlaneCopperThickness / 2 + upperDielectric.TotalThickness + signalCopperThickness +

lowerDielectric.TotalThickness + lowerPlaneCopperThickness / 2) * s_umTom;



trc_y[0] = (upperPlaneCopperThickness / 2 + upperDielectric.TotalThickness + signalCopperThickness / 2) * s_umTom;



fillDielectricPosStripline(layersArray, (upperPlaneCopperThickness / 2 * s_umTom), ref diel_pos);

fillDielectricConst(layersArray, 0, ref diel_const);

int instanceNumber = 0;

if (instanceNumber % 2 == 0)

{

Array.Reverse(diel_pos);

Array.Reverse(diel_const);

}

}
 
I have cleaned up the code formatting in post #3, removing all the unnecessary blank lines and applying proper indenting, to make it easier to read. Please ensure that al code is properly formatted for easy reading in future.
 
Thanks. OK I see. How do I determine the instance number?

If you expect a variable to retain its value between method calls then that variable has to be declared outside any methods. Member variables are a pretty basic concept that pretty much all beginner tutorials will address.
 
Back
Top Bottom