C 数组:我一直得到输出:假。我的代码中有什么错误?

  • 本文关键字:代码 错误 数组 输出 c
  • 更新时间 :
  • 英文 :


问题提示:

编写程序检查两个长度相同的数组是否匹配在第一个数组上有一些移位。数组上的移位意味着移动到最左边元素移到最右边的位置。例如,如果数组a包含{4,6,1,2},那么它将是{6,1,2,4}。如果数组b包含{1,2,4,6},则数组a在2次移位后匹配数组b。当且仅当数组a可以变成时,程序将显示" true ">

在a上移位数之后数组b。否则程序将显示" false "。
Example input/output #1:
Enter the length of the input array: 4
Enter the elements of the first array: 2 8 1 4
Enter the elements of the second array: 1 4 8 2
Output: false
Example input/output #2:
Enter the length of the input array: 5
Enter the elements of the first array: 4 6 7 1 3
Enter the elements of the second array: 1 3 4 6 7
Output: true
1) Name your program arrays2.c.
2) In the main function, the program will ask the user to enter the length of the
arrays and the elements of each array.
3) Include the function shift() in the program. The shift() function
moves the leftmost element of array a to the rightmost position.
void shift(int a[], int n);
4) The main function calls the shift function, evaluates the arrays, and displays
the result.
下面是我的代码:
#include<stdio.h>
#define input
#define N 5
void shift(int a[], int n) {
int i;
int first_element = a[0];
for(i=1; i<n; i++) {
a[i-1]=a[i];
}
a[n-1] = first_element;
}
int main() {
int i, n;
int shift_counter = 0;
int first_array[100];
int second_array[100];
printf("Enter the length of the input array: ");
scanf("%d", &n);
printf("Enter the elements of the first array: ");
for(i=0; i<n; i++) {
scanf("%d", &first_array[n]);
}
printf("Enter the elements of the second array: ");
for(i=0; i<n; i++) {
scanf("%d", &second_array[n]);
}
while (shift_counter <= n) {
if (first_array == second_array) {
printf("Output: true");
break;
}
else{
if (shift_counter == n) {
printf("Output: false");
break;
}
shift_counter = shift_counter + 1;
shift(first_array, n);
}
}
return 0;

}

当我运行第二个示例输入

时,我得到了这个输出
Enter the length of the input array: 5
Enter the elements of the first array: 4
6
7
1
3
Enter the elements of the second array: 1
3
4
6
7
Output: false

理想情况下输出应该是true。我是C语言的新手,我相信数组在这种语言中的工作方式不同。我在哪里犯了错,我该如何改正?

first_array == second_arrayaka&first_array[0] == &second_array[0]总是false,因为它的两个不同的数组存储在不同的地址。这意味着您的while (shift_counter <= n)增加shift_counter,直到n,并由于if (shift_counter == n)而退出。shift()不会改变两个数组的地址,所以它做什么是无关紧要的(你可以把它注释掉,不会改变外部行为)。

其他问题:

  1. #define input未被使用
  2. #define N 5未被使用
  3. first_array和second_array的大小是固定的(100个元素),但是您不检查您读取的运行时大小。即缓冲区溢出。
  4. shift()不按您期望的方式工作。a[0]被复制到a[1],然后a[1]被复制到a[2]意味着你最终得到了{ a[n-1], a[0], a[0], ... }

这是一个可行的解决方案:

#include <string.h>
#include <stdio.h>
//#define DEBUG
void shift(int a[], int n) {
int last_element = a[n-1];
memmove(a + 1, a, sizeof(*a) * (n - 1));
a[0] = last_element;
}
#ifdef DEBUG
void print(char *prefix, int a[], int n) {
printf("%s", prefix);
for(int i = 0; i < n; i++) {
printf("%d%s", a[i], i + 1 < n ? ", " : "n");
}
}
#endif
void check(int a[], int b[], int n) {
for(int i = 0; i < n; i++, shift(a, n)) {
#ifdef DEBUG
print("a: ", a, n);
print("b: ", b, n);
#endif
if(!memcmp(a, b, sizeof(*a) * n)) {
printf("true");
#ifdef DEBUG
printf("n");
#endif
return;
}
}
printf("false");
#ifdef DEBUG
printf("n");
#endif
}
int main() {
int a[] =  { 4, 6, 1, 2 };
int a2[] =  { 6, 1, 2, 4 };
int a3[] =  { 5, 6, 1, 2 };

check(a, a2, sizeof(a) / sizeof(*a));
check(a, a3, sizeof(a) / sizeof(*a));
return 0;
}

,输出为truefalse,显示匹配和不匹配的运行。

如果您通过将//#define DEBUG更改为#define DEBUG来定义DEBUG,那么程序将显示更多的状态,以便您可以看到发生了什么:

a: 4, 6, 1, 2
b: 6, 1, 2, 4
a: 2, 4, 6, 1
b: 6, 1, 2, 4
a: 1, 2, 4, 6
b: 6, 1, 2, 4
a: 6, 1, 2, 4
b: 6, 1, 2, 4
true
a: 6, 1, 2, 4
b: 5, 6, 1, 2
a: 4, 6, 1, 2
b: 5, 6, 1, 2
a: 2, 4, 6, 1
b: 5, 6, 1, 2
a: 1, 2, 4, 6
b: 5, 6, 1, 2
false

最新更新