Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7510127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:08:18+00:00 2026-05-29T23:08:18+00:00

I am trying to implement left array rotation using the method described here: http://www.cs.bell-labs.com/cm/cs/pearls/s02b.pdf

  • 0

I am trying to implement left array rotation using the method described here: http://www.cs.bell-labs.com/cm/cs/pearls/s02b.pdf (under section Reversal algorithm)

I am having trouble reversing the array when the start index isn’t 0.

Here is what I have so far:

void reverse_arr(int *a, int start, int end)
{
    int i;
    int len = end - start;
    //printf("Len pre loop: %d\n", len);
    int swap;

    for(i = start; i < --len; i++)
    {
        //printf("start: %d\tlen: %d\n", start, len);
        swap   = a[i];
        a[i]   = a[len];
        a[len] = swap;
    }
}

This works great when the start index is 0, but when it is anything else it never reverses all of the elements.

ex:

int test[] = {1,2,3,4,5,6};
reverse_arr(test, 0, 2); //reverse the first 2 elements of the array

results in: {2,1,3,4,5,6} which is expected

int test[] = {1,2,3,4,5,6}; 
reverse_arr(test, 2, 6); //reverse the last 4 elements of the array

results in: {2,1,4,3,5,6} which is not expected, only the 3 and 4 were reversed.

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:08:19+00:00Added an answer on May 29, 2026 at 11:08 pm

    You need to adjust the other subscript in the swap:

    void reverse_arr(int *a, int start, int end)
    {
        int len = end - start;
    
        for (int i = start; i < --len; i++)
        {
            int swap = a[i];
            a[i]     = a[i+len];
            a[i+len] = swap;
        }
    }
    

    You could probably also use:

    void reverse_arr(int *a, int start, int end)
    {
        int len = end - start;
    
        end--;
    
        for (int i = start; i < --len; i++, end--)
        {
            int swap = a[i];
            a[i]     = a[end];
            a[end]   = swap;
        }
    }
    

    Working test code

    #if defined(VERSION1)
    static void reverse_arr(int *a, int start, int end)
    {
        int i;
        int len = end - start;
        int swap;
    
        for (i = start; i < --len; i++)
        {
            swap     = a[i];
            a[i]     = a[i+len];
            a[i+len] = swap;
        }
    }
    
    #else
    
    static void reverse_arr(int *a, int start, int end)
    {
        int i;
        int len = end - start;
    
        end--;
    
        for (i = start; i < --len; i++, end--)
        {
            int swap   = a[i];
            a[i]   = a[end];
            a[end] = swap;
        }
    }
    #endif
    
    #define DIM(x)  (sizeof(x)/sizeof(x[0]))
    
    #include <stdio.h>
    
    static void print_array(int *array, size_t size)
    {
        for (size_t i = 0; i < size; i++)
            printf(" %d", array[i]);
        putchar('\n');
    }
    
    
    static void tester(int lo, int hi)
    {
        int test[] = {1,2,3,4,5,6};
        printf("Before: (%d, %d)\n", lo, hi);
        print_array(test, DIM(test));
        reverse_arr(test, lo, hi); //reverse the first 2 elements of the array
        printf("After:  (%d, %d)\n", lo, hi);
        print_array(test, DIM(test));
        putchar('\n');
    }
    
    int main(void)
    {
        tester(0, 2);
        tester(2, 6);
        return(0);
    }
    

    And the results from compiling with -DVERSION1 or not are the same:

    Before: (0, 2)
     1 2 3 4 5 6
    After:  (0, 2)
     2 1 3 4 5 6
    
    Before: (2, 6)
     1 2 3 4 5 6
    After:  (2, 6)
     1 2 6 4 5 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to implement a 3 column fluid layout by following http://www.alistapart.com/articles/holygrail/ (left
So, im trying to implement the bottomupheap algorithm here: http://www.apl.jhu.edu/Classes/Notes/Felikson/courses/605202/lectures/L8/L8.html Algorithm bottomUpHeap(S) Input: a
I am trying to implement share this method. I am using the code as
I'm trying to implement language switching in .htaccess, and the only thing left now
I'm trying to implement UINavigationBar with custom controls. So, I've added UIView on left
trying to implement a dialog-box style behaviour using a separate div section with all
I am trying to implement a type of slider using jquery. However this question
I'm trying to implement drag and drop in Silverlight using F# and asynchronous workflows.
I'm trying to implement Menu in a Window using MVVM pattern. So I have
I am trying to implement swipe in android from left to right and right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.