简体   繁体   中英

No Output - The program doesn't give any output

I am learning data structures. I tried to write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements.

By rotate I mean shifting the elements in an array.

The program doesn't give any error, rather it hangs a bit but it doesn't run.

Here's the code: -

#include <stdio.h>

int rotate(int arr[], int d, int n, int dir)
{
    int temp, i;

    while (d)
    {
        if (dir)
        {

            // for left shift
            // First element will always get replaced in a rotation.
            temp = arr[0];
            for (i = 0; i < n - 1; i++)

                // for left shifting the second element next to its original position.
                arr[i] = arr[i + 1];

            // Putting the temp value in the last position.
            arr[n - 1] = temp;
        }

        else
        {
            // for right shift
            // Last element will always get replaced in a rotation.
            temp = arr[n - 1];
            for (i = n - 1; i > 0; i--)

                // for right shifting the second last element to the last position.
                arr[i] = arr[i - 1];
            // Putting the temp value in the first position
            arr[0] = temp;
        }
        d--;
    }

    // Print the shifted array
    for (i = 0; i < n; i++)
    {
        printf("%d, ", arr[i]);
    }
}

The program only runs when I don't take inputs from the user.

int main()
{
    int n;
    int arr[n];
    int dir;
    int d;

    printf("Enter the size of the array: \n");
    scanf("%d", &n);

    printf("Enter the elements of the array: \n");
    for (int i = 1; i <= n; i++)
    {
        printf("Enter element %d", i);
        scanf("%d", &arr[i]);
    }

    printf("Enter the position: \n");
    scanf("%d", &d);

    printf("Enter the direction: \n");
    // 0: Right Direction and 1: Left Direction
    scanf("%d", &dir);


    // Before shifting the array
    for (int i = 1; i <= n; i++)
    {
        printf("%d, ", arr[i]);
    }

    // After shifting the array
    rotate(arr, d, n, dir);


    return 0;
}

You might want to do int arr[n] after scanf("%d", &n); because n is not initialized when you do int arr[n] . Also array indexing in C starts from 0 so for (int i = 1; i <= n; i++) will be for (int i = 0; i < n; i++) .

This is not a proper answer, so do not accept it as the correct answer. It is just a possible implementation for educational purposes.

Here is a way to rotate the array so that each element is moved only once (except that the first element of a "group" is moved via a temporary variable).

The rotation amount is specified as an integer with positive values rotating right and negative values rotating left. It converts this amount into a number in the range 0 to n-1 which is the index of the element that will be copied to element 0. It then divides the array into one or more interleaved groups of the same size such that successive elements in each group are separated by the rotation amount in a circular fashion, and rotates the elements within each group. (The number of groups is the greatest common divisor of n and the rotation amount, and the number of elements in each group is the total number of elements divided by the number of groups.)

#include <limits.h>
#include <stddef.h>

static size_t rotate_modulus(int d, size_t n);
static size_t gcd_size(size_t a, size_t b);

/* Rotate arr[] of length n right by d, or left by -d. */
void rotate(int arr[], int d, size_t n)
{
    size_t md = rotate_modulus(d, n);   /* Get offset in range 0 to n-1. */
    if (md)
    {
        /* Rotation needed. */
        /* Divide into interleaved groups and rotate each group. */
        size_t num_groups = gcd_size(n, md);
        size_t group_size = n / num_groups;
        size_t group;
        for (group = 0; group < num_groups; group++)
        {
            size_t a = group;   /* Index of first element in group. */
            size_t i;
            /* Rotate elements in group. */
            int temp = arr[a];  /* Get first element. */
            for (i = 0; i < group_size - 1; i++)
            {
                /* Get index of next element in group. */
                size_t b = (a + md);
                if (a >= n - md)
                {
                    b -= n;         /* Index wraps around. */
                }
                arr[a] = arr[b];    /* Move an element. */
                a = b;              /* Advance to next index. */
            }
            arr[a] = temp;          /* Move first element to last element. */
        }
    }
}

/*
 * Get modulus for rotation of n elements.
 *
 * d is the amount to rotate right; negative d rotates left by -d.
 *
 * For zero n, the return value is 0.
 *
 * For non-zero n, the return value is n - s, where s is d plus an
 * integer multiple of n such that s is in the range 1 to n, and the
 * return value is in the range 0 to n - 1.
 */
static size_t rotate_modulus(int d, size_t n)
{
    size_t md;
    if (n < 2)
    {
        /* No rotation needed if n < 2. */
        md = 0;
    }
    else if (d >= 0)
    {
        /* Non-negative d will rotate right. */
        md = d % n;
        if (md)
        {
            md = n - md;
        }
    }
    else
    {
        /* Negative d will rotate left. */
        /* -d would overflow if d == INT_MIN && INT_MIN == -INT_MAX - 1. */
        int fix_overflow = (d < -INT_MAX);
        md = -(d + fix_overflow) % n;
        if (fix_overflow)
        {
            if (++md == n)
            {
                md = 0;
            }
        }
    }
    return md;
}

/*
 * If both a and b are non-zero, return the greatest common divisor of a and b.
 * Otherwise, return 0.
 */
static size_t gcd_size(size_t a, size_t b)
{
    if (b == 0)
    {
        a = 0;
    }
    else
    {
        do
        {
            size_t t = b;
            b = a % b;
            a = t;
        }
        while (b);
    }
    return a;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM