Thread: command line arguments

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    19

    command line arguments

    Will someone please help me out. I've been working on this code for 6 hours and am still lost! I am supposed to use 2 command line arguments to determine the number of strings to be used in this program and the maximum length for those strings. Then my function should create an array of strings, which then calls a RandomString function to create each individual string. Next, my RandomString function should create one string of random characters each time it is called, with the number of characters in the string being a random value between one and the value of the parameter passed in from the main function. I dont think Im close, but could someone tell me what I am doing wrong, please? Im not looking for answers, just help! The code I have written is basically just a guess. Thank you in advance!

    Code:
    int main(int argc, char *argv[]){ 
    
      int num; 
    
      while(argc >= 1){ 
        printf("Enter a number of random strings to be generated:%d\n:", num); 
        printf("Enter the maximum length of characters", 
               " for the strings:%d\n", argc); 
      } 
      argv = calloc(num, argc); 
      for(num = 0; num < argc; num++) 
        RandomString(argv[num], argc); 
    
    } /* End of main function */ 
    
    void RandomString(int argc, char *argv[]){ 
    
      int i, temp; 
    
      srand(time(NULL)); 
    
      for(i = 0; i < argc; i++){ 
        temp = atoi(argv); 
          argv[i] = temp; 
    
      } /* End of for statement */ 
    } /* End of RandomString function */

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This is the basic idea for making the list of strings (error checking omitted):
    Code:
    int main ( int argc, char *argv[] )
    {
      char **string_list;
      int i;
      int n;
      int len;
    
      if ( argc != 3 ) {
        fprintf ( stderr, "usage: $ prog <n strings> <max len>\n" );
        return EXIT_FAILURE;
      }
      n = atoi ( argv[1] );
      len = atoi ( argv[2] );
      string_list = malloc ( n * sizeof *string_list );
      for ( i = 0; i < n; i++ )
        string_list[i] = calloc ( len, sizeof *string_list[i] );
    
      /* Work with your new array of strings */
    
      for ( i = 0; i < n; i++ )
        free ( string_list[i] );
      free ( string_list );
    
      return EXIT_SUCCESS;
    }
    For RandomString, something like this I suppose, you can get more efficient using table lookup, but I'm too lazy to type that up
    Code:
    #include <ctype.h>
    #include <limits.h>
    
    void RandomString ( char *string, int limit, int high )
    {
      int i;
    
      for ( i = 0; i < limit; i++ )
        string[i] = rand() % high;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    19
    Thanks prelude! Is it possible to do this instead?:

    Code:
    #include <ctype.h>
    #include <limits.h>
    
    void RandomString ( char *string, int argc ) 
    /* Or in place of int argc, use int num */
    {
      int i;
    
      for ( i = 0; i < argc; i++ )
        string[i] = rand() % argc;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by chauncey005
    Thanks prelude! Is it possible to do this instead?:

    Code:
    #include <ctype.h>
    #include <limits.h>
    
    void RandomString ( char *string, int argc ) 
    /* Or in place of int argc, use int num */
    {
      int i;
    
      for ( i = 0; i < argc; i++ )
        string[i] = rand() % argc;
    }
    Sure, that will work. However, whatever your value for argc, -1, is is going to be the maximum number you end up putting in your string. So if you want a better range of numbers you'll need to do something like Prelude stated. But yeah, you can make it be whatever you want.

    As for the name, the variable name in the function declaration can be whatever you want it to be. You can call it 'foo' for all it cares. It's just giving some name for the integer for your reference in the function. Usually people name their variables something useful so they know at a glance what it's for.

    But C itself doesn't care what you call it, with exception to calling it reserved words.

    Quzah.
    Hope is the first step on the road to disappointment.

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. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM