Hi guys, i`m currently trying to implement a 2d vector to store x and y of type int.
i have successfully passed it to the function, however i am unable to store the values in it. It always returns with a segementation fault and my program terminates from there. May i know how do i store them properly and call them out?
below is my code snippet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int reconstructSecret(int x, int y, vector< vector<int> > &inVec ,int constructSecret)
{
int getX,getY,formula,accum,count,getSecret,startPosition,nextPosition,numerator,denominator;
getX=x;
getY=y;
int result;
int getSecret2=constructSecret;
inVec.push_back(vector<int> (,getY));
for(int i=0;i<getSecret2;i++)
{
for(int j=0;j<getSecret2;j++)
{
cout<<inVec[i][j]<<endl;
}
}
}
the main method
1 2 3 4 5 6 7 8 9 10 11
vector< vector<int> > inVec;
for(int i=0;i<constructSecret;i++)
{
cout<<"please key in the "<<i<< "share of x value:";
cin>>x;
cout<<"please key in the "<<i<< "share of y value:";
cin>>y;
reconstructSecret(x,y,inVec,constructSecret);
}
What is the comma doing on line 9? Also you are attempting to call the constructor? I am not sure if that is okay to do, but I've never tried it before.
Did you try creating a temporary container outside that's initialized with your variables and then push that to your inVec? I'm pretty sure trying to call any constructor straight up is not good, though I don't remember why.
anyway, is there anyway to store the values that i have gotten from user input
lets say x is 10 and y is 23 , i want to store them in an array that is myArray[0][0] so when i call out position 0 and 0 of the array they will return the values 10 and 23
No unfortunately. Just like with regular arrays, the container will do this:
Array of arrays --[0]--> Array of ints at position 0 --[0]--> int at position 0,0
However, you can have:
Array of arrays --[0]--> Array of ints ----> Access getX and getY stored in array.
But if for some reason you need that extra dimension, then you're going to have to use 3D vector container...