Using unix command line to sort random array

This is a discussion on Using unix command line to sort random array within the C Programming forums, part of the General Programming Boards category; hey everyone I need to use the command line arguments -a and -d to sort a random array in ascending ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    13

    Using unix command line to sort random array

    hey everyone I need to use the command line arguments -a and -d to sort a random array in ascending and descending order (respectively). I have the random number generator down but I have no idea how to implement the argc argv[] part into the program so if i run my program in a unix shell like Program -a would output array in ascending order where Program -d would output array in descending order. Here is what I have so far....
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, const char * argv[])
    {
            int array[20];
            int i;
    
            for (i=0; i<20; i++)
                    array[i] = i;
    
            srand( (unsigned)time( NULL ) );
            for (i=0; i<20; i++)
            {
                    array[i]= rand()%1000;
    
    				
    }
            for (i=0; i<20; i++)
                    printf("array[%d] = %d\n",i,array[i]);
    
            return(0);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    There's something about handling command line arguments in the FAQ.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    ok i got a little farther, will somebody please help me out, it would be greatly appreciated.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    void swap(void *e1, void *e2, size_t size);
    void bubblesort(void *base, size_t n, size_t size,
    int (*cmp)(const void *el1, const void *el2));
    
    		int array[20];
    		int i,n,c=0;
    		
    
    int main(int argc, char * argv[])
    {
            int array[20];
            int i;
    		
    		  for (i=0; i<20; i++)
                    array[i] = i;
    
            srand( (unsigned)time( NULL ) );
            for (i=0; i<20; i++)
            {
                    array[i]= rand()%1000;
    
    				
    }
            for (i = 1; i < argc; i++)
      {
        if (argv[i][0] == '-')
          if (argv[i][1] == 'a'){
              
    				void bubblesort();
    				for( i=0; i<20; i++)
    				printf("%d\n",array[i]);
    					}
          else if (argv[i][1] == 'd')
            {
      			void bubblesort();
    			for( i=0; i<20; i++)
    			printf("%d\n",array[i]);
    		}      else printf ("Unknown switch %s\n", argv[i])
        else if (strcmp(argv[i], "name") == 0)
          puts ("Found name");
      }
    
    		
    			
    
            return(0);
    }
    
    void bubblesort(void *base, size_t n, size_t size,
    int (*cmp)(const void *el1, const void *el2))
    {
    size_t i, sorted = 0;
    char *p = (char *) base;
    
    while(!sorted)
    {
    sorted = 1;
    for(i = 0;i < n-1; i++)
    {
    if(cmp(p+(i*size),p+((i+1)*size))>0)
    {
    swap(p+(i*size),p+((i+1)*size),size);
    sorted = 0;
    }
    }
    }
    }
    
    void swap(void *e1, void *e2, size_t size)
    {
    char buf[256], *p1 = (char *)e1, *p2 = (char *)e2;
    size_t ms;
    
    for(ms = size; 0< ms; )
    {
    size_t m = ms < sizeof(buf)?ms:sizeof(buf);
    memcpy(buf,p1,m);
    memcpy(p1,p2,m);
    memcpy(p2,buf,m);
    ms -= m, p1+=m, p2+=m;
    }
    return;
    }
    
    int cmp(const void *e1, const void *e2)
    {
    int *i1 = *(int **)e1, *i2 = *(int **)e2;
    
    return (*i1<*i2)?-1:(*i1!=*i2);
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    ok i changed the sort functions to a different type, why isnt this working?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    void bubbleDescend(int*,int);
    void printElements(int*, int);
    void bubbleAscend(int*, int);
    void printAscend(int*, int);
    
    		int integers[20];
    		int i,k,n,c=0;
    		
    
    int main(int argc, char * argv[])
    {
    		
    		  for (i=0; i<20; i++)
                    integers[i] = i;
    
            srand( (unsigned)time( NULL ) );
            for (i=0; i<20; i++)
            {
                    integers[i]= rand()%1000;
    
    				
    }
            for (i = 1; i < argc; i++)
      {
        if (argv[i][0] == '-')
          if (argv[i][1] == 'a'){
              
    				bubbleDescend(integers,n);
    				printElements(integers,n);
    					}
          else if (argv[i][1] == 'd')
            {
      			bubbleAscend(integers,n);
    			printAscend(integers, n);
    			}
    			else printf ("Unknown switch %s\n", argv[i]);
        else if (strcmp(argv[i], "name") == 0)
          puts ("Found name");
      }
    
    		
    			
    return 0;
    }
     
    void bubbleDescend(int *p,int length)//Bubble sort function 
    {
    	int h,j;
    	for(j=0;j<length;j++)
    	{
    		for(h=0;h>j;h++)
    		{
    			if(p[j]>p[h])
    			{
    				int temp=p[j]; //swap 
    				p[j]=p[h];
    				p[h]=temp;
    			}
     
    		}
     
    	}
     
    }
     
    void bubbleAscend(int *q, int length)
    {
    	int m,t;
    	for(m=0;m<length;m++)
    	{
    		for(t=0;t<m;t++)
    		{
    			if(q[m]<q[t])
    			{
    				int temp=q[m]; //swap 
    				q[m]=q[t];
    				q[t]=temp;
    			}
     
    		}
     
    	}
     
    }
    void printElements(int *p, int length) //print array elements
    {
    	int k=0;
    	for(k=0;i<length;k++)
    	{
    		printf("%d\n", p[k]);
    	}
    }
    void printAscend(int *q, int length) //print array elements
    {
    	int s=0;
    	for(s=0;s<length;s++)
    	{
    		printf("%d\n",q[s]);
    	}
    }

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    13

    nevermind

    seems this worked
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
     /* function prototypes */
    void bubbleDescend(int *p,int length);
    void printAscend(int *p,int length);
    void bubbleAscend(int *p,int length);
    void printAscend(int *p,int length);
    
    	int integers[20]; /* define array */
    			
    int main(int argc, char * argv[]) /* set up main to take command line arguments */
    {
    		int i=0;
    		  
            srand( (unsigned)time( NULL ) );  /* random seed generator */
            for (i=0; i<20; i++)
            {
                    integers[i]= rand()%1000;	/* fill array slots 0-19 with random
    														   integers 1-1000 */
    			}
              
    				
    	for (i = 1; i < argc; i++)     
      {
        if (argv[i][0] == '-')				/* if commandline input is -a ... */
          if (argv[i][1] == 'a'){
    			printf("\n");
      			bubbleAscend(integers,21);
    			printAscend(integers, 21);
    			printf("\n");
    			}		
          else if (argv[i][1] == 'd')		/* if command line input is =d */
            {
      			bubbleDescend(integers,21);
    			printAscend(integers, 21);
    			}
    			else printf ("Unknown switch %s\n", argv[i]);
        else if (strcmp(argv[i], "name") == 0)
          puts ("Found name");
      }	
    			printf("\n");
    return 0;
    }
     
    void bubbleDescend(int *p,int length) /* Descending sort function */
    {
    	int h,j;
    	for(j=0;j<length;j++)
    	{
    		for(h=0;h<j;h++)
    		{
    			if(p[j]>p[h])
    			{
    				int temp=p[j]; /*swap */
    				p[j]=p[h];
    				p[h]=temp;
    			}
     
    		}
     
    	}
     
    }
     
    void bubbleAscend(int *q, int length) /* ascending sort function */
    {
    	int m,t;
    	for(m=0;m<length;m++)
    	{
    		for(t=0;t<m;t++)
    		{
    			if(q[m]<q[t])
    			{
    				int temp=q[m]; /*swap */
    				q[m]=q[t];
    				q[t]=temp;
    			}
     
    		}
     
    	}
     
    }
    
    void printAscend(int *q, int length) /*print array elements */
    {
    	int s=0;
    	for(s=0;s<length;s++)
    	{
    		printf("%d\n",q[s]);
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  2. How to sort an array?
    By Gerti in forum C Programming
    Replies: 1
    Last Post: 11-26-2005, 04:12 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Random Sort
    By Trebor in forum C++ Programming
    Replies: 1
    Last Post: 06-14-2002, 08:32 AM

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