繁体   English   中英

无输出 - 程序不提供任何输出

[英]No Output - The program doesn't give any output

我正在学习数据结构。 我尝试编写一个函数 rotate(arr[], d, n) 将大小为 n 的 arr[] 旋转 d 个元素。

旋转是指移动数组中的元素。

该程序没有给出任何错误,而是挂起一点但不运行。

这是代码: -

#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]);
    }
}

该程序仅在我不接受用户输入时运行。

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;
}

你可能想在scanf("%d", &n);之后做int arr[n] scanf("%d", &n); 因为当您执行int arr[n]n未初始化。 此外,C 中的数组索引从 0 开始,因此for (int i = 1; i <= n; i++)将是for (int i = 0; i < n; i++)

这不是一个正确的答案,所以不要把它当作正确的答案。 这只是出于教育目的的一种可能实现。

这是一种旋转数组的方法,以便每个元素只移动一次(除了“组”的第一个元素通过临时变量移动)。

旋转量指定为整数,正值向右旋转,负值向左旋转。 它将这个数量转换为0n-1范围内的一个数字,这是将被复制到元素 0 的元素的索引。然后它将数组分成一个或多个相同大小的交错组,这样连续的元素在每个组以循环方式按旋转量分隔,并旋转每个组内的元素。 (组数是n和旋转量的最大公约数,每组的元素数是元素总数除以组数。)

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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