Thread: code explanation

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    31

    code explanation

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define SIZE 200
    
    void sort(int *array, int size);
    int rnd(int range);
    void seedrnd(void);
    
    void main()
    {
     int number[SIZE];
     int x;
    
     printf("Here is the array unsorted and random.\n");
    
     seedrnd();
     for(x=0;x<SIZE;x++)
     {
                        number[x] = rnd(140)+1;
                        printf("%i\t",number[x]);
     }
    
     printf("\nPress enter to see the array sorted.");
     getchar();
     printf("\n");
    
     sort(number, SIZE);
    
     for(x=0;x<SIZE;x++);
                         printf("%i\t",number);
     }
    
     void sort(int *array, int size)
     {
     int a, b, temp;
    
     for(a=0;a<SIZE-1;a++)
                        for(b=a+1;b<SIZE;b++)
                                             if(array[a] > array[b])
                                             {
                                              temp=array[b];
                                              array[b]=array[a];
                                              array[a]=temp;
                                             }
     }
    
     int rnd(int range)
     {
      int r;
    
      r=rand()%range;
      return(r);
     }
    
     void seedrnd(void)
     {
      srand((unsigned)time(NULL));
    }

    i understand the first part but what does this do??--


    Code:
    void sort(int *array, int size)
     {
     int a, b, temp;
    
     for(a=0;a<SIZE-1;a++)
                        for(b=a+1;b<SIZE;b++)
                                             if(array[a] > array[b])
                                             {
                                              temp=array[b];
                                              array[b]=array[a];
                                              array[a]=temp;
                                             }
     }
    
     int rnd(int range)
     {
      int r;
    
      r=rand()%range;
      return(r);
     }
    
     void seedrnd(void)
     {
      srand((unsigned)time(NULL));
    }
    what does it all mean because i dont see any of it used in the actual randomizing and sorting the numbers. also, the \t isnt tabbing so it just looks like one big number, not a huge deal, but if anyone could tell me, thanks.

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    /sigh for some reason it wont show the sorted array correctly. the unsorted/random comes up fine...but once i press enter only about 10 numbers come up.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define SIZE 200
    
    void sort(int *array, int size);
    int rnd(int range);
    void seedrnd(void);
    
    void main()
    {
     int number[SIZE];
     int x;
    
     printf("Here is the array unsorted and random.\n");
    
     seedrnd();
     for(x=0;x<SIZE;x++)
     {
                        number[x] = rnd(140)+1;
                        printf("%i\t",number[x]);
     }
    
     printf("\nPress enter to see the array sorted.");
     getchar();
     printf("\n");
    
     sort(number,SIZE);
    
     for(x=0;x<SIZE;x++);
                         printf("%i\t",number[x]);
     }
    
     void sort(int *array, int size)
     {
     int a, b, temp;
    
     for(a=0;a<SIZE-1;a++)
                        for(b=a+1;b<SIZE;b++)
                                             if(array[a] > array[b])
                                             {
                                              temp=array[b];
                                              array[b]=array[a];
                                              array[a]=temp;
                                             }
     }
    
     int rnd(int range)
     {
      int r;
    
      r=rand()%range;
      return(r);
     }
    
     void seedrnd(void)
     {
      srand((unsigned)time(NULL));
    }
    erg. any help would be awesome.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void main()
    main returns an int.
    See the FAQ/previous replies

    > for(x=0;x<SIZE;x++);
    > printf("%i\t",number[x]);
    Guess what that first ; means.

    Also, is the code as crappily indented in your editor as it appears to be on this board?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Code Explanation
    By dmkanz07 in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 08:24 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM