Complicated riddle?

This is a discussion on Complicated riddle? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } ...

  1. #16
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Code:
    #include <stdio.h>
    
    void swap(int *a, int *b)
    {
    	int temp = *a;
    	*a = *b;
    	*b = temp;
    }
    
    int main(void) 
    {
      int executer[8] = {7, 5, 2, 0, 1, 6, 4, 3};
      int x, y, j;
    
      for(x = 0; x < 8; x++)
        for(y = 0; y < 7; y++)
          if(executer[y] > executer[y+1]) 
          	swap(&executer[y], &executer[y+1]);
    
    	for(j = 0; j < 8; j++)
    		printf("&#37;d\n", executer[j]);
      
      getchar();
      return 0;
    }
    This code works.
    But why 7 and not 8?

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, you are NOT doing a "downto" loop, so not you have "stupid sort", rather than "bubble-sort".

    Code:
    executer[y] > executer[y+1]
    If you run "y" up to 7 [e.g. put 8 in your for-loop] what index will y+1 be in the above conditional?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Oh now I got it!
    Thanks

    BTW,
    GIve me an example of a downto loop and a upto loop.

    BTW 2,
    This is a bubble sort, isn't it? why stupid-sort?

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you ever done a loop that goes from 10 down to 0? That is a "downto loop". For example print all numbers from 10 down to 1 would be a downto loop. I'm sure whatever book or website you are using to learn C from, should have some examples of that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    oh and then the last parameter is i--;
    right?

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, if you count down, then you need to subtract.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vtables riddle.
    By eXeCuTeR in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2008, 02:57 PM
  2. Complicated member objects that depend on `this`
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 09:48 PM
  3. The Most Complicated program
    By Prodigy in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2002, 06:17 PM
  4. Pointer Riddle that is bugging me
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-30-2001, 12:11 AM
  5. Another riddle tread!
    By minime6696 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-24-2001, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21