C/C++ Solving the Loop Puzzle: Shifting Old Scores to New Scores

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Loop Puzzle
AI Thread Summary
The discussion focuses on creating a loop to shift elements in an array, specifically moving elements in `oldScores` one position to the left and placing the first element at the end of `newScores`. Several coding solutions are proposed, with a preference for a method that populates all but the last element in a loop and handles the last element separately for efficiency. Participants emphasize the importance of correctly referencing array indices to avoid overwriting values. The conversation also touches on the challenge of adapting the solution to different programming languages, such as Java and UBasic. Ultimately, the goal is to implement a clear and effective solution for the shifting problem.
ineedhelpnow
Messages
649
Reaction score
0
Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end. Ex: If oldScores = {10, 20, 30, 40}, then newScores = {20, 30, 40, 10}.

Sample program:

Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int SCORES_SIZE = 4;
   vector<int> oldScores(SCORES_SIZE);
   vector<int> newScores(SCORES_SIZE);
   int i = 0;

   oldScores.at(0) = 10;
   oldScores.at(1) = 20;
   oldScores.at(2) = 30;
   oldScores.at(3) = 40;

   <STUDENT CODE>

   for (i = 0; i < SCORES_SIZE; ++i) {
      cout << newScores.at(i) << " ";
   }
   cout << endl;

   return 0;
}

(Worried) I've been staring at this problem for a while. how do i do it? i need to use a for loop i believe (i=0;i<SCORES_SIZE;++i). i don't know what to put in the loop.
 
Technology news on Phys.org
Perhaps you could try something like:

Code:
   for (i = 0; i < SCORES_SIZE; i++)
   {
      if (i == (SCORES_SIZE - 1))
      {
         newScores.at(i) = oldscores.at(0);
      }
      else
      {
         newScores.at(i) = oldscores.at(i + 1);
      }
   }
 
Another alternative (include the math library):

Code:
   for (i = 0; i < SCORES_SIZE; i++)
   {
      newScores.at(i) = oldscores.at(SCORES_SIZE*((i - (SCORES_SIZE - 2)/2)/SCORES_SIZE - floor(1/2 + (i - (SCORES_SIZE - 2)/2)/SCORES_SIZE)) + SCORES_SIZE/2);
   }
 
Perhaps simpler still would be:

Code:
   for (i = 0; i < SCORES_SIZE - 1; i++)
   {
         newScores.at(i) = oldscores.at(i + 1);
   }
   newScores.at(SCORES_SIZE - 1) = oldscores.at(0);
 
(Giggle) thanks. I am going to make a folder on my desktop to hold all the possible solutions for this question. (Rofl) in my activity the way it is i can only edit the student code part so since the math library wasnt included at the beginning there is no way for me to add it. ill try the first one because it looks similar to something i tried.
 
ineedhelpnow said:
(Giggle) thanks. I am going to make a folder on my desktop to hold all the possible solutions for this question. (Rofl) in my activity the way it is i can only edit the student code part so since the math library wasnt included at the beginning there is no way for me to add it. ill try the first one because it looks similar to something i tried.

The last suggestion is similar to the first, but rather than using an if/else construct within the loop, all but the last element of the new array is populated in the for loop, and then the last element is handled separately after the loop. That would be my choice for efficiency. :D
 
So in the end the student part of the code should look like this for all our java users out there:

Code:
   for (i = 0; i < SCORES_SIZE - 1; i++)
   {
         newScores[i] = oldScores[i + 1];
   }
   newScores[SCORES_SIZE - 1] = oldScores[0];
 
CABradfish said:
So in the end the student part of the code should look like this for all our java users out there:

Code:
   for (i = 0; i < SCORES_SIZE - 1; i++)
   {
         newScores[i] = oldScores[i + 1];
   }
   newScores[SCORES_SIZE - 1] = oldScores[0];
This is incorrect because the 0th element is overwritten in the loop, and therefore it won't end up at the end of the array.

Edit: Sorry, I did not notice that the new array is different, so the code is correct after all. Well, this raises a natural question how to update the original array in a similar way.
 
Just in case this helps...

Using my favorite UBasic:

Array A contains: A(1)=10, A(2)=20, A(3)=30, A(4)=40

Program:
k = A(1)
FOR i = 1 to 3
A(i) = A(i+1)
NEXT i
A(4) = k

I'm the kind of dummy that can't see the sense
in anyone using anything other than UBasic :)
 
Back
Top