【问题标题】:No Output - The program doesn't give any output无输出 - 程序不提供任何输出
【发布时间】:2021-09-18 04:04:34
【问题描述】:

我正在学习数据结构。我尝试编写一个函数 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;
}

【问题讨论】:

  • rotate 的返回类型应该是void,因为它不返回值。

标签: arrays c function rotation


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    旋转量指定为整数,正值向右旋转,负值向左旋转。它将这个数量转换为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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-22
      • 2015-10-22
      • 2022-01-10
      • 2023-01-25
      • 2021-04-13
      • 1970-01-01
      • 2019-03-24
      相关资源
      最近更新 更多