void sliceReverse(int array[], int start, int end) { if ( start < end ) { int first = array[start]; for ( int i = start; i < end; i++ ) { first = array[i]; array[i] = array[end]; } array[end] = first; } } 

I can not make out what I'm doing wrong. Give a hint.

    6 answers 6

    Try speaking out loud what your code is doing now. It seems to me that you will immediately understand what exactly you are doing wrong.

    The reverse part of the array, as you know, is almost as simple as the reverse of the entire array, which you have already asked here.

    Generally speaking, sliceReverse(array, start, end) = arrayReverse(array + start, end - start + 1) (or end - start , depending on how the convention of the definition of end chosen) . From here the final code for solving the problem is very simple.

    • So this is the question he asked? Did not go to proc. I did not learn anything ... - skegg
    • Maybe he will learn ... - skegg

    Slightly podshamanil and it turned out like this:

     void sliceReverse(int array[], int start, int end) { int temp; for ( ; start < end; start++, end-- ) { temp = array[start]; array[start] = array[end]; array[end] = temp; } } 

    Thanks also for the previous comments.

      All wrong. Better give ready-made code, and you understand it

       void sliceReverse(int array[], int start, int end) { if ( start >= end ) return; int temp; for ( ; start < end; start++, end-- ) { temp = array[start]; array[start] = array[end]; array[end] = temp; } } 
      • By the way, who minus, explain why. - skegg
      • And why after if return? - arcs_host
      • If it starts after the end, we finish the function, since further conversations with it are useless. - skegg
      • Yes, you can without checks. The cycle ends before it starts. - gammaker 2:41

      The logic of building a cycle is not clear. Every time you rewrite first, but do not use it. In this cycle, you will write the last element everywhere. I would do this:

        int i, j, buf; for (i = start, j = end; i < j; ++i, --j ) { buf = array[i]; array[i] = array[j]; array[j] = buf; } 

        Hint:

        start and end should move towards each other until they are equal.

        Make an exchange between the elements of the array addressed by them.

          Try this (maybe where small blots - did not have time to check):

            void sliceReverse (int array [], int start, int end) {
                   while (start <end) {
                       int tmp = array [start];
                       array [start] = array [end];
                       array [end] = tmp;
                       ++ start;
                       --end;
                   }
               } 

          • The variable temp is better to declare before the loop. - skegg
          • Can you explain why? - DUP
          • At the beginning of each new iteration, memory will be allocated for this variable in the stack, and at the end - “deleted”. Then stand out again, etc. There will be meaningless extra work that slows down the program. It is better to allocate a memory once and to apply to it each time. - skegg
          • @mikillskegg Disagree, by the way. The code will still be optimized, and if we declare it in a cycle, we follow the rule “declare variables as close as possible to the area of ​​use”. - Costantino Rupert
          • one
            About the place to ad temp. I do not know how all the compilers, and gcc for all such variables allocates a place in the stack once at the entrance to the function. The memory of some of them (if they are in different blocks) will overlap. - avp