In: Computer Science
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}.
--------------Code Below-----------
#include <iostream>
using namespace std;
int main() {
const int SCORES_SIZE = 4;
int oldScores[SCORES_SIZE];
int newScores[SCORES_SIZE];
int i;
for (i = 0; i < SCORES_SIZE; ++i) {
cin >> oldScores[i];
}
/* Your solution goes here */
for (i = 0; i < SCORES_SIZE; ++i) {
cout << newScores[i] << " ";
}
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
const int SCORES_SIZE = 4;
int oldScores[SCORES_SIZE];
int newScores[SCORES_SIZE];
int i,j;
for (i = 0; i < SCORES_SIZE; ++i) {
cin >> oldScores[i];
}
// oldScores with element 0 copied to the end of newScores.
newScores[SCORES_SIZE-1]=oldScores[0];
//a loop that sets newScores to oldScores shifted once left
for(j=1;j<SCORES_SIZE;j++){
newScores[j-1]=oldScores[j];
}
//result
for (i = 0; i < SCORES_SIZE; ++i) {
cout << newScores[i] << " ";
}
cout << endl;
return 0;
}
//screenshot of the code
output:
Explanation: