Solved

Help me walk through a few loops to see how many executions. I need a lesson

Posted on 2004-09-25
8
169 Views
Last Modified: 2010-03-31
I'm having difficulty finding out how many executions these loops operate sum++. i'm not only interested in the answer but how youy figured it out.


 A)   for(i=1 ; i<= 100 ; i++)
         for (j= 1 ; j <= i; j++) sum++;    

 A)   i'm thinking that the first for loop goes through 100 times and the second for loop is also 100 times so 100^2 is correct?? is this right?


B) for(i = 101; i <=200; i++)
       for(j= i - 1;  j <i+1;j++) sum++;

B) I tried walking through it and it seems the first loop starts out and goes through 99 times. hen thew second one is true for 99 times so 99^2
  my thinking was as follows
1st time through j =100 ; 100 < 101 +1; 100++

2nd time  j=101 ; 101 <102+1; 101++
.
.
.
.
199th time j= 199 ; 199 < 200 +1 ; 200++ so stops here 199-100 = 99 times....is this the right logic??
   

0
Comment
Question by:tyweed420
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 92

Accepted Solution

by:
objects earned 20 total points
ID: 12153143
A) No, the first loop will go 100 times, but the second loop will execute a variable number of time.
first time (i=1) it will execute once, second time (i=2) it will execute twice, etc
So total loops = 1 + 2 + 3 + ... + 100.

B) No, again right for the first loop, but not for the second.
The second loop will execute (i+1) - (i-1) times = 2 times
So total number of loops is 99 x 2 = 198 times.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12153844
B) 200!

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12153986
Classwork done then?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12155171
No reason to give a wrong answer .....
;JOOP!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tyweed420
ID: 12156456
Objects thanks for the help you get the huge 20 points! may i ask though when you figured out part B how did you know to take (i+1) - (i-1) is that some type of formula or common knowledge proccess you used to take those two arguments and derive 2?

Thanks in advance!
0
 
LVL 92

Expert Comment

by:objects
ID: 12156544
> B) 200!

That is correct, I overlooked the <=.

> how did you know to take (i+1) - (i-1)

just the difference between the upper and lower ranges for the index.
0
 

Author Comment

by:tyweed420
ID: 12182959
Hey guys one last question for ya regarding counting loops. These two are very difficult for me

1)  for(int i = 100 ; i > 0  ;i--)  
         for(j=i;   j  <  2 * i + 2 ; j++)
             sum++

         1. i'm thinking the first loop goes through 99 times but then when you get into the second loop it lookslike this

         i=100                 i=99           ....................      i=1
      =======        ========                          =========
       j=100                j=99                                         j=1
      i=202                 i=20                                         i=4                                   so its 3+4+5+6..........+102 = times sum++ is executred is this correct?
     102 times             101 times                                 3 times
     


2)           for(int i = 0 ; i <10000  ;i++)  
                  for(j=0 ;   j *j*j < i ; j++)
                           sum++

 this one i have no clue can someone walk me through it ?
             
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12187274
No, you won't get 2 answers for only 20 points.

;JOOP!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 21
Java Timer (static) 9 36
servlet doXXX methods 3 36
Arrays.asList  VS  ArrayList 4 44
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now