DEV Community

Cover image for Write a loop that sets newscores to oldscores shifted once left, with element 0 copied to the end.
smithmchael550
smithmchael550

Posted on

Write a loop that sets newscores to oldscores shifted once left, with element 0 copied to the end.

I got a problem with my textbook under the for-loop chapter. It’s saying something like this:

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}.

I am learning c++ and writing my code in codeblocks. What could be the possible solution to this problem? As far as I can understand the above problem it says to set some values to oldscore from newscore. Once shifted, then we need to copy the very first vale with 0 and shift it to the end.

Resolution:

I’ve found a possible solution to the problem above. I am attaching my sample code snippet below:

int main()
{
   const int Value_Size = 4;

   int oldScores[Value_Size] = { 10, 20, 30, 40 };
   int newScores[Value_Size];

   for (int n = 0; n < Value_Size; ++n)
   {
      std::cout << oldScores[n] << ' ';
   }
   std::cout << '\n';

   newScores[Value_Size - 1] = oldScores[0];

   for (int n = 0; n < Value_Size - 1; n++)
   {
      newScores[n] = oldScores[n + 1];
   }

   for (int n = 0; n < Value_Size; ++n)
   {
      std::cout << newScores[i] << ' ';
   }
   std::cout << '\n';
}
Enter fullscreen mode Exit fullscreen mode

This is it.
But you can read more at Kodlogs.com

Top comments (1)

Collapse
 
coderlegi0n profile image
CoderLegion

As far as I can tell, the problem is that some numbers should be set to old score from newscore.
After it has relocated, o must be substituted for the initial value by moving it to the end.
In my programme, I first printed the oldScores, then sorted the values in the next for-loop, and then printed the newScores in the next for-loop. See the code here kodlogs.net/18/write-loop-that-set...