Thread: working sample for sorting arrays but I use globals, would prefer better style

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    working sample for sorting arrays but I use globals, would prefer better style

    Hello everybody


    I have a working sample that gets your input for an array and sorts your numbers afterwards. It works but I am using globals and I believe it aint good. However, because there are 3 different functions if I put them in each, their value goes away as the function ends and the other function needs to reuse the outputted values. I tried sort of passing arguments but it is a mess, it would complain about everything.

    Question: how would I organize the location of the variables in this code to make it "stylish" and correct ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<math.h>
    
    int i, j, t, o; 
    
    int x[10];
    int enterNumbers();
    int sortingNumbers ();
    
    int main () {
    
    enterNumbers();
    sortingNumbers();
    
    
    
    getchar();
    
    return 0;
    
    }
    
    enterNumbers () {
        
                 
            for (i= 0; i<10; i++)
    {
         printf("Input the %d number of your list: ", i);
         
         scanf("%d", &x[i]);
          fflush(stdin);
         
             
         
    }     
                    printf("The entered array is: ");    
        putchar('{');
        
        for(j = 0; j < 10; j++)
        
        {
               
     printf("%d, ", x[j]);
                          
               
               }      
                
                putchar('}');
                
                     
                 }    
                 
           
                   
        sortingNumbers() 
        
        {
        
            for(j = 0; j <10; j++)
            
            {       
                   
                      for(i = 0; i <9; i++)
                      {
               
                          if (x[i] > x[i+1]) // IF THE NUMBER ON THE LEFT IS BIGGER THAN HIS NEXT TO THE RIGHT
        
                          {               // THEN SWAP THEM
                            t = x[i];
                            
                            x[i] = x[i+1];
          
                            x[i+1] = t;         
                   
                   
                          }      // END OF IF
        
        
                      }     // END OF INNER LOOP. 
    
            }  // END OF OUTER FOR LOOP
        
        printf("Sorted it looks like\n");
    
    for (o = 0; o <10; o++)
    {
         printf("%d\,", x[o]);
         
    }
        
       }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well there's no shortage of threads showing how to pass arrays to functions as parameters.

    Also, check out the FAQ - particularly on not using fflush(stdin)
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>  
    #include <stdlib.h>
    #include<math.h>
    
    
    void enterNumbers(int *);
    void sortingNumbers(int *);
    
    
    int main () {
       int i, j, t, o; 
       int x[10];
    
       enterNumbers(x);
       sortingNumbers (x);
    
       getchar();
       return 0;
    }
    void enterNumbers (int *x) {
       int i;         
       for (i= 0; i<10; i++) {
          printf("Input the %d number of your list: ", i);
          scanf("%d", &x[i]);
          getchar(); //eats the newline left behind
       }     
       printf("The entered array is: ");    
       putchar('{');
        
       for(i = 0; i < 10; i++) {
          printf("%d, ", x[i]);
       }      
                
       putchar('}');
    }    
             
    /* 
    You work the sortingNumbers function out, similar to the above function. 
    
    Unless there is some reason to use a different loop increment (decrement) counter, use i, please.
    */  
        sortingNumbers() 
        
        {
        
            for(j = 0; j <10; j++)
            
            {       
                   
                      for(i = 0; i <9; i++)
                      {
               
                          if (x[i] > x[i+1]) // IF THE NUMBER ON THE LEFT IS BIGGER THAN HIS NEXT TO THE RIGHT
        
                          {               // THEN SWAP THEM
                            t = x[i];
                            
                            x[i] = x[i+1];
          
                            x[i+1] = t;         
                   
                   
                          }      // END OF IF
                      }     // END OF INNER LOOP. 
    
            }  // END OF OUTER FOR LOOP
        
       printf("Sorted it looks like\n");
       for (i = 0; i <10; i++) {
          printf("%8d", x[i]); //<--try that
       }
    }
    Print out your comments in lowercase, and avoid excessive blank lines - maybe one out of 5 to 7 lines might be blank or nearly blank.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    If I dont use fflush(stdin) my console vanishes right after scanf
    I ll have a look to that stuff but it is not so much about how to pass arrays as arguments, it is how to connect all the functions
    About blank lines, I only arrange that neatly when I have finished the code, I deliberately leave white spaces when I am still working because always I have to intertwine something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting a doubly linked (linux-style) list
    By smoking81 in forum C Programming
    Replies: 4
    Last Post: 10-01-2008, 01:02 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. vectors vs c style arrays
    By markucd in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2006, 11:11 AM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. a sample outlook addin please check its working
    By codefx in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2004, 01:30 AM

Tags for this Thread