S
S
sidor_tank2017-10-19 22:50:14
C++ / C#
sidor_tank, 2017-10-19 22:50:14

How to rearrange the elements of an array located between its minimum and maximum elements?

Condition: rearrange in reverse order the elements of the array of random numbers located between its minimum and maximum elements. You can only use arrays, conditions, and loops. The bottom line is this: if we have an array of random numbers, let's say: 5, 45, 34, 98, 17, then it will take the form:
5, 34, 45, 98, 17.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
devalone, 2017-10-19
@devalone

1 Find the index of the minimum element minIndex
2 Find the index of the maximum element maxIndex
3 Check that there are elements between them
4 Reverse the range from min(minIndex, maxIndex) + 1to max(minIndex, maxIndex) - 1

M
Mercury13, 2017-10-19
@Mercury13

Find the index of the minimum and maximum.
If necessary, swap so that one is smaller than the other. INDICES, not elements.
And now the cycle.

++низИндекс
--верхИндекс
пока низИндекс < верхИндекс
  поменять a[низИндекс], а[верхИндекс]
  ++низИндекс
  --верхИндекс

If the indexes are unsigned, you need to check somehow that the first --topIndex will not lead to "anti-overflow". For example, "if topIndex > 0"...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question