# operating with vectors

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-03-2010
kraghavan
operating with vectors
Hi all
I have a2d vector say
vector<vector<int> > V ( 5,vector<int>(5)); // 25 size

Suppose i want to do this operation. if the last element in vector[i] and the first element of v[i+1] differ by a value greater than 20..., i want to push the v[i+1].front() element into v[i].back() position in the array.
how to do it?

i wrote a code like this but got segmentation error. please help me how to design this one!!

Code:

```int main (void) {         vector <vector<int> > v (5,vector<int>(5));         vector<int>::iterator it;         int rnd;         for (int i=0;i<5;i++)         {                 for ( int j=0;j<5;j++)                 {                 rnd = rand() % 100;                 v[i][j]= rnd;                 //cout<<v[i][j]<<" ";                 }         }                 for (int i=0;i<5;i++)         {                 if ( ( (v[i].back() )-(v[i+1].front() ) ) >= 20)                 {                         //cout<<"ok!"<<endl;                                 v[i].push_back(v[i+1].front());                                 v[i].resize(v[i].size()+1);                                 v[i+1].resize(v[i+1].size()-1);                                }         } return 0; }```
i know there is a flaw but the logic iw ant to achieve is simple. please suggest how it shud be rewrote.
• 06-03-2010
whiteflags
It seems that you have a segfault because this for loop takes too many steps:

Code:

```for (int i=0;i<5;i++)         {                 if ( ( (v[i].back() )-(v[i+1].front() ) ) >= 20)                 {                         //cout<<"ok!"<<endl;                                 v[i].push_back(v[i+1].front());                                 v[i].resize(v[i].size()+1);                                 v[i+1].resize(v[i+1].size()-1);                                }         }```
Trying to push_back() elements from a vector nonexistent. Actually, you are accessing another, inner vector outside of the outer vector's bounds. To fix it you have to keep i + 1 in bounds. Related to the problem, you need to stop using magic numbers for your for loops and stuff. The size() function is fast and appropriate for controlling for loops.

Also, I can't figure out why you are resizing. What are you trying to do there?
• 06-03-2010
iMalc
You don't push_back and then resize by size()+1 push_back already increases the size of the container. You should also use pop_front.
• 06-04-2010
kraghavan
OK i got your point. But i tried this way. i gives me a segmentation fault...whats the problem here can any1 help me??
I will be greatful.

Code:

```#include <iostream> #include <vector> #include <time.h> using namespace std; int main (void) {         time_t tm;         time(&tm);         srand(tm);         vector <vector<int> > v (5,vector<int>(5));         vector<int>::iterator it;         int rnd;         for (int i=0;i<5;i++)         {                 for ( int j=0;j<5;j++)                 {                 rnd = rand() % 100;                 v[i][j]= rnd;                 //cout<<v[i][j]<<" ";                 }         }         int k;         for (int i=0;i<4;i++)         {                 while (abs( ( (v[i].back() )-(v[i+1].front() ) ) < 30))                 {                                 k = v[i+1].front();                                 v[i+1].erase(v[i+1].begin() );                                 v[i].push_back(k);                        }         }                 for (int i=0;i<v.size();i++)         {                 //cout<<v[i].size()<<" ";                 for(int j=0;j<v[i].size();j++)                 {                         cout<<v[i][j]<<" ";                 }                 cout<<endl;         }         cout<<endl; return 0; }```
• 06-04-2010
Elysia
This modification works for me:
Code:

```        for (int i=0;i<4;i++)         {                 auto BackE = v[i].back();                 auto FrontE = v[i+1].front();                 if (abs(BackE - FrontE) < 30)                 {                         k = v[i+1].front();                         v[i+1].erase(v[i+1].begin() );                         v[i].push_back(k);                        }         }```
I don't know if the program is 100% right, but I believe the output seemed correct.
• 06-04-2010
kraghavan
No i tried IF condition... but u see i need the elements to be pushed into prev array as long as they are less than 30 of difference. if does it once... can you suggest how can handle continous shifts using while?
thanks
• 06-04-2010
whiteflags
If you want to change it so that the same vectors will be compared, lengthened, and shortened respectively, you can do that. It's just that you won't bump the loop count until the comparison between the two elements fails. The abs(v[i].back () - v[i + 1].front ()) < whatever comparison is still only a comparison.

The output might look like this:
Code:

```vector[0] = ( 37, 20, 32, 70, 63, ). vector[1] = ( 31, 95, 49, 93, 38, 24, 30, 12, ). vector[2] = ( 90, 74, ). vector[3] = ( 36, 83, 27, 79, 74, 70, 51, 60, 55, ). vector[4] = ( 96, ).```
Isn't that what you expect?
• 06-04-2010
laserlight
You also have to be careful of what happens if a vector gets reduced to being empty.
• 06-04-2010
kraghavan
yeah thanks.. thats what i expect... but how to handle it when the vector becomes empty... i want the next set of vector to be pushed forward... can you suggest what to code when the size of v[i] becomes 0.... i want to remove it... how to do it using iterators?

thanks
regards,
KR
• 06-04-2010
kraghavan
i got the segment fault only becuz the size of the vecotr became zero and the system dint know what to do...
I want help in given telliing the system to move forward when the size of i+1 becomes zero...
regards
• 06-04-2010
laserlight
Test for v[i + 1].empty()
• 06-08-2010
kraghavan
Hey i tried the problem using this.. can you tell me if the flow of logic is right in my steps?.. would appreciate any help...
Code:

```do                 {                         if (abs( v[i].back() - v[i+1].front() ) < 5)                         {                                 k = v[i+1].front();                                 v[i].push_back(k);                                 v[i+1].erase(v[i+1].begin() );                                 i++;                                }                 }while ((i<5) && (!v[i].empty()) ); // 5 is the size of the vector of vectors.. 5*5 as i said before...```
• 06-08-2010
EVOEx
Quote:

Originally Posted by Elysia
This modification works for me:
Code:

```        for (int i=0;i<4;i++)         {                 auto BackE = v[i].back();                 auto FrontE = v[i+1].front();                 if (abs(BackE - FrontE) < 30)                 {                         k = v[i+1].front();                         v[i+1].erase(v[i+1].begin() );                         v[i].push_back(k);                        }         }```
I don't know if the program is 100% right, but I believe the output seemed correct.

Auto? Isn't that C++0x only?
I love the feature though, it's going to make some things a lot easier!
• 06-08-2010
Elysia
Yes, it is. There are available compilers that supports it.
• 06-08-2010
EVOEx
Quote:

Originally Posted by Elysia
Yes, it is. There are available compilers that supports it.

Yes, though I don't think it's a good idea suggesting to use it to people who don't fully know what they're doing...
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last