2012-07-10 60 views
-1

我只是想知道我怎麼能在一個整數保存我的數組「cadenaNumeros」的價值和指標,如果用戶的條目是這樣的:123456789或48934013或183853025 ...等如何保存在ANSI C數組中的位置的值和索引?

我想用FOR來做,但我的代碼沒有工作。

例如:如果我們有這個數組「123456789」,值將是2和索引= 1,這個數字將它保存在一個整數。

我編程在ANSI C.

#include <stdio.h> 
#include <stdlib.h> 

void main() 
{ 
    int a = 0, i = 0, indice, numero[20], vuelta, quedan, x; 
    int cadenaNumeros[20000]; 
    int num, num2; 

    printf("\n\nEscribe el numero de palancas: "); 
    scanf("%d", &num2); 

    printf("\n\nEscriba el numero de la palanca roja: "); 
    scanf("%d", &num); 

    printf("\n\nIntroduzca una cadena de numeros: "); 
    scanf("%d", &cadenaNumeros); 

    for(i = 0; cadenaNumeros[i] < num2; i++); { 
     cadenaNumeros[0] == numero[x]; 
     printf("%d", numero[x]); 
    } 

    system("PAUSE"); 
    return 0; 
} 
+0

'cadenaNumeros [0] == NUM​​ERO [X];'是一個空操作。另外,[爲什麼void main不好](http://www2.research.att.com/~bs/bs_faq2.html#void-main)。 – chris 2012-07-10 20:59:23

+3

作爲一個無操作是該行的問題最少。 'x'沒有被初始化,並且可能超出邊界('numero'也沒有初始化,btw)。 – eran 2012-07-10 21:02:26

+0

@chris這是正確的,即時通訊工作在DEV C++ 4.9,一切都運行完美。只是我可以保存索引和我的數組的值。 – 2012-07-10 21:03:19

回答

0
int index = -1, i = 0; 

// Assuming num2 is the number you're searching for 
// And the array size is 20000 
for(; i < 20000; i++) { 
    if(cadenaNumeros[i] == num2) { 
     index = i; 
     break; 
    } 
} 

if(index != -1) { 
    printf("Found %d at index %d\n", num2, index); 
} 
else { 
    printf("Number not found\n"); 
}