Create C-Source application written in C++ for the question below. Any/all help
ID: 3546066 • Letter: C
Question
Create C-Source application written in C++ for the question below. Any/all help appreciated:
Write a function with the function prototype of
int compare(double a[], double b[], int npts);
It will compare values in two arrays of the same size. If the corresponding values in each element of the
two arrays are the same, the function returns 1. Otherwise, it returns 0. Test the function with function calls
compare(a, a, 4) and compare(a, b, 4) with the arrays a = [1, 2, 3, 4], b = [1, 3, 4, 5].
Pogram, #comments also help me understand this programming.
Explanation / Answer
#include <stdio.h>
int compare(double a[], double b[], int npts){ // function return type and parameters
int i;
for(i = 0; i < npts; i++){
if(a[i] != b[i]) return 0; /* checking each element one by one, if any of the corresponding elemrnts are not same, return 0 */
}
return 1; /* the control of the program reaches here. It means all the correspnding elements are equal, so return 1 */
}
int main(){
double a[] = {1, 2, 3, 4}, b[] = {1, 2, 3, 5}; // array declaration
printf("%d ", compare(a, a, 4)); // compare a with a
printf("%d ", compare(a, b, 4)); // compare a with b
}