Optional: rearrange an active sequence
Use half-open algorithm ranges and preserve values while shifting, rotating, inserting, or deleting.
Sort and reverse mean different things
This is a second-reading extension. <algorithm> provides std::sort(a, a + n), which rearranges the active values into nondecreasing order. Duplicates remain, and original positions may change. The range includes its start and excludes its end.
Using a + n − 1 as the end leaves out the last active value; using a + CAPACITY includes inactive cells. For strings, pass s.begin() and s.end(). std::reverse reverses current order; it produces descending order only if the sequence was sorted first.
Both algorithms modify the supplied sequence rather than returning a new ordinary string. Sorting is more work than a simple traversal; do not sort merely to check whether the original sequence was already ordered.
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string s;
std::getline(std::cin, s);
std::sort(s.begin(), s.end());
std::cout << s << '\n';
std::reverse(s.begin(), s.end());
std::cout << s << '\n';
return 0;
}A rotation saves the value that wraps
A one-step right rotation turns [10, 20, 30, 40] into [40, 10, 20, 30]. Save the last element, copy the remaining suffix right starting from the right end, then place the saved element at index zero.
Copying rightward from left to right overwrites future source values. Guard n > 0 before reading a[n − 1]. A rotation preserves every value and the active length, unlike a shift that discards one end and fills the other.
Insertion requires spare capacity
To insert at a position from 0 through n, require n < CAPACITY. Move a[i − 1] into a[i] from i = n downward while i > position, write the new value into the gap, then increment n.
The first write can use old index n only because a spare allocated cell exists. Insertion at position n appends without moving a suffix. Physical capacity stays fixed; active length grows.
Deletion closes the gap
For a valid position in a nonempty sequence, copy each following value left: a[i] = a[i + 1] from the deletion position while i + 1 < n. Then decrement n. The old final cell is now inactive and need not be erased.
Deletion moves left to right; insertion moves right to left. These directions preserve each source until it is read. Inserting or deleting near the front can move almost every active element, so the work grows with the sequence length.
An array has capacity 5 and active length 5. Can the bounded-array insertion procedure add another element?
Turn the idea into code
Optional: read 0 ≤ n ≤ 1000 integers and rotate the active array right by one. Leave an empty list empty.
Give me a hint
Save the last value before shifting. Copy from right to left.
Show the explained solution
The saved value wraps into index zero, while every earlier element moves exactly one place right. The guard makes the empty case safe and a one-element list stays unchanged.
#include <iostream>
int main() {
int a[1000]{};
int n = 0;
std::cin >> n;
for (int i = 0; i < n; ++i) { std::cin >> a[i]; }
if (n > 0) {
int last = a[n - 1];
for (int i = n - 1; i > 0; --i) { a[i] = a[i - 1]; }
a[0] = last;
}
for (int i = 0; i < n; ++i) {
if (i > 0) { std::cout << ' '; }
std::cout << a[i];
}
std::cout << '\n';
return 0;
}Your notes
Mark complete when you can explain it yourself.