Create 2d array with int input from command line

I need to blend these two items together so that the numbers i enter in my command line argument create a 2d array full of random numbers. how can i do this? thinking i need to use atoi to convert argv to an int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 14 int main(int argc, char *argv[])
 15 {
 16 
 17    int random_number;//used for random number
 18    int row, cols;//command line
 19    int user_cli;//user commmand line input 
 20 
 21 
 22    //this section will create random numbers
 23    srand (time(NULL));
 24    random_number = rand() % 100;// 0 to 99
 25    cout << random_number << endl;
 26 
 27    //command line argument
 28    cout << "there are " << argc << " arguments." << endl;
 29    for (int narg = 0; narg < argc; narg++)
 30    {
 31       cout << narg << " " << argv[narg] << endl;
 32    }
 33 
 34    //this section is my array
 35    int print_array[2][2] = {{1,2}, {3,4}};
 36 
 37    for (int r = 0; r < 2; r++)
 38    {
 39       for(int c = 0; c < 2; c++)
 40       {
 41          cout << print_array[r][c] << " ";
 42       }
 43       cout << endl;
 44    }
 45 return 0;
Last edited on
nobody likes my question. is it hard to read? way off?
Bumping after less than an hour?


So, the user gives two numbers X and Y as command line arguments and and you then create a 2D array with X*Y elements?

Yes, atoi, strtol, etc conversion from const char * to integer is required. First test that you do get enough arguments. Then test that you did get valid X and Y. Then reserve the array dynamically. Finally, initialize the array with numbers and show it.
Topic archived. No new replies allowed.