0
How to print out the last 5 values added to an array?
Let say I make an array of 10 elements and want to print the values of the 5 most recently added elements. Sure, I could print out the half but if it were an array of 20 elements, it wouldnât work. What should I do? Sorry if Iâm not asking this right. I just donât know how to properly ask for it.
1 Answer
+ 5
use simple for, but iterate in reversed order OR start from the array size -5 like:
for (int i = arraySize-5; i < arraySize; ++i)
{
//do something
}