Thread: Command Line Arguments in C

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Chicago
    Posts
    2

    Question Command Line Arguments in C

    I need to enter 5 words and then sort them alphabetically. The words are zebra, chimpanzee, dog, cat and buffalo. So far I have this, but I don't know how to call the words. Any ideas, help, suggestions, etc would be appreciated!

    Code:
    #include <stdio.h>
    #include <string.h>      
    
     
    int main(int argc, char *argv[5]) {               
        int i = 0;                                   
    	int j;
           
         for (i = 0; i < argc; i++) {
            printf("argv[%d] = %s\n", i, argv[i]);
        }
     int main(void);
    
     int name;
     int hold;
     int last;
    
    	for (i = last ; i > 0 ; i--)  {
             for (j = 1 ; j <= i ; j++)
                if (strcmp(name[j],name[j - 1]) < 0) {
                   strcpy(hold,name[j]); 
                   strcpy(name[j],name[j - 1]);
                   strcpy(name[j - 1],hold);
    			}
    	printf("%s\n", name[0]); 
    	}
    	    return 0;                                     
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Some problems:
    Code:
    int main(void);
    What is this doing here? Delete this line.

    Code:
    if (strcmp(name[j],name[j - 1]) < 0) {
    strcmp compares strings, but you are passing in... well I'm not sure what you are passing in since you are attempting to subscript an integer here.

    This is how you would compare 2 strings:
    Code:
    int cmp = strcmp(argv[1], argv[2]);
    That should give you a hint on how to proceed.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Chicago
    Posts
    2

    Smile

    Thank you!

  4. #4
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    you can use two functions for sort and print array with strings
    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAXLINE  1000   /* maximal length of a line */
    
    /* SortLines:  sort lines for nlines alphabetically */
    void SortLines(char lines[][MAXLINE], unsigned nlines)
    {
        int i, j;
        char tmp[MAXLINE];
        
        for (i = 0; i < nlines; i++)
            for (j = i+1; j < nlines; j++)
                if (strcmp(lines[i], lines[j]) > 0) {
                    strcpy(tmp, lines[i]);
                    strcpy(lines[i], lines[j]);
                    strcpy(lines[j], tmp);
                }
    }
    
    /* PrintLines:  print lines for nlines */
    void PrintLines(char lines[][MAXLINE], unsigned nlines)
    {
        int i;
        
        for (i = 0; i < nlines; i++)
            printf("%s\n", lines[i]);
    }
    but I recommend you to copy args first to temporary array for use them by it and sort only this array, not the arglist

  5. #5
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    There's no need to come up with bubblesort since C already has qsort(), which yields much better asymptotic runtime in the average case and works perfectly well on arrays.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Snafuist View Post
    There's no need to come up with bubblesort since C already has qsort(), which yields much better asymptotic runtime in the average case and works perfectly well on arrays.

    Greets,
    Philip
    Sure but if you can't write a bubblesort in C blindfolded then go out back and shoot yourself now, or at least do not pass GO until you can.

    It is kind of like using recursion to do factorials or fibronacci numbers; no, you don't have to and somebody else already came up with an easier way (why code in C at all?)

    Also, on short lists that are almost or mostly sorted (which in a list of ten items, might be that way by chance) a bubblesort can be a better option than a quicksort.
    Last edited by MK27; 04-27-2009 at 10:37 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Snafuist View Post
    There's no need to come up with bubblesort since C already has qsort(), which yields much better asymptotic runtime in the average case and works perfectly well on arrays.

    Greets,
    Philip
    Have you considered the fact that this is probably a homework assignment?

  8. #8
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    when you write without qsort, you can translate the code to another languages
    there is exchange sort (not bubble)

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by c.user View Post
    when you write without qsort, you can translate the code to another languages
    there is exchange sort (not bubble)
    There's also insertion sort, selection sort and (my favourite!) mergesort, which is actually better than quicksort unless you spent too much time with abstract analysis
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    it is only exchange the elements without any algorithms like binary-search or something else
    it is very simple to understand
    Last edited by c.user; 04-27-2009 at 09:44 PM.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by c.user View Post
    it is only exchange the elements without any algoritms like binary-search or something else
    it is very simple to understand
    It's not that there is no algorithm, it's more like that no one could or should bother to describe a bubble sort as something resembling math.

    Maybe I am wrong about that but you are right to say it is very simple; I think most people who are not taught sorting before they need to do it will actually impliment a bubble sort intuitively. The hard part about this assignment is the "alphabetically" part.
    Last edited by MK27; 04-27-2009 at 09:12 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Mk27,
    Quote Originally Posted by my
    algorithms like binary-search or something else

  13. #13
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    ......dup......

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    First off, do you have to input the words and store them in an array or are each words assigned to an index of an array from the get go?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM