Copying and modifying an array

I am copying and modifying an array elements {10, 20, 30, 40}
my output:20 30 40 20
expected output: 20 30 40 10
How do I grab oldScores[0] and display last in newScores[3]?

[#include <iostream>
using namespace std;

int main() {
const int SCORES_SIZE = 4;
int oldScores[SCORES_SIZE];
int newScores[SCORES_SIZE];
int i = 0;

oldScores[0] = 10;
oldScores[1] = 20;
oldScores[2] = 30;
oldScores[3] = 40;

/* Your solution goes here */
for (i = 0; i < SCORES_SIZE; ++i) {
newScores[i] = oldScores[i + 1];

}

for (i = 0; i < SCORES_SIZE; ++i) {
cout << newScores[i] << " ";
}
cout << endl;

return 0;
}
Last edited on
You are taking values from beyond the end of an array. Your first loop should have
< SCORES_SIZE - 1.

Your final item doesn't fit into the regular formula. You would need
newScores[SCORES_SIZE-1] = oldScores[0];
outside the loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main() {
	const int SCORES_SIZE {4};
	const int oldScores[SCORES_SIZE] {10, 20, 30, 40};
	int newScores[SCORES_SIZE] {};

	for (size_t i = 0; i < SCORES_SIZE - 1; ++i)
		newScores[i] = oldScores[i + 1];

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

	for (size_t i = 0; i < SCORES_SIZE; ++i)
		cout << newScores[i] << ' ';

	cout << '\n';
}

Thanks!! Finally working.
Topic archived. No new replies allowed.