Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone tell me what\'s wrong with my code? It doesn\'t response the requirem

ID: 3694843 • Letter: C

Question

Can anyone tell me what's wrong with my code? It doesn't response the requirement.

/**
   * Returns new vector with elements shifted right by a given number of positions.
   */
   public Vector shifted(long amount) {

       /*
           to do

           [1 2 3 4] 0 => [1 2 3 4]
           [1 2 3 4] 1 => [4 1 2 3]
           [1 2 3 4] 2 => [3 4 1 2]
           [1 2 3 4] 3 => [2 3 4 1]
           [1 2 3 4] 4 => [1 2 3 4]
           [1 2 3 4] 5 => [4 1 2 3]
       */
       Vector vector = new Vector(length);
       vector = this.cloned();
       amount = amount % length;
       while(amount-- > 0){
           long tmp = vector.elements[0];
           for (int i = 1; i < length; i++){
               vector.elements[i + 1] = vector.elements[i];
           }
           vector.elements[0] = tmp;
       }
return vector;
   }

Explanation / Answer

long tmp = vector.elements[0];

last = vector.elements[length-1];

           for (int i = vector.elements[length-2];i>=0; i--){
               vector.elements[i + 1] = vector.elements[i];
           }

vector.elements[0]=last;

           
       }
return vector;
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote