This is the code that works, but it isn't scalable to varying array lengths.
How can I simplify this?
if(section_time_cumulative[0]<= time &&
section_time_cumulative[1]>=time)
{
linears[0]
}
else if(section_time_cumulative[1]<= time &&
section_time_cumulative[2]>=time)
{
linears[1]
}
else if(section_time_cumulative[2]<= time &&
section_time_cumulative[3]>=time)
{
linears[2]
}
else if(section_time_cumulative[3]<= time &&
section_time_cumulative[4]>=time)
{
linears[3]
}
else if(section_time_cumulative[4]<= time &&
section_time_cumulative[5]>=time)
{
linears[4]
}
else if(section_time_cumulative[5]<= time &&
section_time_cumulative[6]>=time)
{
linears[5]
}
else if(section_time_cumulative[6]<= time &&
section_time_cumulative[7]>=time)
{
linears[6]
}
else if(section_time_cumulative[7]<= time &&
section_time_cumulative[8]>=time)
{
linears[7]
}
else
{
0
}
Expected result:
I've tried this way, but it doesn't produce the desired result; it's resulting in only the 'last' iteration of values being defined as I expect.
for(arrIndex = 0 ; arrIndex< section_bars.length-1; arrIndex++)
{
if(section_time_cumulative[arrIndex]<= time &&
section_time_cumulative[arrIndex+1]>=time)
{
linears[arrIndex]
}
else
{
0
}
}
Note only the last values output (far right end of graph):


arrIndex< section_bars.length-1should bearrIndex < section_bars.length-2orarrIndex <= section_bars.length-1since you wantarrIndexto be from 0 to 7, not to to 8.