-
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 */
-
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;
}
-
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;
}
-
Quote:
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.