简体   繁体   中英

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.

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. So, if we have one id, then we have one truck.

So, after that we have both a & b arrays filled with 3 integers each array and the number of trucks updated.

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. 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] . If I don't find a the same value, then I go to a.id[1] to do the same step.

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.

Use different variables for the loops to avoid this.

Also be careful not to print uninitialized elements of c.id .

Another point is that your usage pos is buggy. 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 */
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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