# Move a number down a vector

Posted 10 October 2019 - 09:45 AM

Hey all,

I know this is super trivial to most of you, but I'm having some trouble with my logic here.
I am trying to move a the number "1" down a vector length 6. so it will start of with all zeros and move that "1" down.

Example:
```0 0 0 0 0 0,
1 0 0 0 0 0,
0 1 0 0 0 0,
0 0 1 0 0 0,
0 0 0 1 0 0,
0 0 0 0 1 0,
0 0 0 0 0 1
```

After that, theres another vector length 12 that continues after the "1" position in the previous vector reaches position 6.

Example: 0 0 0 0 0 0 | 1 0 0 0 0 0 0 0 0 0 0 0, 0 0 0 0 0 0 | 0 1 0 0 0 0 0 0 0 0 0 0, 0 0 0 0 0 0 | 0 0 1 0 0 0 0 0 0 0 0 0 keep moving that "1" down until every entry is "0" except the 12th position.

Here's what I have done. It just gives me all "1's" when I run it.

```#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
vector<int> dBodyVelocity(6,0);
vector<int> qdd(12,0);

for(int x=0; x<18; x++)
if( x<6 ){
dBodyVelocity[x]=1;
dBodyVelocity[x-1]=0;
if (x==5){
dBodyVelocity[x]=0;
}
for(int y=0;y<6;x++)
cout << dBodyVelocity[y] << " ";
}
else{
qdd[x]=1;
qdd[x-1]=0;

// for (int x : dBodyVelocity)
// cout << x << " ";

//for (int x : qdd)
//cout << x << " ";

return 0;
}
}

```

Any help would be greatly appreciated! Thanks!

Is This A Good Question/Topic? 0

## Replies To: Move a number down a vector

### #2 modi123_1 • • Suitor #2
•    Reputation: 15318
• Posts: 61,423
• Joined: 12-June 08

## Re: Move a number down a vector

Posted 10 October 2019 - 09:51 AM

Riddle me this - when your index, 'x', is 0 at the first iteration of the loop.. and this line is asking for a -1 index in 'dBodyVelocity', what do you believe happens?

```15        dBodyVelocity[x-1]=0;
```

Using 'y', but incrementing 'x'?

```19    for(int y=0;y<6;x++)
```

## Re: Move a number down a vector

Posted 10 October 2019 - 09:53 AM

You're correct, I didn't know a better way to do set the last value to zero.

I honestly don't know what my reasoning was there. I just thought of running another loop with y increments to generate the vector at each position.

### #4 modi123_1 • • Suitor #2
•    Reputation: 15318
• Posts: 61,423
• Joined: 12-June 08

## Re: Move a number down a vector

Posted 10 October 2019 - 09:55 AM

IF you want to set a value, but only when a condition is not something, wouldn't an IF statement fit?

```IF CONDITION THEN
do what ever
```

What sort of condition would check to make sure that math is not out of bounds for a vector's index?

As for the for loop - typically you would increment the loop control variable. In this case 'y'.

## Re: Move a number down a vector

Posted 10 October 2019 - 10:01 AM

Would in this case

```if( x<6 & x!=0){
dBodyVelocity[x]=1;
dBodyVelocity[x-1]=0;
if (x==5){
dBodyVelocity[x]=0;
}

```

work?

I'm trying to write this in the simplest way possible. I feel like the route im taking now just makes a simple thing like this very messy.

### #6 modi123_1 • • Suitor #2
•    Reputation: 15318
• Posts: 61,423
• Joined: 12-June 08

## Re: Move a number down a vector

Posted 10 October 2019 - 10:01 AM

What happens when you try it?

My preference is keep the IF statement relative to the specific line it is dealing with.

## Re: Move a number down a vector

Posted 10 October 2019 - 10:11 AM

According to my (clearly flawed) logic this:

```int main()
{
vector<int> dBodyVelocity(6,0);
vector<int> qdd(12,0);

for(int x=0; x<18; x++)
if( x<6 & x!=0){
dBodyVelocity[x]=1;
dBodyVelocity[x-1]=0;
if (x==5){
dBodyVelocity[x]=0;
}
}
else{
qdd[x]=1;
qdd[x-1]=0;

cout << dBodyVelocity[x] << " ";

return 0;
}
}

```

Should give me something that looks like this

```0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

```

But the answer I get is a "0" So confused.

I think that was because of the return 0;

If I take that out I get

```0 0 0 0 0 0 0 0 0

```

Actually no, I'm only getting the output of dbodyvelocity there. Which is a length 6 vector. why are there 9 elements in that?

## Re: Move a number down a vector

Posted 10 October 2019 - 10:18 AM

Okay, I changed it now to 2 for loops for each vector

```using namespace std;

int main()
{
vector<int> dBodyVelocity(6,0);
vector<int> qdd(12,0);

for(int x=0; x<6; x++)

if( x<6 & x!=0){
dBodyVelocity[x]=1;
dBodyVelocity[x-1]=0;
if (x==5){
dBodyVelocity[x]=0;
}
cout << dBodyVelocity[x] << " ";
}

for(int y=0; y<12; y++)
if( y<12 & y!=0){
qdd[y]=1;
qdd[y-1]=0;
cout<<qdd[y]<< " ";
}

}

```

output is:

```1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1

```

But according to what I'm trying to do it should be:
```0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

```

### #9 modi123_1 • • Suitor #2
•    Reputation: 15318
• Posts: 61,423
• Joined: 12-June 08

## Re: Move a number down a vector

Posted 10 October 2019 - 10:49 AM

You are going down a wrong path. Again, you need to be checking the specific line I mentioned.. and checking the specific condition that would make sure to not have a negative when x-1.

## Re: Move a number down a vector

Posted 10 October 2019 - 10:52 AM

Okay, yes but I don't seem to completely understand. Please see code below:

```using namespace std;

int main()
{
vector<int> dBodyVelocity(6,0);
vector<int> qdd(12,0);

for(int x=0; x<6; x++){

if( x<6 && x!=0){
dBodyVelocity[x]=1;
dBodyVelocity[x-1]=0;
if (x==5){
dBodyVelocity[x]=0;
}

}
cout << dBodyVelocity[x] << " ";
}

for(int y=0; y<12; y++){
if( y<12 && y!=0){
qdd[y]=1;
qdd[y-1]=0;
}
cout<<qdd[y]<< " ";
}
}

```

Output is:
```0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1

```

I think my logic here is correct, am I right ?

### #11 modi123_1 • • Suitor #2
•    Reputation: 15318
• Posts: 61,423
• Joined: 12-June 08

## Re: Move a number down a vector

Posted 10 October 2019 - 11:29 AM

Certainly not getting the response you were indicating would be correct, so I would say 'no'.

Go back to your original code and fix the two lines I mentioned. The loop control variable on one, and a single if statement for a single line to check the lower bounds on the other.

## Re: Move a number down a vector

Posted 11 October 2019 - 03:16 AM

2. If you set dBodyVelocity[x] to 1 and then display it in the same iteration, then of course you're going to see a string of 1's. You've just set it to 1, haven't you?
3. Moving one number at a time is not a huge problem, but if you had to shift the contents of a vector to the left or to the right, using std::rotate would probably be a better solution.
4. That said, here's my modification to your last piece of code that should help you see what each of your vectors looks like at each iteration:

```#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void printVector(const vector<int>& vec)
{
for (int val : vec) {
cout << val << " ";
}
cout << endl;
}

int main()
{
vector<int> dBodyVelocity(6, 0);
vector<int> qdd(12, 0);

for (int x = 0; x < 6; x++) {
if (x < 6 && x != 0) {
dBodyVelocity[x] = 1;
dBodyVelocity[x - 1] = 0;
if (x == 5) {
dBodyVelocity[x] = 0;
}
}
printVector(dBodyVelocity);
}

for (int y = 0; y < 12; y++) {
if (y < 12 && y != 0) {
qdd[y] = 1;
qdd[y - 1] = 0;
}
printVector(qdd);
}
}
```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }