# Thread: Multi-Dimensional Vector

1. ## Multi-Dimensional Vector

I'm having trouble outputting a multi-dimensional vector that has specific values.

Code:
```void DisplayVect (const vector<vector<int> >& v);

int main(){

const int Max = 7;

vector<vector<int> > v(8,5);

DisplayVect(v);

system("Pause");

}

void DisplayVect (const vector<vector<int> >& v)
{  for (int i = 0; i < v.size(); i++)       // loops through each row of v
{  for (int j = 0; j < v[i].size(); j++) // loops through each element of each row
cout << v[i][j] << " ";           // prints the jth element of the ith row
cout << endl;
}
}```
That code above works fine, but as far as assigning values, this code will add on to the vector of 0's with values from 0 to 6, instead of changing the values that are already printed.
Code:
```for(int i = 0; i < Max; i++){
for(int j = 0; j < Max; j++){
v[i].push_back(j);
}
}```
Thanks for your help.

2. You are using the push_back() member function which was designed precisely to add on one more element to the container.

3. push_back() adds new elements onto the vector. You should use assignment and subscripting like you did in the first loop:
Code:
```void ResetVect (const vector<vector<int> >& v)
{  for (int i = 0; i < v.size(); i++)       // loops through each row of v
{  for (int j = 0; j < v[i].size(); j++) // loops through each element of each row
v[i][j] = 0;
}
}```

4. Thanks, that makes sense about the push_back() part.

One problem with the code, in the ResetVect() function, an error occurs when v[i][j] = 0:
error C3892: 'j' : you cannot assign to a variable that is const
I'm not too sure why that is occurring.

Code:
```void DisplayVect (const vector<vector<int> >& v);
void ResetVect (const vector<vector<int> >& v);

int main(){

const int Max = 8;

vector<vector<int> > v(8,5);

ResetVect(v);
DisplayVect(v);

system("Pause");

}

void DisplayVect (const vector<vector<int> >& v)
{  for (int i = 0; i < v.size(); i++)       // loops through each row of v
{  for (int j = 0; j < v[i].size(); j++) // loops through each element of each row
cout << v[i][j] << " ";           // prints the jth element of the ith row
cout << endl;
}
}

void ResetVect (const vector<vector<int> >& v)
{  for (int i = 0; i < v.size(); i++)       // loops through each row of v
{  for (int j = 0; j < v[i].size(); j++) // loops through each element of each row
v[i][j] = 0;
}
}```

5. If you are modifying the vector, the parameter should not be a reference to const, it should just be a reference.

6. I'm still having some trouble with the program; it will output a 5x8 vector of 0's but that's about it.

What am I missing in my code so that the values in each row will go from 0 to 4?

7. What is your updated code snippet?

8. Forgot about that:

Code:
```...
void DisplayVect (vector<vector<int> >& v);
void ResetVect (vector<vector<int> >& v);

int main(){

vector<vector<int> > v(8,5);

ResetVect(v);
DisplayVect(v);

system("Pause");

}

void DisplayVect (vector<vector<int> >& v)
{
for (int i = 0; i < v.size(); i++){       // loops through each row of v
for (int j = 0; j < v[i].size(); j++){ // loops through each element of each row
cout << v[i][j] << " ";			 // prints the jth element of the ith row
}
cout << endl;
}
}

void ResetVect (vector<vector<int> >& v)
{
for (int i = 0; i < v.size(); i++){      // loops through each row of v
for (int j = 0; j < v[i].size(); j++){ // loops through each element of each row
v[i][j] = 0;
}
}
}```

9. Well, it seems to me that you need to add another function along the lines of ResetVect() to perform this special assignment of numbers for you.

10. Originally Posted by Kramer55
I'm still having some trouble with the program; it will output a 5x8 vector of 0's but that's about it.

What am I missing in my code so that the values in each row will go from 0 to 4?
I'm mot shure that I understood the question ( because the answer is so obvious ).
Code:
```void ResetVect (vector<vector<int> >& v)
{
for (int i = 0; i < v.size(); i++){      // loops through each row of v
for (int j = 0; j < v[i].size(); j++){ // loops through each element of each row
v[i][j] = j;
}
}
}```
Kurt

Popular pages Recent additions