I am trying to compare elements of two arrays according their order for example:
int a[] = { 1, 2, 3 };
int b[] = { 1, 2, 4 };
Having 3 elements there is no problem with a for
-loop and condition
if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2])
But how can I do the same with n elements in arrays?
The condition should looks like: if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && ... && a[n] == b[n])
but it's inappropriate.
// Comparing elements of two one-dimensional arrays with
// equal sets of elements according their order.
#include <stdio.h>
int main(void) {
// two one-dimensional arrays with 3 elements for example
int a[] = { 1, 2, 3 };
int b[] = { 1, 2, 4 };
// comparing elements:
for (int i = 0; i < 3; i ) {
if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]) // but if I have n - elements the problem appears
{
printf("all right\n");
}
else
{
printf("no all right\n");
}
}
}
CodePudding user response:
The great thing about for loops is that the incrementing variable i can actually be used to index into your array! Notice how you are comparing every element of a to every element of b incrementing by one. Your variable i is also incrementing by one. Your code right now checks the entire array 3 complete times, making your runtime three times longer than necessary, in addition to not being scalable. Fix it by comparing a[i] to b[i]. If they are not the same, print your message and break the loop using the break keyword. If you reach the end of the loop and they are all the same, print your success message.
CodePudding user response:
Having both a for
loop and an explicit comparison expression is redundant. You can solve the problem for an arbitrary number of elements with a simple function that takes both arrays and their length, compares the array elements and returns a boolean value:
#include <stdbool.h>
#include <stdio.h>
bool compare_arrays(const int *a, const int *b, size_t n) {
for (size_t i = 0; i < n; i ) {
if (a[i] != b[i])
return false;
}
return true;
}
int main() {
// two one-dimensional arrays with 3 elements for example
int a[] = { 1, 2, 3 };
int b[] = { 1, 2, 4 };
size_t a_len = sizeof(a) / sizeof(*a);
// comparing elements:
if (compare_arrays(a, b, a_len)) {
printf("all right\n");
} else {
printf("no all right\n");
}
return 0;
}
CodePudding user response:
This for loop
for (int i = 0; i < 3; i ) {
if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]) // but if I have n - elements the problem appears
{
printf("all right\n");
}
else
{
printf("no all right\n");
}
}
does not make sense because within it all three elements of the array are compared in the if statement in each iteration of the loop.
if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]) // but if I have n - elements the problem appears
What you need is that in each iteration of the loop to compare corresponding elements of the array with the given index i
.
So you can write for example
int i = 0;
while ( i < 3 && a[i] == b[i] ) i;
if ( i == 3 )
{
printf("all right\n");
}
else
{
printf("no all right\n");
}
you could form such a comparison of two arrays as a separate function as for example
int compare_arrays( const int a[], const int b[], size_t n )
{
size_t i = 0;
while ( i < n && a[i] == b[i] ) i;
return i == n;
}
The function returns 1
if two arrays are equal each other and 0
otherwise.
So in main you can write for example
if ( compare_arrays( a, b, 3 ) )
{
printf("all right\n");
}
else
{
printf("no all right\n");
}