0

I need to take an integer number of columns and rows as input from the command line. This is what i have so far. I just put some random values in my array to check if it was working

 14 int main(int argc, char *argv[])
 15 {
 16 
 17    int random_number;//used for random number
 18    int row, cols;//command line ARGUMENT VARIABLES
 19 
 20    //this section will create random numbers
 21    srand (time(NULL));
 22    random_number = rand() % 100;// 0 to 99
 23    cout << random_number << endl;
 24 
 25    //command line argument
 26    cout << "there are " << argc << " arguments." << endl;
 27    for (int narg = 0; narg < argc; narg++)
 28    {
 29       cout << narg << " " << argv[narg] << endl;
 30    }
 31 
 32    //this section is my array
 33    int print_array[2][2] = {{1,2}, {3,4}};
 34 
 35    for (int row = 0; row < 2; row++)
 36    {
 37       for(int column = 0; column < 2; column++)
 38       {
 39          cout << print_array[row][column] << " ";
 40       }
 41       cout << endl;
 42    }
guesswho
  • 23
  • 1
  • 1
  • 7
  • 1
    http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean is what you are looking for. You'll only need to transform char argv[] into integer – makciook Oct 29 '13 at 13:05
  • ass5.cpp:14: warning: second argument of ‘int main(int, int**)’ should be ‘char **’... ohhh maybe you mean atoi – guesswho Oct 29 '13 at 13:09
  • it's an array of char strings. Leave it like: int main(int argc, char *argv[]). Then cast it to int using atoi, istringstream, sscanf – makciook Oct 29 '13 at 13:11

1 Answers1

0

If you are trying to create a 2D array based in inputs from the command line, you will either need to use dynamic memory (not preferred), or use a container (e.g. std::vector):

int main(int argc, char** argv)
{
    if (argc != 3)
        exit(-1); // we are expecting exactly 3 arguments

    // unsigned because we cannot create negative sized arrays
    std::string sRows = std::string(argv[1]);
    std::string sCols = std::string(argv[2]);
    unsigned int rows = stoul(sRows); // get the first argument
    unsigned int cols = stoul(sCols); // get the second argument

    std::vector<std::vector<int>> print_array;

    // fill the vector here

    // now print the vectors
    std::for_each(print_array.begin(), print_array.end(), [&](const std::vector<int>& vec)
    {
        std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " ");
        std::cout << std::endl;
    });

    return 0;
}
Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • In code `(print_array.begin(), print_array.end(), [&](const std::vector& vec)`, what does `[&](const std::vector& vec)` mean? And I think you miss another right parenthesis at this line. – Jin Chen Oct 29 '13 at 13:35
  • @JinChen That is the lambda syntax introduced in C++11. The last `)` is after the `}` for the lambda block. Instead of creating a lot of 1-off functors, C++11 allows you to create a function inline. Predicates are just one of the uses for them (and one of the primary forces that pushed the standards committee to include it). – Zac Howland Oct 29 '13 at 13:41