C 比较函数中的数组
我的函数C 比较函数中的数组,c,C,我的函数countCopies即使得到正确的输入也不起作用。它所要做的就是将一个整数数组作为输入,然后在该数组中搜索第二个输入x的重复项 int main() { char intArray[100]; //The integer array can only hold 100 integers int i, x, j; printf("Please enter a couple of integers and when you're done enter end. ")
countCopies
即使得到正确的输入也不起作用。它所要做的就是将一个整数数组作为输入,然后在该数组中搜索第二个输入x的重复项
int main() {
char intArray[100]; //The integer array can only hold 100 integers
int i, x, j;
printf("Please enter a couple of integers and when you're done enter end. ");
i = 0;
while (scanf("%d", &intArray[i++]) == 1)
/*empty loop*/;
scanf("%*s");
printf("Enter x:");
scanf("%d", &x);
printf("Copies = %d\n", countCopies(intArray, i, x));
}
int countCopies(int a[], int n, int x) {
int count = 0;
int j = 0;
for (j = 0; j < n - 1; j++) {
if (a[j] == x) {
count++;
}
}
return count;
}
intmain(){
char intArray[100];//整数数组只能容纳100个整数
int i,x,j;
printf(“请输入两个整数,完成后输入end。”);
i=0;
而(scanf(“%d”,&intArray[i++])==1)
/*空循环*/;
scanf(“%*s”);
printf(“输入x:”);
scanf(“%d”和&x);
printf(“拷贝数=%d\n”,countCopies(intArray,i,x));
}
int countCopies(int a[],int n,int x){
整数计数=0;
int j=0;
对于(j=0;j
循环的不正确:您应该将测试更改为j
。
C中惯用的for
循环:for(j=0;j
精确地迭代n
次,j
取0
到n-1
的值,这些值恰好对应于n
元素数组中的所有有效位置
请注意,数组的元素类型错误:它应该是int
,而不是char
。您还应该检查第一个循环中的数组边界,以及最后一个scanf
的转换是否成功
以下是更正的版本:
#include <stdio.h>
int countCopies(int a[], int n, int x);
int main(void) {
int intArray[100]; //The integer array can only hold 100 integers
int i, x;
printf("Please enter a series of integers, end the list with the word end.\n");
for (i = 0; i < sizeof(intArray) / sizeof(*intArray); i++) {
if (scanf("%d", &intArray[i]) != 1)
break;
}
if (scanf("%d", &x) == 1) {
printf("too many numbers\n");
return 1;
}
scanf("%*s"); /* skip the end word. Note that any word is OK */
printf("Enter x:");
if (scanf("%d", &x) == 1) {
printf("Copies = %d\n", countCopies(intArray, i, x));
}
return 0;
}
int countCopies(int a[], int n, int x) {
int j, count = 0;
for (j = 0; j < n; j++) {
if (a[j] == x) {
count++;
}
}
return count;
}
#包括
整数份拷贝数(整数a[],整数n,整数x);
内部主(空){
int intArray[100];//整数数组只能容纳100个整数
int i,x;
printf(“请输入一系列整数,以单词end结尾。\n”);
对于(i=0;i
循环的不正确:您应该将测试更改为j
。
C中惯用的for
循环:for(j=0;j
精确地迭代n
次,j
取0
到n-1
的值,这些值恰好对应于n
元素数组中的所有有效位置
请注意,数组的元素类型错误:它应该是int
,而不是char
。您还应该检查第一个循环中的数组边界,以及最后一个scanf
的转换是否成功
以下是更正的版本:
#include <stdio.h>
int countCopies(int a[], int n, int x);
int main(void) {
int intArray[100]; //The integer array can only hold 100 integers
int i, x;
printf("Please enter a series of integers, end the list with the word end.\n");
for (i = 0; i < sizeof(intArray) / sizeof(*intArray); i++) {
if (scanf("%d", &intArray[i]) != 1)
break;
}
if (scanf("%d", &x) == 1) {
printf("too many numbers\n");
return 1;
}
scanf("%*s"); /* skip the end word. Note that any word is OK */
printf("Enter x:");
if (scanf("%d", &x) == 1) {
printf("Copies = %d\n", countCopies(intArray, i, x));
}
return 0;
}
int countCopies(int a[], int n, int x) {
int j, count = 0;
for (j = 0; j < n; j++) {
if (a[j] == x) {
count++;
}
}
return count;
}
#包括
整数份拷贝数(整数a[],整数n,整数x);
内部主(空){
int intArray[100];//整数数组只能容纳100个整数
int i,x;
printf(“请输入一系列整数,以单词end结尾。\n”);
对于(i=0;i
要扩展chqrlie的答案
(i=0;i
的代码按以下顺序执行:
变量i
设置为零
测试条件i/*do stuff*/
中的代码,否则,将退出循环
应用增量i++
回到2
这意味着,在循环迭代中,当i增加到n时,for循环在循环中的代码执行之前中断,因此,如果要访问包含n个元素的数组,则访问的最后一个元素的索引为n-1。因此,chqrlie提到了C循环范式。为了扩展chqrlie的答案
(i=0;i
的代码按以下顺序执行:
变量i
设置为零
测试条件i/*do stuff*/
中的代码,否则,将退出循环
i++
这意味着,在循环迭代中,当i增加到n时,for循环在循环中的代码执行之前中断,因此,如果要访问包含n个元素的数组,则访问的最后一个元素的索引为n-1。因此,chqrlie提到了C循环范式。将
j
更改为j
为什么这scanf(%*s”)代码>??@ameyCU,请看@AshishAhuja我知道这是什么意思,但我不明白为什么在这里使用它(我看不出有任何理由在这里使用它。)@ameyCU:请看我的答案:scanf(%*s”)代码>跳过结束
字。像往常一样,不明显的代码应该被注释。将j
更改为j
为什么这scanf(“%*s”)代码>??@ameyCU,请看@AshishAhuja我知道这是什么意思,但我不明白为什么在这里使用它(我看不出有任何理由在这里使用它。)@ameyCU:请看我的答案:scanf(%*s”)代码>跳过结束
字。和往常一样,不明显的代码应该被注释。我建议cal