我正在尝试编写一个移动并切换数组前三个元素的代码。 main函数获取数组长度和元素,然后显示函数调用的结果。函数roll
从数组a1
获取元素,移动前三个元素,然后将它们存储到数组a2
。我必须使用指针下标,并且在函数中不能有[]
。我必须使用这个原型:
void roll(int *a1, int n, int *a2)
这是我写的代码。我一定不能正确调用函数,或者函数没有正确地将元素分配到数组a2
,因为我只输出0
。
#include <stdio.h>
void roll(int *a1, int n, int *a2)
{
int *p, *q;
q = a2;
for(p = a1; p < a1 + n; p++)
*p = *(p+2);
*(p+1) = *p;
*(p+2) = *(p+1);
*q = *p;
}
int main(void)
{
int i, x;
printf("Enter the length of the array:\n");
scanf("%d", &x);
int a[x];
int b[x];
// int new_arr[x];
printf("Enter the elements of the array:\n");
for(i = 0; i < x; i++)
scanf("%d", &a[i]);
roll(a, x, b);
printf("The output array is:");
// for(i = 0; i < x; i++);
printf("%d", *b);
return 0;
}
答案 0 :(得分:1)
所以,我没有编译过这个,但是:
for(p = a1; p < a1 + n; p++)
*p = *(p+2);
*(p+1) = *p;
*(p+2) = *(p+1);
*q = *p;
只会通过*p = *(p+2)
循环运行第一行for ()
。
你需要在那里放括号:
for(p = a1; p < a1 + n; p++)
{
*p = *(p+2);
*(p+1) = *p;
*(p+2) = *(p+1);
*q = *p;
}
答案 1 :(得分:1)
有些不清楚你的意思:
函数'roll'从数组a1获取元素,移动前三个元素,然后将它们存储到数组a2。
对数组进行传统的 Roll 操作会将 n th 元素与第一个元素进行交换(历史原点是值的滚动在计算器堆栈上到当前行)。如果是这种情况,您可以使用简单的:
void roll (int *a1, int n, int *a2)
{
int tmp = *a1; /* save the 1st element of a1 */
*a2++ = *(a1++ + --n); /* store the nth element of a1 as a2[0] */
while (--n) /* for the next n-2 elements */
*a2++ = *a1++; /* assign to a2 in order */
*a2 = tmp; /* assign the 1st of a1 as the nth of a2 */
}
您可以按照自己喜欢的方式对其进行索引,只需递减'n'
即可。将a1
的第一个元素保存为tmp
可以直接递增a1
和a2
(您的roll
函数会收到指针的副本)。
完全放弃,你可以做类似的事情:
#include <stdio.h>
void roll (int *a1, int n, int *a2)
{
int tmp = *a1; /* save the 1st element of a1 */
*a2++ = *(a1++ + --n); /* store the nth element of a1 as a2[0] */
while (--n) /* for the next n-2 elements */
*a2++ = *a1++; /* assign to a2 in order */
*a2 = tmp; /* assign the 1st of a1 as the nth of a2 */
}
int main(void)
{
int i, x;
printf ("Enter the length of the array: ");
if (scanf("%d", &x) != 1) {
fprintf (stderr, "error: invalid input.\n");
return 1;
}
int a[x], b[x], *p = b;
printf ("Enter the elements\n");
for (i = 0; i < x; i++) {
printf (" a[%2d]: ", i);
if (scanf ("%d", &a[i]) != 1) {
fprintf (stderr, "error: invalid input.\n");
return 1;
}
b[i] = a[i]; /* initialize b */
}
roll (a, x, b);
printf("The output array is:");
while (x--)
printf (" %2d", *p++);
putchar ('\n');
return 0;
}
(注意:如果你的目的只是将元素1和3从a1
交换到a2
,那么'n'
在函数中没有用处除了允许检查n > 2
以确保您不会尝试访问界限之外的数组,但这本身似乎令人怀疑)
示例使用/输出
$ ./bin/array_roll
Enter the length of the array: 5
Enter the elements
a[ 0]: 1
a[ 1]: 2
a[ 2]: 3
a[ 3]: 4
a[ 4]: 5
The output array is: 5 2 3 4 1
仔细看看,如果您有其他问题,请告诉我。另外,如果 Roll 的含义与我所描述的传统卷不同,请删除一行。
如果您只是想对a1
中a2
的前3个元素进行操作(注意:b
将需要完全初始化,如上所述),您可以只需使用:
roll (a, 3, b);
示例w / roll(a,3,b)
$ ./bin/array_roll
Enter the length of the array: 5
Enter the elements
a[ 0]: 1
a[ 1]: 2
a[ 2]: 3
a[ 3]: 4
a[ 4]: 5
The output array is: 3 2 1 4 5
示例滚动元素3向下,推动剩余
在您定义“roll”的评论之后,只需进行一次调整即可将当前roll
函数更改为roll3
函数,该函数将在{{{{}}中滚动第3个元素1}}到a1
的开头,然后按顺序填充a2
的剩余部分a2
。这里的扭曲不是循环a1
而是循环--n
没有帮助,因此您可以测试滚动索引并跳过从for (int i = 0; i < n-1; i++)
到a1
的复制,例如(a2
)。
您可以执行以下操作:
if (i == 2) {/* skip */}
呼叫:
void roll3 (int *a1, int n, int *a2)
{
*a2++ = *(a1 + 2); /* store the 3rd element of a1 as a2[0] */
n--; /* decrement n (we have filled 1-element) */
for (int i = 0; i < n; i++, a2++) { /* loop n-1 times, increment a2 */
if (i == 2) /* if we reach rolled index, increment a1 */
a1++;
*a2 = *a1++; /* assign elements */
}
}
使用roll3功能的示例
roll3 (a, x, b);
仔细看看,如果您有疑问,请告诉我。到目前为止,处理指针递增/递减和循环填充应该开始下沉。如果没有,最好的学习方法是取出2号铅笔和纸片,绘制$ ./bin/array_roll
Enter the length of the array: 5
Enter the elements
a[ 0]: 1
a[ 1]: 2
a[ 2]: 3
a[ 3]: 4
a[ 4]: 5
The output array is: 3 1 2 4 5
和{{ 1}}然后逐步完成循环绘制线,分配每个元素以查看哪个元素在哪里 - 以及如何。
答案 2 :(得分:0)
检查这是否是你想要的。
#include <stdio.h>
void roll(int *a1, int n, int *a2)
{
int *p, *q;
q = a2;
p = a1;
int temp=*p;
*p = *(p+2);
*(p+2)=*(p+1);
*(p+1)=temp;
for(int i=0;i<n;i++)
{
*(a2+i)=*(p+i);
}
}
int main(void)
{
int i, x;
printf("Enter the length of the array:\n");
scanf("%d", &x);
int a[x];
int b[x];
// int new_arr[x];
printf("Enter the elements of the array:\n");
for(i = 0; i < x; i++)
{
scanf("%d", &a[i]);
b[i]=a[i];
}
roll(a, x, b);
for(i = 0; i < x; i++)
printf("%d\n", *(b+i));
return 0;
}
答案 3 :(得分:0)
对每个帮助过的人......我让它工作......问题出在我的电话里......我有一个额外的';'在阻止它运行的for循环之后......非常感谢你们!