In my course on object-oriented programming, we discussed a topic that I donβt think he ever called, I tried to find out what it was to find the right way to solve them, but I was out of luck.
This is not homework, but a question for clarifying the process of solving this problem.
for I = (N + 2) downto -1 for J = (I - 1) to (N + 4) // Code is run here
Question: "How many times // Code is run here works?"
Here is what I was trying to solve:
1) I = (N + 2) , J = [(N + 2) - 1] from this (and what I remember) you use b - a - 1 to solve for the number of times executed, which gives us X = [(N + 2) - 1] - (N + 2) - 1 , which can be simplified to X = -2
2) I = -1 , J = ((- 1) - 1) and X = ((-1) - 1) - (-1) - 1 which simplifies to X = -2`
I get lost when working with the second for loop and how to end the problem. I know that we should get an answer, for example r(r + 1)/2
I just want to say that I tried to find the name of this type of technique, but he just called it "Code Counting", which did not return any queries related to this topic.
thanks
EDIT: This course was in Java, so I used the Java tag for this question if anyone is interested.
EDIT2: To clarify, this was a written exam , so we had to do it with pens and paper, I would like to explain how to solve this question, since I have tried many times to do it and still got the wrong answer.