Change a sequence without losing it
Update, copy, compare, reverse, and inspect neighbours using the correct bounds.
Change one cell or every matching cell
After validating its index, an array element behaves like a variable: a[index] = value changes one cell. A conditional scan can replace every negative value with zero or replace all occurrences of one target.
Replacement leaves the active length unchanged. Removing an element is a different operation because later elements must move and n must decrease. Avoid treating replacement with zero as deletion.
Copy and compare actual elements
Raw arrays do not support whole-array assignment with b = a. Copy corresponding cells with a loop. Later changes to either array do not affect the other copy.
Sequence equality requires equal active lengths and equal values at every corresponding index. Comparing raw array names is not a content comparison. Once a mismatch makes a same flag false, do not set it true again when a later pair matches.
Look at a neighbour only when it exists
To compare each element with its predecessor, start at i = 1. Accessing a[i − 1] at i = 0 would use index −1. To compare a[i] with a[i + 1], require i + 1 < n.
A nondecreasing sequence allows equal neighbours; a strictly increasing sequence does not. Checking existing order does not sort the array. Empty and one-element sequences contain no violating adjacent pair.
Reverse each mirrored pair once
The mirror of index left is n − 1 − left. Save one value before overwriting its cell, then exchange the pair. Assigning left from right followed by right from the new left would lose the original left value.
Stop after n / 2 swaps. Swapping all n pairs would undo the reversal. For odd n, the middle cell stays in place. This scan uses linear work and only one temporary value beyond the existing array.
#include <iostream>
int main() {
int a[1000]{};
int n = 0;
std::cin >> n; // 0 <= n <= 1000.
for (int i = 0; i < n; ++i) { std::cin >> a[i]; }
for (int left = 0; left < n / 2; ++left) {
int right = n - 1 - left;
int temporary = a[left];
a[left] = a[right];
a[right] = temporary;
}
for (int i = 0; i < n; ++i) {
if (i > 0) { std::cout << ' '; }
std::cout << a[i];
}
std::cout << '\n';
return 0;
}Why must an in-place reversal stop after n / 2 swaps?
Turn the idea into code
Count strict rises: elements larger than their immediate predecessor. For [2, 2, 5, 1, 4], print 2. Allow 0 ≤ n ≤ 1000.
Give me a hint
The first element has no predecessor. Start the scan at index 1.
Show the explained solution
Only 5 > 2 and 4 > 1 are rises. Equal neighbours do not count. Zero- and one-element lists naturally return zero.
#include <iostream>
int main() {
int a[1000]{};
int n = 0;
std::cin >> n;
for (int i = 0; i < n; ++i) { std::cin >> a[i]; }
int rises = 0;
for (int i = 1; i < n; ++i) {
if (a[i] > a[i - 1]) { ++rises; }
}
std::cout << rises << '\n';
return 0;
}Your notes
Mark complete when you can explain it yourself.