LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2004, 09:10 PM   #1
leroy27336
Member
 
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127

Rep: Reputation: 15
# 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++;
 
Old 09-12-2004, 09:22 PM   #2
b0ng
LQ Newbie
 
Registered: Aug 2004
Location: Location??? Where I am is top secret, if I tell you, I have to kill you.
Distribution: College, Slack
Posts: 24

Rep: Reputation: 15
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.
 
Old 09-12-2004, 09:43 PM   #3
leroy27336
Member
 
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127

Original Poster
Rep: Reputation: 15
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
 
Old 09-12-2004, 09:52 PM   #4
b0ng
LQ Newbie
 
Registered: Aug 2004
Location: Location??? Where I am is top secret, if I tell you, I have to kill you.
Distribution: College, Slack
Posts: 24

Rep: Reputation: 15
Yeah, I know that, that's why it matters what N is.
 
Old 09-12-2004, 09:55 PM   #5
leroy27336
Member
 
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127

Original Poster
Rep: Reputation: 15
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.....
 
Old 09-13-2004, 12:29 AM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 09-13-2004, 01:00 AM   #7
leroy27336
Member
 
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127

Original Poster
Rep: Reputation: 15
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.......
 
Old 09-13-2004, 01:09 AM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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?
 
Old 09-13-2004, 03:07 AM   #9
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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.

Last edited by Proud; 09-13-2004 at 03:08 AM.
 
Old 09-13-2004, 08:59 PM   #10
leroy27336
Member
 
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127

Original Poster
Rep: Reputation: 15
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...
 
Old 09-14-2004, 04:07 AM   #11
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get next executed line phuna Programming 2 07-08-2005 12:05 AM
hotplug script executed 3 times Borelian Linux - Software 1 06-28-2004 11:46 PM
.bashrc not being executed, why? realos Linux - Newbie 1 12-20-2003 07:46 PM
.bash_logout does not get executed nearfar Linux - Newbie 3 10-17-2003 11:40 AM
.xinitrc isn't executed hampel Linux - Software 5 08-06-2003 03:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration