none
Move a number down a vector RRS feed

  • Question

  • 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; 
    } 
    }
    



    Thursday, October 10, 2019 4:31 PM

All replies

  • I changed it to 2 loops now 

    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 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:
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

    Thursday, October 10, 2019 5:00 PM
  • Your main problem is that you have used & instead of && in your if statements.  Those are two very different operators.  Also note that "if( x < 6" and "if( y < 12" in your loops are silly.  Neither of those conditions can ever happen, because the "for" statement won't allow it.

    You should think about code reuse.  The whole advantage of using a container like a vector is that you don't need to hard code things like the "6" and "12".  So:

    #include <fstream>
    #include <iostream>
    #include <vector>
    
    void PrintVector( const std::vector<int> & vec )
    {
        for( auto i : vec )
            std::cout << i << " ";
        std::cout << "\n";
    }
    
    void SlideOnes( std::vector<int> & vec )
    {
        for( int i = 0; i < vec.size(); i++ )
        {
            if( i > 0 )
                vec[i-1] = 0;
            vec[i] = 1;
            PrintVector( vec );
        }
    }
    
    int main()
    {
        std::vector<int> dBodyVelocity(6,0);
        std::vector<int> qdd(12,0);
    
        SlideOnes( dBodyVelocity );
        SlideOnes( qdd );
    }
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Thursday, October 10, 2019 5:19 PM
  • You're a legend mate. I'm going to go over this and get back here if I have any questions.

    Can you please see if there is a mistake in my logic here:

    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

    Which technically seems correct. It's just that how would I get the output here to look like what you have in your code?

    Thursday, October 10, 2019 5:24 PM
  • Actually, I should restate.  The & vs && thing is an issue, but it's not your main problem.  The problem is the design of your code.  Think about your pseudo-code:

        for every element in the vector
           set the element to 1
           if possible, set previous element to 0
           print the element

    Naturally, that loop is going to print a set of 1s.  I think what you wanted is what MY code did; you want to print the whole vector after every iteration, not just print the last element (which you just set to 1).


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Thursday, October 10, 2019 5:24 PM
  • Okay yes, completely agreed. If not too much trouble, can you please see my new code? Thanks!

    I guess what i'm asking is, does my logic there seem correct. Also will changing the position of the cout to after the assignment of the "1" print out every iteration of the vector?

    • Edited by tes64dk Thursday, October 10, 2019 5:28 PM
    Thursday, October 10, 2019 5:26 PM
  • Also can you please explain what is happening here. I've never seen "auto" before. Is there a more "primitive" way to do this section.

    void PrintVector( const std::vector<int> & vec )
    {
        for( auto i : vec )
            std::cout << i << " ";
        std::cout << "\n";
    }


    Thursday, October 10, 2019 5:36 PM
  • Also can you please explain what is happening here. I've never seen "auto" before. 

    void PrintVector( const std::vector<int> & vec )
    {
        for( auto i : vec )
            std::cout << i << " ";
        std::cout << "\n";
    }


    Range-based for Statement (C++)
    https://docs.microsoft.com/en-us/cpp/cpp/range-based-for-statement-cpp?view=vs-2019

    What is the correct way of using C++11's range-based for?
    https://stackoverflow.com/questions/15927033/what-is-the-correct-way-of-using-c11s-range-based-for

    auto (C++)
    https://docs.microsoft.com/en-us/cpp/cpp/auto-cpp?view=vs-2019

    - Wayne

    Thursday, October 10, 2019 5:57 PM
  • You're a legend mate. I'm going to go over this and get back here if I have any questions.

    Can you please see if there is a mistake in my logic here:

    int main() { vector<int> dBodyVelocity(6,0); vector<int> qdd(12,0); for(int x=0; x<6; x++){ if( x<6 && x!=0){

    Can the first half of this if ever be false?

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

    When x is 0, you skip the preceding big if statement and immediately execute this cout statement. This will print the value of element 0 but this element might be changed in the next iteration when x is 1.

      }
      
    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

    Which technically seems correct. It's just that how would I get the output here to look like what you have in your code?


    Thursday, October 10, 2019 6:19 PM
  • Hello,

    Is your problem solved? If so, please mark the useful replies as answers, so that it will help other members to find solution quickly if they faces similar issue. If you have anything else about this issue, please feel free to contact us.

    Best Regards,

    Suarez Zhou

    Tuesday, October 15, 2019 7:10 AM
  • Why are you worried about "primitive"?  You're using a std::vector, which is not a "primitive" type.  Let the compiler work for you, not vice versa.

    When you are iterating through a vector, the type of the iterator can be rather length:
        std::vector<int>::iterator
    That's a lot of typing.  The compiler can figure out what the type needs to be.  The "auto" statement says "I don't want to type it, so you figure out the type of this variable."  That way, if I change the vector to a vector of floats, or a std::list of longs, I don't have to change my code.

    I explained the problem with your "new" code in my second post.  You are printing exactly one item each time through the loop -- the last item, which is always 1.  You need to be printing the entire vector in every iteration.

    And you should NOT be hardcoding the vector bounds (6 and 12) in the loops.  That is just a bug waiting to happen.  If you really don't want to use the range-based for statement (and you SHOULD use it), you can do

        for( int i = 0; i < qdd.size(); i++ )

    Learn to write survivable code.  When you hard-code the array bounds, your code will break when the vector grows in a later modification.  Unlike normal C arrays, a std::vector knows its length.  Let it tell you.  Don't assume.

    As a general rule, any time you see an arbitrary integer constant in your code (other than 0), you should question it.  Many companies forbid those in their code reviews.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, October 15, 2019 6:12 PM