There is a .bin file containing an array of numbers:
new int[] { 12, 6, 7, -1, 5, 15, 6, 4, -1, 15, 5 } There is a method that, in a cycle, searches for a local minimum and changes its sign to the opposite:
private static void doWork() { try (RandomAccessFile raf = new RandomAccessFile(fileName, "rw")) { int n = (int)raf.length() / Integer.BYTES; // Кол-во int prevValue = raf.readInt(); // пред. элемент. for (int i = 1; i < n - 1; i++) { // текущий элемент. int value = raf.readInt(); // позиция текущего элемента long pos = raf.getFilePointer(); // следующий элемент. int nextValue = raf.readInt(); if (value < prevValue && value < nextValue) { raf.seek(pos); raf.writeInt(-value); } } // for i } catch (Exception ex) { ex.printStackTrace(); } } // doWork But it does not work properly: it only changes the sign once for the opposite, and then with the prev. number to local minimum.
What have I done wrong?
6,-1,-1. The local minimum is where the derivative changes the sign from minus to plus. - vp_arth6,-1,-1,2what to do? - rjhdby