简体   繁体   English

如何在数组中查找一个值并将其复制到 C 语言中的另一个值?

[英]How to find a value in array and copy it to another one in C language?

I writing a code where I read 3 truck id's from standard input and store them within an array.我编写了一个代码,从标准输入中读取了 3 个卡车 ID,并将它们存储在一个数组中。

The array of id's is part of an structure called tTruckList, where I store both id and the number of trucks that we have. id 数组是名为 tTruckList 的结构的一部分,我在其中存储 id 和我们拥有的卡车数量。 So, if we have one id, then we have one truck.所以,如果我们有一个 id,那么我们就有一辆卡车。

So, after that we have both a & b arrays filled with 3 integers each array and the number of trucks updated.因此,在这之后,我们同时拥有ab arrays,每个数组都填充了 3 个整数,并更新了卡车的数量。

Then I want to compare each position of the array a.id to each position of the array b.id and if the value is the same, then copy it in a third array c.id within the c truck list. Then I want to compare each position of the array a.id to each position of the array b.id and if the value is the same, then copy it in a third array c.id within the c truck list.

To do that I wrote for loop inside another for loop.为此,我在另一个 for 循环中编写了 for 循环。 So I can compare fist a.id[0] to each position of the array b.id and if the value is the same I copy it within c.id[i] .所以我可以将拳头a.id[0]与数组b.id的每个 position 进行比较,如果值相同,我将其复制到c.id[i]中。 If I don't find a the same value, then I go to a.id[1] to do the same step.如果我没有找到相同的值,那么我 go 到 a.id[1] 执行相同的步骤。

However, this final step is not working with my current code and I don't know where is the mistake.但是,这最后一步不适用于我当前的代码,我不知道错误在哪里。 Could you help me?你可以帮帮我吗?

Here is my code thank you:这是我的代码谢谢:

#include <stdio.h>
#define MAX 3

typedef struct {
    int id[MAX];
    int numTrucks;
} tTruckList;
    
int main (int argc, char** argv) {
    
    tTruckList a, b, c;
    a.numTrucks = 0;
    int i;
    int pos;
    int aux;
    int aux2;
    
    for(i=0; i<MAX; i++){
        printf("id of truck a >>\n");
        scanf("%d", &a.id[i]);
        a.numTrucks = a.numTrucks + 1;
    }
    
    for(i=0; i<MAX; i++){
        printf("id of truck b >>\n");
        scanf("%d", &b.id[i]);
        b.numTrucks = b.numTrucks +1;
    }
    
    c.numTrucks = 0;
    
    for(i=0; i<MAX; i++){
        aux = a.id[i];
        pos=0;
        for(i=0; i<MAX; i++){
            aux2 = b.id[i];
            if(aux == aux2){
                c.id[i-pos]= aux;
                c.numTrucks= c.numTrucks +1;
            } else {
                pos = pos + 1;
            }
        }
    }
    
    printf("first id c: %d\n", c.id[0]);
    printf("second id c: %d\n", c.id[1]);
    printf("third id c: %d\n", c.id[2]);
    printf("number of trucks c: %d\n", c.numTrucks);
    
return 0;
}

You used the same variable i in the both outer and inner loop, so the outer loop will run only once because i will be MAX after running the inner loop.您在外循环和内循环中使用了相同的变量i ,因此外循环将只运行一次,因为运行内循环后i将是MAX

Use different variables for the loops to avoid this.为循环使用不同的变量来避免这种情况。

Also be careful not to print uninitialized elements of c.id .还要注意不要打印c.id的未初始化元素。

Another point is that your usage pos is buggy.另一点是您的使用pos是错误的。 You will have to see the all elements to determine if there is an element with the same value in the worst case, so thinking that there are no element with the same value seeing only one element is wrong.在最坏的情况下,您必须查看所有元素以确定是否存在具有相同值的元素,因此认为只有一个元素没有具有相同值的元素是错误的。

    c.numTrucks = 0;
    /* initialize c.id not to print unintialized variables */
    for(i=0; i<MAX; i++){
        c.id[i]=0;
    }
    
    for(i=0; i<MAX; i++){
        aux = a.id[i];
        for(pos=0; pos<MAX; pos++){ /* use different variable than the outer loop in the inner loop */
            aux2 = b.id[pos];
            if(aux == aux2){
                c.id[c.numTrucks]= aux;
                c.numTrucks= c.numTrucks +1;
                break; /* an element with the same value is found, so no additional iteration is required */
            }
            /* delete else part not to take extra actions seeing only one element */
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM