Home > Enterprise >  Comparing two arrays..in c
Comparing two arrays..in c

Time:04-28

I set up two arrays one of 50 units big, array1[50] which has 50 random integers from a range of 50-100 and another array which prompts the user to enter in 10 intgers from the same range of numbers. My problem is, how do i compare the two, im tyring to find the number of times the 10 user inputted numbers match the numbers stored in the array1[50] which holds the seedeed random numbers. Ive tried to do a for loop within a for loop, like this.

array2[10]
array[50]
int counter = 0;

for(int i = 0; i < 10;   i){
    for(int k = 0; k < 50;   k){  //i've tried this and it does not work, i don't know what else to do
        if( array2[i] == array[k]);
          counter;
    }
}
//any help is appreciated thanks.

CodePudding user response:

There are easier ways to achieve this using other libraries. But considering you're working with small arrays, you could use your code with the following modification:

array2[10]
array[50]
int counter = 0;

for(int i = 0; i < 10;   i){
for(int k = 0; k < 50;   k){  
if( array2[i] == array[k])counter  ;
}
}

That should work.

CodePudding user response:

As pointed out @yano & @Weather Vane a semicolon at the end of if( array2[i] == array[k]); makes it a separate statement. As a result counter is incremented every time and ends up with value 500.

Instead of comparing each element of input with all random value you can just prepare a master table for storing random values as indexes. This way, you only check number of times the user inputs.

Since the range is just [50..100], it can be done using array of size 101. For larger range say more than a million, 1000000 use dynamic memory (malloc() siblings).

  • To populate the random values you can use :
    #define RANGE_MIN 50
    #define RANGE_MAX 100
    #define RAND_NUM_COUNT 50

    srand (time(NULL));

    char master [RANGE_MAX   1] = {0};
    for (int ri = 0; ri < RAND_NUM_COUNT; ) {
        int xnum = RANGE_MIN   rand() % (RANGE_MAX - RANGE_MIN   1);
        if (master [xnum]) continue;
        master[xnum] = 1;
          ri;
    }
  • To count how many times user has guessed numbers right:
    int input[guesses];
    int ans = 0;
    for (int ui = 0; ui < guesses;   ui) {
        if (input[ui] < RANGE_MIN || input[ui] > RANGE_MAX)
            continue;
        if (master[input[ui]]) // correct guess / match
              ans;
    }
    printf ("User has guessed %d correctly out of %d guesses.\n", ans, guesses);
  • What if the user repeats his guesses, i.e. you want to count unique guesses only. You can reset a correct guess in master[] so that repetition is not counted.
    for (int ui = 0; ui < guesses;   ui) {
        if (input[ui] < RANGE_MIN || input[ui] > RANGE_MAX)
            continue;
        if (master[input[ui]]) {    // correct guess / match
              ans;
            master[input[ui]] = 0;  // next repeat is not counted
        }
    }
  •  Tags:  
  • c
  • Related