2016-01-09 62 views
0

我想知道用户输入的数字是否是数组中的数字。如何将数字与数组中的另一个数字进行比较? [c]

这里是我的代码:

#define ARR_SIZE 10 

int main() 
{ 
    int my_arr[10]; 
    int secnum = 0; 
    int i = 0; 

    for (i = 0; i < ARR_SIZE ; i++) 
    { 
     printf("Enter a number: "); 
     scanf("%d",&my_arr[i]); 
    } 

    printf("Enter the another number"); 
    scanf("%d",&secnum); 

    if(my_arr[i] == secnum) 
    { 
     printf("an ex"); 
    } 

} 

但它不工作。

如何将数字与另一个数字进行比较?

注意:我不知道指针,所以我需要无指针才能做到。

+0

请descrip,不起作用。永远不要忽略'scanf()'的返回值总是检查。 –

+0

'if'检查你的代码在哪里? – Pawan

+0

错误:下标值既不是数组也不是指针。我不需要在指针中使用,而且我也不会错误地指出错误。如何解决它? – NoNameP

回答

0

循环后,我等于ARR_SIZE10)。所以你比较my_arr[10]secnum0)。但my_arr[10]虽然在语法上正确,但指向未定义的值,因为数组的大小为10

0
#define ARR_SIZE 10 

int main() 
{ 
int my_arr[10]; 
int secnum = 0; 
int i = 0; 

for (i=0;i<ARR_SIZE ; i++) 
{ 
    printf("Enter a number: "); 
    scanf("%d",&my_arr[i]); 
} 
printf("Enter the another number"); 
scanf("%d",&secnum); 

for (i=0;i<ARR_SIZE ; i++) 
{ 
    if(my_arr[i]==secnum) 
    { 
     printf("Given number is in array\n"); 
     break; 
    } 
} 

} 
+0

它不适合我。对于这个前例,我需要检查另一个函数 – NoNameP

+0

@NoNameP然后,请将它作为约束添加到您的问题中,以及其他限制和规范。 – Ziezi

0

正如在评论中指出由OP的答案之一,OP显然需要一个例行检查数组中的关键。

因此,一旦我们已经保存了array,并已接受了key进行搜索,我们需要把这个arraykey传递给搜索功能将返回truefalse取决于key是否存在数组或者不在家。

#include <stdbool.h> // We need this for `true` and `false` bool values 

bool search(int [], int); // Function declaration 

/**** Function Definition *****/ 
bool search(int numbers[], int key) 
{ 
    int i; 

    for(i = 0; i < ARR_SIZE; i++) 
     if(numbers[i] == key) 
      return true; 

    return false; 
} 

/** Calling search function from main **/ 
... 
if(search(my_arr, secnum)) 
    printf("Number found in array!\n"); 
else 
    printf("Number could NOT be found in array!\n"); 
1

为什么它不起作用,代码有什么问题?

  • 你只是比较一个值而不是所有的数组元素。
  • scanf循环后i的值为10,因此arr[i]将超过 阵列并可能导致非法内存访问。

查看程序中的注释。

#define ARR_SIZE 10 
int main() 
{ 
    int my_arr[ARR_SIZE]; //Use ARR_SIZE because if ARR_SIZE changes, 10 won't causing unforseen errors. 
    int secnum = 0; 
    int i = 0; 

    for (i = 0; i < ARR_SIZE ; i++) 
    { 
     printf("Enter a number: "); 
     scanf("%d",&my_arr[i]); 
    } 

    printf("Enter the another number"); 
    scanf("%d",&secnum); 

    for (i = 0; i < ARR_SIZE ; i++) // Ensures you are comparing secnum with each array element. 
    { 
     if(my_arr[i] == secnum) 
     { 
      printf("an ex"); //Do you wish to break here because one you find a match, the goal is attained :) 
     } 
    } 
} 
0

要在数组中找到一个值,您应该遍历它并将每个值与所需的数值进行比较。您还应该检查返回值scanf()以控制它实际读取的项目数。看看这个审查代码是否有帮助:

#include <stdio.h> 

#define ARR_SIZE 10 

int read_numbers(int a[], int size) { 
    int i = 0; 
    int r = 0; 
    while (i < size) { 
     printf("Please, enter a number (%d of %d): ",i+1,size); 
     r = scanf(" %d",&a[i]);   // the space before will ignore any trailing newline 
     if (r == EOF) break;   // if scanf fails return 
     if (r != 1) {     // if user don't enter a number, repeat 
      printf("Wrong input!\n"); 
      scanf("%[^\n]*");   // will read and ignore everything lweft on stdin till newline 
      continue; 
     } 
     ++i; 
    } 
    return i;  // return size, unless scanf fails 
} 

int find_number(int a[], int size, int x) { 
    int i = 0; 
    while (i < size && a[i] != x) ++i; 
    return i; // if x isn't in the array returns size 
} 

int main() 
{ 
    int my_arr[ARR_SIZE]; 
    int secnum = 0; 
    int i = 0; 
    int n = 0; 

    n = read_numbers(my_arr, ARR_SIZE); 
    if (n < ARR_SIZE) { 
     printf("Warning, only %d numebers read out of %d!\n",n,ARR_SIZE); 
    } // if happens you have uninitialized elements in array 

    printf("Now, enter the value you want to find.\n"); 
    if (read_numbers(&secnum, 1) != 1) {  // reuse the function 
     printf("Sorry, an error has occurred.\n"); 
     return -1; 
    } 

    i = find_number(my_arr, n, secnum);  // If all went right n==ARR_SIZE 
    if (i < n) {    // a match has been found 
     printf("Found!\n"); 
    } else { 
     printf("Not found.\n"); 
    } 
    return 0; 
} 
相关问题