c - 我如何保存 ANSI C 数组中位置的值和索引?

标签 c arrays indexing int

我只是想知道如何将数组“cadenaNumeros”的值和索引保存为整数,如果用户条目是这样的:123456789或48934013或183853025...等。

我想用 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;
}

最佳答案

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");
}

关于c - 我如何保存 ANSI C 数组中位置的值和索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11421836/

相关文章:

c - 在 Windows 的 C/C++ 中打开 com 端口时将 HANDLE 作为参数传递

mysql - 索引两列表的所有字段

sql-server - 相对于batch的查询成本是100%

arrays - 使用数组创建对象的对角线使用 for in 循环

r - 使用df [col]访问列给出:错误 'x'对于 'sort.list'必须是原子的

C 语言国际象棋游戏 - 有些棋子不动

c - 如何释放 Bison 的内存?

无法访问地址 X 处的内存

java - 了解数组的克隆方法

python - 替换 numpy 数组中特定索引处的元素