Question

In: Computer Science

1. Write a program array_compare.c that determines the number of elements are different between the two...

1. Write a program array_compare.c that determines the number of elements are different between the two integer arrays (same size) when compared element by element (first element with first element, second with second, and so on). The program should include the following function. Do not modify the function prototype.

void count_diff(int *a1, int *a2, int n, int *num_diff);

The function takes two integer array parameter a1 and a2 of size n. The function stores the number of elements that are different to the variable pointed by the pointer variable num_diff.

The function should use pointer arithmetic – not subscripting – to visit array elements. In other words, eliminate the loop index variables and all use of the [] operator in the function. The main function should ask the user to enter the size of the arrays and then the elements of the arrays, and then declare the arrays. The main function calls the count_diff function. The main function displays the result.

Example input/output #1: Enter the length of the array: 5

Enter the elements of the first array: -12 0 4 2 36

Enter the elements of the second array: -12 0 4 2 36

Output: The arrays are identical

Example input/output #2:

Enter the length of the array: 4

Enter the elements of the first array: -12 0 4 36

Enter the elements of the second array: 12 0 41 36

Output: The arrays are different by 2 elements

Solutions

Expert Solution

If you need any clarifications/corrections kindly comment

Please give a Thumps Up if you like the answer.

Program

#include<stdio.h>

void count_diff(int *a1, int *a2, int n, int *num_diff);
int main()
{
int a1[50],a2[50],i,n,num_diff;

printf("Enter the length of the array:");
scanf("%d",&n);
printf("Enter elements of first array:");
for(i = 0; i <n; ++i)
scanf("%d", a1 + i);

printf("Enter elements of second array:");
for(i = 0; i <n; ++i)
scanf("%d", a2 + i);

count_diff(a1,a2,n,&num_diff);
if(num_diff==0)
printf("\nThe arrays are identical\n");
else
printf("\nThe arrays are different by %d elements\n",num_diff);

return 0;

}
void count_diff(int *a1, int *a2, int n, int *num_diff)
{

int i, count=0;
   for(i = 0; i < n; i++)
   {
       if(*a1 != *a2)
       {
                 count++;
       }
       a1++;
       a2++;
   }
*num_diff=count;
}

Output


Related Solutions

Write a program in JAVA that takes in two words, and then it recursively determines if...
Write a program in JAVA that takes in two words, and then it recursively determines if the letters of the first word are contained, in any order, in the second word. If the size of the first word is larger than the second then it should automatically return false. Also if both strings are empty then return true. YOU MAY NOT(!!!!!!!!!!): Use any iterative loops. In other words, no for-loops, no while-loops, no do-while-loops, no for-each loops. Use the string...
write a mips program to find the number of elements of the array that are divisible...
write a mips program to find the number of elements of the array that are divisible by 3.
write a MIPS program to ask user to input the number of elements of array
write a MIPS program to ask user to input the number of elements of array
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should be only 3) in it and prints out the number of photos in the gallery.
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Count the number...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Your program should also keep a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT