Solving the Loop Puzzle: Shifting Old Scores to New Scores

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Loop Puzzle
Click For Summary
SUMMARY

The discussion focuses on implementing a loop in C++ to shift elements of an array, specifically moving elements in the `oldScores` vector to the `newScores` vector. The correct implementation involves using a for loop to copy elements from `oldScores` to `newScores`, with the first element of `oldScores` being placed at the end of `newScores`. The final code snippet provided is: for (i = 0; i < SCORES_SIZE - 1; i++) { newScores[i] = oldScores[i + 1]; } newScores[SCORES_SIZE - 1] = oldScores[0];. This method is efficient and avoids overwriting elements during the loop.

PREREQUISITES
  • Understanding of C++ syntax and vector usage
  • Familiarity with for loops and array indexing
  • Basic knowledge of variable initialization and assignment
  • Concept of element shifting in arrays
NEXT STEPS
  • Explore C++ vector manipulation techniques
  • Learn about array rotation algorithms
  • Investigate performance implications of different looping constructs in C++
  • Study how to implement similar logic in other programming languages, such as Java or Python
USEFUL FOR

Students learning C++, software developers looking to optimize array manipulations, and educators teaching programming fundamentals.

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 :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
12
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K