I try to create a simple function in C which permit us to find the current step of a series of number by giving the current line number, the total number of lines, and the number of steps...
Example of numbers series :
line 0 - 0 --
line 1 - 0 |--> STEP 1
line 2 - 0 --
line 3 - 1 --
line 4 - 1 |--> STEP 2
line 5 - 1 |
line 6 - 1 --
line 7 - 2 --
line 8 - 2 |
line 9 - 2 |--> STEP 3
line 10 - 2 |
line 11 - 2 --
Parameters : currentLine = 5; totalLines = 12; steps = 3;
I have three different steps in this case, all steps are incremented by one more line. Each step is represented by the same number, next to line numbers.
In my example, I choose currentLine = 5, which represents the line where we want to find the current step. So in my case I need to find : 2.
My prototype of my function giving the current step of the current line :
int findCurrentStep(int currentLine, int totalLines, int steps);
I just want to know how to calculate it ?
EDIT : Thank you for your answers, I just made another method.
int findCurrentStep(int currentLine, int totalLines, int steps)
{
int step;
int trim_lines;
step = steps;
trim_lines = totalLines;
while (currentLine <= trim_lines -1)
{
trim_lines = totalLines - 3 + steps - 1;
step--;
}
return step;
}
This works with just one step, but no several ones...
EDIT : -
This is a 1-step solution.
You need to find that the currentLine is equal or less than which first r-terms of the AP starting with 3. The first term whose sum overall surpasses currentLine would be the currentStep.
Let's suppose that currentLine is less than or equal to sum of first r terms of AP.
So, currentLine <= 3 + ... + 3 + (r-1) * 1.
PSEUDO-CODE :-
initialStep = 3; // initialStep is 3 in this case.
sum = currentLine; .
currentStep = 0;
while(sum > initialStep){
sum -= initialStep;
initialStep = intialStep + 1;
currentStep++;
}
requiredAnswer = currentStep + 1;
Your answer would be (currentStep + 1) after the loop as the loop would be terminated when the condition would fail.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments