Knowee
Questions
Features
Study Tools

Write a function, which prints all numbers in common between two given arrays, with each number printed only once.  The function returns number of common elements.Assume that both arrays contain each number only once.int common(int *a, int lena, int *b, int lenb);Note: the function does printing.Note: in the output shown, the last number printed is the return value of the function common.

Question

Write a function, which prints all numbers in common between two given arrays, with each number printed only once.  The function returns number of common elements.Assume that both arrays contain each number only once.int common(int *a, int lena, int *b, int lenb);Note: the function does printing.Note: in the output shown, the last number printed is the return value of the function common.

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Here is a function in C that accomplishes this task:

#include <stdio.h>

int common(int *a, int lena, int *b, int lenb) {
    int count = 0;
    for(int i = 0; i < lena; i++) {
        for(int j = 0; j < lenb; j++) {
            if(a[i] == b[j]) {
                printf("%d ", a[i]);
                count++;
                break;
            }
        }
    }
    printf("\n");
    return count;
}

This function works by iterating over each element in the first array, and for each element, it iterates over the second array to check if there are any matches. If a match is found, it prints the number and increments a counter. The counter is then returned at the end of the function.

This problem has been solved

Solution 2

Here is a simple implementation in C:

#include <stdio.h>

int common(int *a, int lena, int *b, int lenb) {
    int count = 0;
    for(int i = 0; i < lena; i++) {
        for(int j = 0; j < lenb; j++) {
            if(a[i] == b[j]) {
                printf("%d ", a[i]);
                count++;
                break;
            }
        }
    }
    printf("\n");
    return count;
}

int main() {
    int a

This problem has been solved

Solution 3

Here is a simple implementation in C:

#include <stdio.h>

int common(int *a, int lena, int *b, int lenb) {
    int count = 0;
    for(int i = 0; i < lena; i++) {
        for(int j = 0; j < lenb; j++) {
            if(a[i] == b[j]) {
                printf("%d ", a[i]);
                count++;
                break;
            }
        }
    }
    printf("\n");
    return count;
}

int main() {
    int a[] = {1, 2, 3, 4, 5};
    int b[] = {4, 5, 6, 7, 8};
    int lena = sizeof(a) / sizeof(a[0]);
    int lenb = sizeof(b) / sizeof(b[0]);
    printf("Number of common elements: %d\n", common(a, lena, b, lenb));
    return 0;
}

This function works by iterating over each element in the first array, and for each element, it iterates over the second array to check if there is a match. If a match is found, it prints the number and increments a counter. The counter is returned at the end of the function.

This problem has been solved

Similar Questions

Write a program to eliminate the common elements in the given 2 arrays and print only the non-repeating elements and the total number of such non repeating elements.Input Format:The first line contains space separated values, denoting the size of the two arrays in integer format respectively.The next two lines contains the space-separated integer arrays to be compared.Sample Input:5 41 2 8 6 52 6 8 10Sample Output:1 5 103

Create an array of integers, then create an array of pointers pointing to each element in the integer array. Print the values using both arrays. Write the code in c

c program to print unique elements in an array

Problem statementMahi is working on a program to find the intersection of two sorted arrays.Help Mahi by writing a function that takes two sorted arrays and their sizes as input and prints the common elements (intersection) of the two sorted arrays.Function Specificationsvoid findIntersection(int arr1[], int size1, int arr2[], int size2)Input format :The first line of input contains an integer value 'N1', representing the number of elements in the first array.The second line of input consists of N1 space-separated integers arr1[i], representing the elements of the first sorted array.The third line of input contains an integer value 'N2', representing the number of elements in the second array.The fourth line of input consists of N2 space-separated integers arr2[i], representing the elements of the second sorted array.Output format :The output displays display the intersection of the two arrays, with elements separated by spaces.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ N1, N2 ≤ 101 ≤ arr1[i], arr2[i] ≤ 100Sample test cases :Input 1 :41 2 3 432 4 6Output 1 :2 4 Input 2 :71 2 3 4 5 6 754 5 6 7 8Output 2 :4 5 6 7 Input 3 :101 2 3 4 5 6 7 8 9 101010 20 30 40 50 60 70 77 100 78Output 3 :10

You are given two lists of different lengths of positive integers. Write an algorithm to count the number of elements that are not common to each list.InputThe first line of the input consists of an integer - listInput1_size, an integer representing the number of elements in the first list (N).The second line consists of N space-separated integers representing the first list of positive integers.The third line consists of an integer- listInput2_size, representing the number of elements in the second list (M).The last line consists of M space-separated integers representing the second list of positive integers.OutputPrint a positive integer representing the count of elements that are not common to both the lists of integers.ExampleInput:111 1 2 3 4 5 5 7 6 9 101011 12 13 4 5 6 7 18 19 20Output:12Explanation:The numbers that are not common to both lists are [1, 1, 2, 3, 9, 10, 11, 12, 13, 18, 19, 20].So, the output is 12.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.