2016-01-09 60 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; 
} 
相關問題