Computer Science, asked by gaznaferali4001, 1 year ago

Find elements which are present in first array and not in second in c

Answers

Answered by Anonymous
0

Even worse, you don't protect the program from writing outside the array. The C programming language doesn't protect against this wrong behavior, therefore you as the programmer must do this.

Before every write to an array you must check that the array index is valid. For example:

assert(0 <= idx && idx < arr_len);

arr[idx] = value;

Only if you can prove mathematically that the assert condition is always true, can you leave out the assert. Follow this advice, and your programs will be better than 80% of the existing C programs.

To reduce the complexity of the main function, you should define a function like this:

bool intarray_contains(const int *arr, size_t arr_len, int value) {

...

}

Then you don't need to write this code in main.

After that, to ensure that your program works correctly (it currently doesn't), you should extract the calculation into a separate function:

int min_not_present_in(const int *arr1, size_t arr1_len, const int *arr2, size_t arr2_len) {

...

}

This function definition allows you to do the calculation without entering the values every time. Like this:

int positive[] = { 1, 2, 3 };

int odd[] = { 1, 3, -1 };

int even[] = { -2, 0, 2 };

assert(min_not_present_in(positive, 3, odd, 3) == 2);

assert(min_not_present_in(positive, 3, even, 3) == 1);

To make all this work, you need the following lines at the top of your file:

#include <assert.h>

#include <stdbool.h>

Answered by padmaangmo007
1

Find elements which are present in first array and not in second using C

Similar questions