Solving the Loop Puzzle: Shifting Old Scores to New Scores

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    Loop Puzzle
In summary, the conversation is discussing how to write a loop that shifts the elements of an array one space to the left, with the first element being copied to the end. Multiple solutions are suggested, including using an if/else statement, using the math library, and simply using a for loop. The final suggested solution is to update the original array in a similar way by storing the first element in a temporary variable and then changing the elements in the loop. The conversation ends with a comment about using UBasic for this task.
  • #1
ineedhelpnow
651
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
  • #2
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);
      }
   }
 
  • #3
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);
   }
 
  • #4
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);
 
  • #5
(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.
 
  • #6
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
 
  • #7
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];
 
  • #8
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.
 
  • #9
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 :)
 

Related to Solving the Loop Puzzle: Shifting Old Scores to New Scores

1. How does the loop puzzle work?

The loop puzzle involves shifting old scores to new scores in a continuous loop. This means that the last score in the loop will become the first score in the next loop, and so on. The goal is to find a way to shift the scores in such a way that all scores end up in a desired order.

2. What is the purpose of solving the loop puzzle?

The purpose of solving the loop puzzle is to improve problem-solving skills and logical thinking. It also helps to improve memory and concentration as the player must remember and manipulate multiple scores at once.

3. How do I approach solving the loop puzzle?

First, start by identifying the desired order of the scores. Then, try to find a pattern in the movement of the scores. It may be helpful to write out the scores in a sequence to visualize the loop. From there, you can start shifting the scores in different ways until you find a solution.

4. Are there any tips or tricks for solving the loop puzzle?

One tip is to work backwards from the desired order. This means starting with the last score and working your way towards the first score. Another tip is to focus on one score at a time and try to manipulate it to its correct position in the loop. Lastly, don't get discouraged if you don't find the solution right away, take breaks and come back to it with a fresh perspective.

5. Are there variations of the loop puzzle?

Yes, there are many variations of the loop puzzle. Some may have a different number of scores, while others may involve different movements or rules for shifting the scores. These variations can provide new challenges and keep the puzzle interesting for players.

Similar threads

  • Programming and Computer Science
Replies
1
Views
4K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
1
Views
908
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
3
Views
766
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
4
Views
831
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top