-4

I am modifying a software which is for trash collection. While reading the code, I asked myself is there any formula or trick to quickly calculate the number of time the statement gets executed inside the nested loop rather than doing it manually in head. Here's the kinda pseudo code for this----

for(i=1; i<=9;i++)
{
   sum = 0;
   for(j=i; j<=N && sum<=10; j++)
      sum = sum + arr[j] //any quick trick to calculate it's number of execution
}
14
  • 5
    In this situation, it will highly depend on the content of the array, due to sum<=10 predicate. You can estimate worst and best case scenarios. But for real-world execution, you need to run it and measure it.
    – Euphoric
    Mar 20, 2020 at 12:15
  • 1
    By introducing a counter variable? Or do you mean "beforehand", by analysing the code?
    – Doc Brown
    Mar 20, 2020 at 12:25
  • 1
    Do you know about computational complexity ? Mar 20, 2020 at 12:35
  • 1
    OK. Which takes us back to @Euphoric's comment. Mar 20, 2020 at 12:39
  • 2
    Quick trick eh? Set N to 0 and it executes no times : ) Seriously the quickest way to calculate this is to write a program that calculates this. Mar 20, 2020 at 13:50

1 Answer 1

0

You can use another variable like this to find out how many time loop is executed or what is the current number of a loop being executing in debug :

int count = 0;

for(i=1; i<=9;i++)
{
   sum = 0;
   for(j=i; j<=N && sum<=10; j++)
    {
      sum = sum + arr[j] //any quick trick to calculate it's number of execution
      count++;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.