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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-12-2004, 09:10 PM
|
#1
|
Member
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127
Rep:
|
# 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++;
|
|
|
09-12-2004, 09:22 PM
|
#2
|
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:
|
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.
|
|
|
09-12-2004, 09:43 PM
|
#3
|
Member
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127
Original Poster
Rep:
|
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
|
|
|
09-12-2004, 09:52 PM
|
#4
|
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:
|
Yeah, I know that, that's why it matters what N is.
|
|
|
09-12-2004, 09:55 PM
|
#5
|
Member
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127
Original Poster
Rep:
|
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.....
|
|
|
09-13-2004, 12:29 AM
|
#6
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
09-13-2004, 01:00 AM
|
#7
|
Member
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127
Original Poster
Rep:
|
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.......
|
|
|
09-13-2004, 01:09 AM
|
#8
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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?
|
|
|
09-13-2004, 03:07 AM
|
#9
|
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794
Rep: 
|
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.
|
|
|
09-13-2004, 08:59 PM
|
#10
|
Member
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127
Original Poster
Rep:
|
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...
|
|
|
09-14-2004, 04:07 AM
|
#11
|
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794
Rep: 
|
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 03:48 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|