1

I have two lists of numbers, the first list has 365 numbers and the other one has 8760.

I want to loop the first list first and get the value. Then inside the first loop, I want to loop the second list to get the 24 number each time.

For the next iterative, it will skip the first 24 and get the next 24.

The problem is the "countelev" seems to not change and stay in ''24'' for every time when I print it. It supposes to be changed to 48, 72, 96....

This is my code:

foreach (var dec in declinationangle)
{
    double declination = dec;
    double elev = Degreetoradian(declination);
    double lati = Degreetoradian(latitude);

    // Solar elevation angle, expressed as α, is the angular height of the
    //sun in the sky measured from the horizontal 0° at sunrise and 90° when
    //the sun is right overhead.
    //α = sin−1 (sin δ sin φ + cos δ cos φ cos τ)
    int countelev = 0;

    foreach (var hra in Solarhourangle.Skip(countelev).Take(24))
    {
        double hras = Degreetoradian(hra);
        double elev1 = Math.Sin(elev) * Math.Sin(lati);                    
        double elev2 = Math.Cos(elev) * Math.Cos(lati) * Math.Cos(hras);                                  
        double slav = Math.Asin(elev1 + elev2);
        double sla = Radiantodegree(slav);

        solarelevationangle.Add(sla);
    }

    countelev += 24;
    TaskDialog.Show("countelev", countelev.ToString());
}
funnydman
  • 9,083
  • 4
  • 40
  • 55
Roy Lin
  • 27
  • 3
  • 3
    `int countelev = 0` is defined in the scope of `foreach (var dec in declinationangle)` which is setting to 0 everytime. Move it outside of first foreach block scope. – user1672994 Nov 29 '19 at 06:36
  • I got it solved by putting the int count =0 outside the foreach loop. Thanks! – Roy Lin Nov 29 '19 at 06:41
  • OT your inner `foreach` will iterate over the entire list until that countelev is reached (and then another 24). A plain `for`-loop over the index starting at countelev and ending 24 later (or sooner, if the end is reached) would be more efficient – Hans Kesting Nov 29 '19 at 07:25
  • @RoyLin when an answer helps you, it is customary to upvote [see here](https://stackoverflow.com/help/someone-answers) – Hans Kesting Nov 29 '19 at 07:28
  • "I want to loop the first list first and get the value." what value? A specific value, or a calculated value? Please explain. – John Alexiou Nov 29 '19 at 16:37

2 Answers2

2

int countelev = 0;

This statement needs to be the first statement of the block that you have pasted. Means the statement needs to be outside the nested foreach statements.

Jane
  • 139
  • 1
  • 8
1

It's considerably simpler with for instead of foreach. You only need one loop, with an index that iterates over the larger array. It can select the correct element from the smaller array by dividing the index by 24 and throwing away the remainder (which, conveniently, is how integer division in c# works).

Here's another way to think of it. Instead of generating elements from two smaller lists, you are populating elements in the target list from two other lists-- since there is only one target list, you only need one loop.

for (var i = 0; i< Solarhourangle.Count; i++)
{
    hra = Solarhourangle[i];
    dec = declinationangle[i/24];  

    double elev = Degreetoradian(declination);
    double lati = Degreetoradian(latitude);

    double hras = Degreetoradian(hra);
    double elev1 = Math.Sin(elev) * Math.Sin(lati);                    
    double elev2 = Math.Cos(elev) * Math.Cos(lati) * Math.Cos(hras);                                  
    double slav = Math.Asin(elev1 + elev2);
    double sla = Radiantodegree(slav);

    solarelevationangle.Add(sla);
}
John Wu
  • 50,556
  • 8
  • 44
  • 80