Thread: Help me to understand this loop concept with clear step by step intial;cond;increemnt

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    1

    Help me to understand this loop concept with clear step by step intial;cond;increemnt

    for( i=0; i< 10; i++){
    for( j=i; j < 10; j++) {
    if( a[i] > a[j]) {
    temp = a[j];
    a[j] = a[j+1];
    a[j+1] = temp;

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Let me show you an equivalence:
    Code:
    int i;
    for (i = 0; i < 10; i++){
        // Things happening in here
    }
    Code:
    int i;
    
    i = 0;
    while (i < 10)
    {
        // Things happening in here
    
        i++;
    }
    See?
    Devoted my life to programming...

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Vivek Nach View Post
    Code:
    for( i=0; i< 10; i++){
    for( j=i; j < 10; j++) {
    if( a[i] > a[j]) {
    temp = a[j];
    a[j] = a[j+1];
    a[j+1] = temp;
    The code appears to be a sort routine, if the code worked correctly! It doesn't!

    First of all, you need to properly format your code. Secondly, you need to properly close your braces.
    Code:
       for( i=0; i< 10; i++){
          for( j=i; j < 10; j++) {
             if( a[i] > a[j]) {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
             }
          }
       }
    Your first error is:
    Code:
    if( a[i] > a[j]) {
    i & j are pointing to the same subscript. The statement will always be false!

    Assuming that a is an array of 10 elements, how should this line be written?
    Code:
    for( j=i; j < 10; j++) {

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rstanley
    i & j are pointing to the same subscript. The statement will always be false!
    That is true on each first iteration of the inner loop, but it will not be true on subsequent iterations. So, the code does an unnecessary iteration, but it should not change the net effect, hence it is inefficient rather than an error per se.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    I knew I needed a second cup of coffee today! ;^)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Heheh. That said, it looks like there is an error on that line, but it has to do with whether a[i], a[j] or a[j+1] should be involved in the comparison. Furthermore, it looks like there is another error on the line that you suggested fixing, but it has to do with a[j+1] in the loop body: if j == 9, and 9 is the last index, then a[j+1] accesses the array out of bounds.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    I was leaving it ti the OP to figure out the remainder of the problems. I didn't want to do all the work for the OP.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-08-2013, 03:16 AM
  2. Step by step read data from exe file to memory.
    By JoBlack in forum C++ Programming
    Replies: 1
    Last Post: 06-23-2012, 02:02 PM
  3. Replies: 4
    Last Post: 05-26-2011, 06:51 AM
  4. why does the memory increase step by step?
    By zcrself in forum C Programming
    Replies: 9
    Last Post: 07-14-2010, 12:04 AM
  5. I always seem to be one step off (Loop question)
    By SquirrelRJ in forum C++ Programming
    Replies: 21
    Last Post: 02-16-2005, 09:13 AM