Thread: 2 dim array

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    2 dim array

    1. is it possible to send a two dimensional array to a function and sort it?
    i couldnt get it to work. would it be easier to make it a pointer example:
    int *twodim[] = { 12, etc?
    2. what about my code readability? how could i improve the indentation to make this code look good?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void sort(int *array); // prototype function
    
    
    
    int main(int argc, char *argv[])
    {
      
      int twodim[9]={  // array to be sorted.
          12, 3, 4,
          11, 17 ,8,
          5, 99, 102
          };
          
      int x,c;
      printf("unsorted\n");
      printf("--------\n");
      
         for(x=0;x<9;x++)
          {
           printf("%i\t",twodim[x]);
        
           }
           printf("\n");
    
    
      
      printf("sorted\n");
      printf("------\n");
      sort(twodim);         //function call
      
         for(x=0;x<9;x++)
         {
           printf("%i\t", twodim[x]);  //display the sorted array
           }
           printf("\n"); 
    
      getchar();	
      return 0;
      
    }
      
    void sort(int *array) // sort function
    {
        int temp,a,b;
        
        
        for(a=0;a<9;a++)
          for(b=0;b<9;b++)
              if(array[a]< array[b])
               {
                 temp = array[a];
                 array[a] = array[b];
                 array[b] = temp;
                 }
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    I got the two dim array to work.
    but i still need someone to see how i could improve on my indentation
    readability!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void sort(int *array); // prototype function
    
    
    
    int main(int argc, char *argv[])
    {
      
      int twodim[3][3]={  // array to be sorted.
          12, 3, 4,
          11, 17 ,8,
          5, 99, 102
          };
          
      int x,c,z;
      printf("unsorted\n");
      printf("--------\n");
      
         for(x=0;x<3;x++)
           for(z=0;z<3;z++)
             {
           printf("%i\t",twodim[x][z]);
        
           }
           printf("\n");
    
      
      printf("sorted\n");
      printf("------\n");
      sort(twodim);         //function call
      
         for(x=0;x<3;x++)
           for(z=0;z<3;z++)
         {
           printf("%i\t", twodim[x][z]);  //display the sorted array
           }
           printf("\n"); 
    
      getchar();	
      return 0;
      
    }
      
    void sort(int *array) // sort function
    {
        int temp,a,b;
        
        
        for(a=0;a<9;a++)
          for(b=0;b<9;b++)
              if(array[a]< array[b])
               {
                 temp = array[a];
                 array[a] = array[b];
                 array[b] = temp;
                 }
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    And that didn't generate any warnings when you compiled it?

    It should have.

    Given your array, the correct prototype would be
    void sort( int array[3][3] ); // prototype function

    Notice the resemblance to your array declaration?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by mrsirpoopsalot
    I got the two dim array to work.
    but i still need someone to see how i could improve on my indentation
    readability!
    Go to Wikipedia, and read up on "indentation" - especially the first two styles it mentions - K&R and BSD (aka "student").

    The left margin should be a guide to that line's place in a hierarchy of values. Lines of code outside a function, and ending braces always go all the way to the left side of the page.

    Code inside a function is always indented 2 - 5 spaces, with lines further indented if they're inside another loop or if block of code. Never indent just one space, or more than 5. One char's indentation may be missed (especially with proportional fonts like Arial), and more than 5 is a waste of valuable horizontal space on the screen.

    For K&R, the start of a block of code and that blocks closing brace, always line up vertically. The closing brace only, will have it's own line. For BSD, opening and closing braces always line up, and always have their own separate line, as well.

    This:
    Code:
     
    if(array[a]< array[b])
     {
       temp = array[a];
       array[a] = array[b];
       array[b] = temp;
       }
    With the maddening one char offset of the first and last braces, becomes this: (K&R)
    Code:
     
    if(array[a]< array[b])  {
       temp = array[a];
       array[a] = array[b];
       array[b] = temp;
    }
    Or this: (BSD)
    Code:
     
    if(array[a]< array[b])
    {
       temp = array[a];
       array[a] = array[b];
       array[b] = temp;
    }
    Anyplace you have a semi-colon, put one space after it, same with any operator that allows it:
    This:
    Code:
    for(i=0,i<arraysize;i++)
    becomes the more readable:
    Code:
    for (i = 0, i < arraysize; i++)
    Did you notice the errors above? The semi-colon after the 0 was replaced by a comma. In the second example, the added whitespace around it, makes it much more noticeable. In the first example, you could easily miss it.

    You might think all this is quite trivial, but the truth of the matter is that we are all highly evolved to notice small changes in patterns we see. Whether it's the relative size and shape of the pieces on the chessboard, or fine details in spacing on code, you WILL (whether you want to or not), begin to memorize certain patterns you see repeatedly. That very much includes code.

    Code with a good and consistent style, is just pattern-recognized, with no real effort it seems. Code patterns that are not good or consistent, make you just immediately stop and say "WTF is this crap?". Now read just that small sentence again with different, non-standard spacing: " W TFi sth isc r a p?"

    The first sentence just leaps out at you without any conscious effort, almost, but what about the second one, with just a slight difference in spacing?

    Hope you can see what I mean.

    If you work as a programmer, the company will dictate the style they require - because it let's their employee's all use their pattern recognition skills to a high degree, and speeds the work. Until then, just choose one of the two styles mentioned, and stick with it. When others are looking at your code, it's no time to be showing your individualistic streak. We're marching in review here, not turning somersaults and cartwheels and showing off our latest dance steps.

    Adak

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Thumbs up

    thanks for all your help. i am trying to learn c and want to do it the right way!
    i do understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM