LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   # of times loops are executed (https://www.linuxquestions.org/questions/programming-9/of-times-loops-are-executed-229862/)

leroy27336 09-12-2004 09:10 PM

# of times loops are executed
 
can anyone tell me the number of times the ++ operator is executed in each of the following loops ?

for (i=0; i < n; i++)
for (j=0; j < i; j++)
for (k = 0; k < j; k++)
x++;





for (i=0; i < n; i++)
for (j=0; j < i; j++)
for (k = 0; k < i; k++)
x++;

b0ng 09-12-2004 09:22 PM

No, what are you on?

There is no way to tell simply because you give way to little info.

It's like asking someone to tell you what color a room is, when the lights are off.

n could be anything.

SO that is going to stop people from being able to figure it out. If you gave a little more info, maybe, someone could tell you, but with what you gave, there is no way, that i know of that anyone could find out.

leroy27336 09-12-2004 09:43 PM

no b/c there is a general formula for each loop, and it doesn't matter what n is.....for example...if i gave you the loop

for (i=0; i < n; i++)
for (j=0; j < n; j++)
for (k = 0; k < n; k++)
x++;


then the ++ operator is executed exactly n^3 times......it's that same sort of technique

b0ng 09-12-2004 09:52 PM

Yeah, I know that, that's why it matters what N is.

leroy27336 09-12-2004 09:55 PM

no because in the above example...n could be any number....and it would still execute exactly n^3 time....if n was 2 then it would execute 8 times....if n was 4 then it would execute 64 times....
so no matter what n is, it will still execute n^3 times.....

itsme86 09-13-2004 12:29 AM

What exactly are you asking for? You asked for a specific value in the original post but now you make it sound like you want a formula which isn't even right because you have ++ in 4 different places in the loops so it doesn't get called n^3 times.

leroy27336 09-13-2004 01:00 AM

i just want to know how many times the x++ gets executed in each loop....i can not make it any more clear than that.......

itsme86 09-13-2004 01:09 AM

Well in that case, the answer is you haven't given enough information to give an answer. We need to know what n is to give you a number. Technically speaking, x++ only gets executed in the innermost loop, not each loop, and it gets executed n times...whatever n is...that's the answer you're looking for?

Proud 09-13-2004 03:07 AM

I dunno if it ever does, look at the conditions and when the increments happen:
Code:

for (i=0; i < n; i++)
  for (j=0; j < i; j++)
    for (k = 0; k < j; k++)
      x++;

So i is set to 0, we assume 0 < n is true and the first for loop is entered. The second for loop is encountered and j is set to 0 too. The next thing is to evaluate its condition, which gives us 0 < 0, false, and the loop body and j++ never gets executed. Then after an i++ we're back to the condition of the first loop. Now if n = 1 then 1 < 1 is false and we're done.

Otherwise we loop again and test that j < i, which is now true. I'm gunna stop working the timings from here but my point is that dependant on the value of n you might only get to a certain depth in the for loops and never execute the x++ even if you increment other variables. It all depends on when the conditions are evaluated.

leroy27336 09-13-2004 08:59 PM

you are right, it is dependent on the value of N.....but i'm thinking there still has to be a general formula that will evaluate x++ for any value of N......and that is what i'm trying to figure out.....i also need to figure out the big Oh complexity of each loop as well....i'm thinking it may just be O(N) since i, and j are constants and the loop just depends on the value of N...but i'm not real sure...

Proud 09-14-2004 04:07 AM

Honestly I think this is your homework which is why I havent given you the formula, but I've started you off on it with my previous post. The mods do not want homework questions answered, this site's for people having problems with linux and you'll understand everything better if you do it yourself. Just continue my workings, maybe write the outcome for each value of n >= 0 and try to see the pattern wrt n.


All times are GMT -5. The time now is 06:14 AM.