Reputation: 23135
I want to pass a 2d int array into a main function.
Since the main function signature is like this
int main(int argc, char*argv[]
How can I get the 2d array into a int[][]
in my code?
Command line:
myprogram {{2,3},{5,6},{4,5},{6,8},{2,5}}
Upvotes: 0
Views: 362
Reputation: 38234
You need to parse the command line input that you give to the program. That might be slightly involved.
What you can do is--since you know that that array is gonna be a 2D array. Pass the command line input like this:
myprogram < number of rows(r) > < number of columns(c) > n_1 n_2 n_3 .... n_(r*c)
where n
s are the elements of your array in 'row-major' fashion.
In this way you code to read the command line input will be much simpler.
Otherwise if you want to retain the format of input. You have to build a 'small' parser to read those input. Basically you have 5 'kinds' of characters that you need to deal with:
Handling space will be little bit tricky because with each space
in the command line input argc
increments by one.
EDIT: You may follow upon these lines. NB: This code may have errors and some logic also needs to be checked, but this can be a starting point for you to start parsing the command line input in the format that you have given.
int main()
{
int count = 1;
std::vector<vector<int>> arr2d;
std::vector<int> arr1d;
std::string buf;
while(count<argc) {
//accumulate what all you get from command line into string
buf += argv[count];
count++;
}
std::string curr_num;
int num_elements;
int i = 0;
int lparen=0,rparen=0,nrows=0;
char c;
while(i<buf.length()){
c = buf[i];
switch(c) {
case '{':
lparen++;
break;
case '}':
rparen++;
nrows++;
arr2d.push_back(arr1d);
arr1d.clear();
break;
case ' ':
break;
case ',':
num_elements++;
if(curr_num.length() != 0) {
//i'm not sure this will work but you have to do something similar
arr1d.push_back((int)curr_num.c_str());
}
else {//do some error handling}
case '0':
//....
case '9':
curr_num += buf[i];
break;
default:
//throw error;
//there might be other checks you may feel like doing
//you'll get better idea as you start writing on your own.
}
i++;
}
if(lparen || rparen)
//throw error ...
//...
return 0;
}
Upvotes: 0
Reputation: 3873
The main()
function takes either nothing or command-line arguments as char *
s - you can't just give it whatever you please! You would have to parse the char *
to get the data you need.
Do you have to enter information in that fashion, though? If it were entered like this (just through cin
, not command-line arguments:
1 2 3
4 5 6
7 8 9
You could assign it to an int a[][]
using something like this:
int main()
{
const int X=3, Y=3;
int a[X][Y];
string currentLine;
for(int i = 0; i != X; ++i) {
getline(cin, currentLine);
istringstream currentInput(currentLine);
for(int j = 0; j != Y; ++j)
currentInput >> a[i][j];
}
return 0;
}
Upvotes: 0
Reputation: 26783
This: {{2,3},{5,6},{4,5},{6,8},{2,5}}
will end up as a string in your program. You will need to break it up into pieces yourself to turn it into an array of arrays.
Upvotes: 5