argc & argv

This is a discussion on argc & argv within the C Programming forums, part of the General Programming Boards category; Hello there, Iím having problem with argc and argv. The program suppose to work in such a way that once ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    Question argc & argv

    Hello there,

    Iím having problem with argc and argv.
    The program suppose to work in such a way that once an input from the argc it will display the srand num.

    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {
            int i;
    
            printf("%d\n", argc);
            for(i=0; i<argv; i++)
    
            if(argc > 1)
            {
              int num;
              srand(time(NULL));
              while(num-- > 0)
                    printf("%d ", rand() % 10);
            }
            return(0);
    }
    The code above, doesn't seems to be right. Which I'm aware but having trouble understanding and combining different functions and arguements as a whole. As for now, I do understand how it works individually.

    Code:
    "argv & argc"
    #include <stdio.h> int main(int arc, char *argv[]) { int i; printf("%i\n", argc); for(i=0; i< argc; i++) printf("%s\n", argv[i]); return(0); }
    "srand num"
    #include <stdio.h> #include <stdlib.h> #include <time.h> int main (void) { int num=10; srand(time(NULL)); while(num-- > 0) printf("%d ", rand() % 10); return(0); }
    Really appreciate any help.
    Thanks in advance!

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I'm not sure what your problem is. In argv strings are stored. If you want to convert the strings to numbers, you can use the function atoi(), which is in stdlib.h. If you want to use the numbers as stored in argv then you could do something like this:

    Code:
    for (i = 1; i < argc; i++)
    {
      /* Convert i-th string in argv to an integer value. */
      num = atoi (argv [i]);
    }
    Regards,
    Shiro

  3. #3
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Well, first you're comparing "i" with "argv" when I think you mean to compare it with "argc". "argv". Second, you're not initializing "num" which is why it will only print once. Perhaps this is what you mean:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {
        int i;
        printf("argc: %d\n", argc);
    
        for (i = 0; i < argc; i++) {
            srand(time(NULL));
            printf("%d ", rand() % 10);
        }
        return 0;
    }
    This prints something along the lines of when run with the arguments "a b c d":
    Code:
    argc: 5
    9 9 9 9 9

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Thanks for your help Nessarose!

    However, the program is not functioning to what is being asked for.

    I'm using gcc compiler for my program, after i typed
    ./numList 5 20 (it should appear...by random)
    5
    6
    11
    3
    18

    Note: 5 indicates how many times it should display and 20 is the range of number from srand.
    It should work more or less like the attached code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (void)
    {
      int num=10;
      srand(time(NULL));
     
      while(num-- > 0)
        printf("%d ", rand() % 10);
    
     return(0);
    }
    Now, my problem is how to display range of srand num using the command line argument. Does this make sense?

    Any help?
    Thanks!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Nessarose
    Code:
    int main(int argc, char *argv[])
    {
        int i;
        printf("argc: %d\n", argc);
    
        for (i = 0; i < argc; i++) {
            srand(time(NULL));
            printf("%d ", rand() % 10);
        }
        return 0;
    }
    This prints something along the lines of when run with the arguments "a b c d":
    Code:
    argc: 5
    9 9 9 9 9
    Well, your output is all the same, because you're using srand incorrectly. You should only ever call srand once in your program. Call it outside the loop, before you call rand.

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

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by miryellis
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (void)
    {
      int num=10;
      srand(time(NULL));
     
      while(num-- > 0)
        printf("%d ", rand() % 10);
    
     return(0);
    }
    Now, my problem is how to display range of srand num using the command line argument. Does this make sense?

    Any help?
    Thanks!
    First off, do you actually know how to make rand display a range, rather than just "0 - 9" like you've shown above? That's your first problem.

    The second problem is that you need to use something like atoi to convert the arguments into numbers. Then you use those numbers in the forumulae for your range generation.

    I'll leave that up to you. Give it a shot, and see what you can come up with. Do it in the two steps as I've suggested.
    1) Make a program that will generate a range. Search the board for random numbers or rand and I'm sure you'll find examples of its use for ranges.
    2) Make a program that will convert numbers from their string form to an actual integer.
    3) Make a program that combines 1 and 2.

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

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    You also shouldn't use time() to seed your random numbers. time() returns type time_t, which by standard does not have to be an arithmitec type. srand() takes an unsigned int; it could on some systems be not possible to convert the return value of time() to an unsigned int. For example:
    Code:
    /*  Somewhere in time.h */
    typedef struct {
          int hours; /* Hours from Midnight January 1, 1970 */
          int second; /* Seconds plus hours will result in tiem from Midnight January 1, 1970 */
    } time_t;
    /* Somewhere in your code */
    srand(time(NULL));  /* Won't work */
    Get the point?
    Last edited by chrismiceli; 09-19-2004 at 10:34 PM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    i followed quzah and chris, then write this program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {
        int i;
        int num;
        printf("argc: %d\n", argc);
        num=atoi(argv[1]);
    
        printf("argv[1] is the paramenter represents the number u wanna print.\n");
        printf("argv[1]=%d\n",num);
    
        srand(time(NULL));
    
        for (i = 0; i < num; i++) {
            
            printf("%d ", rand() % 10);
        }
        return 0;
    }

    i used gcc to compile and exexute,and get the right result.
    if any potential problems,correct me.

  9. #9
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    You didn't yield to my resonse. If you really want to use the current time to seed your random numbers you can get creative with some time.h functions, but you can't use directly time()'s return value portably.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by chrismiceli
    You didn't yield to my resonse. If you really want to use the current time to seed your random numbers you can get creative with some time.h functions, but you can't use directly time()'s return value portably.
    If you type cast it you can.

    Quote Originally Posted by bideyore
    i followed quzah and chris, then write this program:

    ...snip...

    i used gcc to compile and exexute,and get the right result.
    if any potential problems,correct me.
    If you want to take it a step further, use two command line arguments, one for the minimum number, and one for the top number, and generate a number randomly between those two.

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

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Thank you for all the prompt replies...

    I will read on atoi as quzah suggested and check on bideyore's code too.

    Back in here once I "trip" on it again.
    Thanks you guys!

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    i am reading the FAQ to seek a better way of generating random numbers : )
    here i changed a little to use 3 arguments: argv[1]: numbers to print; argv[2]: minimum number;argv[3]:top number;
    i also add a little function to check the number of the arguments.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void GenRandNum(char* argmin, char* argmax, char* argnum)
    {
        int i;
        int min,max,num;
        min=atoi(argmin);
        max=atoi(argmax);
        num=atoi(argnum);
    
        srand(time(NULL));
    
        for (i = 0; i < num; i++)
        {
            printf("%d ", (min + ( rand() % max )) );
        }
    }
    
    int CheckArg(int argc)
    {
        if(argc == 4)
        	return 1;
        else 
            return 0;
    }
    
    
    
    void print()
    {
        printf("use arg[1] to show how many numbers u wanna create.\n");
        printf("use arg[2] and arg[3] to specify ur range.\n");
    }
    
    int main(int argc, char *argv[])
    {
        if ( CheckArg(argc) )
        	GenRandNum(argv[2],argv[3],argv[1]);    
        else
    	print();
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help getting a grib on argc and argv
    By elwad in forum C Programming
    Replies: 10
    Last Post: 05-01-2009, 11:13 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  3. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 05:47 PM
  4. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 11:17 AM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-09-2001, 12:04 AM

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