【问题标题】:Rotating array anti-clockwise, unexpected debug output in one step逆时针旋转数组,一步出意外调试输出
【发布时间】:2015-06-27 17:30:16
【问题描述】:

我正在使用最简单的方法逆时针旋转数组,即将元素从 index=0 存储到 index=所需的旋转位置数,在一个临时数组中,最后将这些元素插入另一个数组的末尾。代码如下:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int *a, *temp, i, j=0, n,np,*b;
    printf("Enter no. of elements in arr:\n");
    scanf("%d",&n);
    a=(int*) malloc(n*sizeof(int));//Primary array
    b=(int*) malloc(n*sizeof(int));//Final array that would be printed in the end
    printf("Enter elements:\n");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    printf("Enter positions to be rotated anti-clockwise:\n");
    scanf("%d",&np);
    temp=(int*) malloc(np*sizeof(int));//storing elements left of index=0
    for(i=0;i<np;i++,j++)
    {
        //printf("hi\n");
        temp[j]=a[i];
        printf("temp[%d]=%d\n",j,temp[j]);
    }
    j=0;
    printf("After rotating:\n");
    for(i=n-1;i>=0;i--)
    {
        printf("i=%d ",i);
        b[i-np]=a[i];
        printf("b[%d]=%d\n",i-np,b[i]); /*Here is 1 unexpected thing happening, the program is not picking up correct value of array a at index i.*/
    }
    for(i=np-1;i<n;i++,j++)
        b[i]=temp[j];//storing temp elements in final array

    printf("Finally matrix is\n");

    for(i=0;i<n;i++)
        printf("%d\n",b[i]);

    getch();
    return 0;
}

【问题讨论】:

  • 抱歉,逆时针这个词是什么意思?您是否试图将您的数组元素以圆形方式放置?请解释一下。

标签: c arrays debugging data-structures


【解决方案1】:

无论您尝试将数组元素向一个方向或另一个方向旋转,过程总是相同的:获取您将要移动到另一个极端的极值元素的副本。然后,将下一个元素(从您保存到另一侧的元素)复制到您打开的孔中。最后,用保存的元素盖住洞。

typedef int ARRAY_ELEMENT;
ARRAY_ELEMENT the_array[N_ELEMENTS];

/* to put the first element at the end */
ARRAY_ELEMENT saved_element = the_array[0];
for (int i = 1; i < N_ELEMENTS; i++) the_array[i-1] = the_array[i];
the_array[N_ELEMENTS-1] = saved_element;

/* to put the last element at the beginning */
ARRAY_ELEMENT saved_element = the_array[N_ELEMENTS-1];
for (int i = N_ELEMENTS-1; i > 0; i--) the_array[i] = the_array[i-1];
the_array[0] = saved_element;

您也可以通过将第一个元素与第二个元素、第二个元素与第三个元素等交换来做到这一点……直到下一个元素和最后一个元素。您将获得相同的结果,但任务要多得多(每轮三个任务,而不是一个)

如果您只想旋转数组的一部分,请设计一个例程,将指向第一个元素的指针和这些元素的数量作为参数......然后将其应用于要旋转的数组部分。

void rotate_upwards(ARRAY_ELEMENT *a, size_t n) /* a[0] -> a[1], a[1] -> a[2] ... */
{
    ARRAY_ELEMENT saved = a[n-1];
    int i;
    for (i = n-1; i > 0; i--) a[i] = a[i-1];
    a[i] = saved;
} /* rotate_upwards */

void rotate_downwards(ARRAY_ELEMENT *a, size_t n) /* a[0] <- a[1], ... */
{
    ARRAY_ELEMENT saved = a[0];
    int i;
    for (i = 0; i < n-1; i++) a[i] = a[i+1];
    a[i] = saved;
} /* rotate_downwards */

如果你想在33位置旋转array[100]5元素,你可以使用

rotate_downwards(array + 33, 5);

您将只旋转数组的一部分。

【讨论】:

    【解决方案2】:

    如果我在有罪的打印中理解正确,您正在打印旋转数组。 问题在于您正在打印的元素:

    printf("b[%d]=%d\n",i-np,b[i]);
    

    您实际上正在打印b[i],我认为您要打印a[i]b[i-np]

    【讨论】:

    • 是的,我更正了它,但仍然在最终数组中得到垃圾值。
    【解决方案3】:

    你说“意外的事情”的这个循环是错误的

    for(i=n-1;i>=0;i--)
    {
        printf("i=%d ",i);
        b[i-np]=a[i];               // <--- error here
        printf("b[%d]=%d\n",i-np,b[i]); /*Here is 1 unexpected thing happening, the program is not picking up correct value of array a at index i.*/
    }
    

    您正在索引b[i-np],这将在i &lt; np 时破坏b,它将是。

    我无法提出解决方案,因为我不明白“逆时针旋转数组”是什么意思。

    【讨论】:

    • 假设 a={1,2,3,4} 我们必须以逆时针方式将其旋转一个位置,那么最终数组将是:{2,3,4,1}
    • @sahildhawan 请举一个涉及np的例子。在您的问题中发布输入数组、np 的值和预期的输出数组,而不是在评论中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多