In C programming language write a function that takes two arrays
as input m and n as well as their sizes: size_m and size_n,
respectively.
Then it checks for each element in m, whether it exists in n.
The function should update a third array c such that for each
element in m:
the corresponding position/index in c should be either
1, if this element exists in m, or
0, if the element does not exist in n
#include <stdio.h>
//function to udpate array for each element
//1, if this element exists in m, or
//0, if the element does not exist in n
void updateArray(int m[][3], int n[][3], int size_m, int
size_n)
{
//array declaration
int newArr[size_m][size_m];
//update the newArr
for(int i=0; i<size_m; i++)
{
for(int j=0; j<size_m; j++)
{
if(m[i][j] == n[i][j])
newArr[i][j] = 1;
else
newArr[i][j] = 0;
}
}
//display the new array
for(int i=0; i<size_m; i++)
{
for(int j=0; j<size_m; j++)
{
printf("%d ", newArr[i][j]);
}
printf("\n");
}
}
int main()
{
//array declaration and initialization
int m[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int n[][3] = {{1, 12, 3}, {10, 11, 6}, {17, 8, 19}};
//function calling
updateArray(m, n, 3, 3);
return 0;
}
OUTPUT:
1 0 1
0 0 1
0 1 0
Get Answers For Free
Most questions answered within 1 hours.