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

74 views

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

``````#include <iostream>

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';
}``````

This program is producing exactly the same output as given in the problem:

``````10 20 30 40
20 30 40 10``````

In my program, I printed my oldScores first, and then sorted the values in the next for-loop, after then printed the newScores inside the next for-loop.
I hope someday someone will get help from this.

Thanks

posted Mar 19 in c++