Question

In: Computer Science

Propose an algorithm in C to match numbers / tokens (words) from one array to another...

Propose an algorithm in C to match numbers / tokens (words) from one array to another array and pull out the matching numbers.

Solutions

Expert Solution

CODE: C Programming Language

#include<stdio.h>
void printMatching(int arr1[],int n1,int arr2[],int n2){
   int i,j;
   for(i=0;i<n1;i++){
       for(j=0;j<n2;j++){
           if(arr2[j]==arr1[i]){
               printf("%d ",arr1[i]);
               break;
           }
       }
   }
}
int main(){
   int arr1[]= {10,20,30,40,50};
   int arr2[]= {10,30,40,60,70};
   int n1 = sizeof(arr1)/sizeof(arr1[0]);
   int n2 = sizeof(arr2)/sizeof(arr2[0]);
   printMatching(arr1,n1,arr2,n2);
   return 0;
}

======================================================================

SCREENSHOT OF THE CODE:

======================================================================

OUTPUT:

Thank you. Please ask me if you have any doubt.


Related Solutions

(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array...
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size...
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size 5. Show that, when the group size is 7, the algorithm still runs in linear time.
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a number output result: true if x in a false otherwise Sideeffect NA Plan if (top < bottom) result := false else // get the middle of the array (refining) middle := (top - bottom)/2 + bottom (refining by +1 or -1 or integer division …) // example what is midpoint between 6 and 8 // (8-6)/2 = 1 we need to add 6 to...
c++ give ways of filling array of uniform random numbers
c++ give ways of filling array of uniform random numbers
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each...
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it’s not a duplicate of a number already read. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem. Your solution must include a function called isUnique() that returns 1 (true) if the number input is...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
In C an array Numbers is declared float Numbers[10][10]. Due to a bug, the program tried...
In C an array Numbers is declared float Numbers[10][10]. Due to a bug, the program tried to reference Numbers[11][-13]. Which element of Numbers will actually be accessed?
Mini JAVASCRIPT Algorithm Exercise "Sum All In Array" Objectives Create a function that adds all numbers...
Mini JAVASCRIPT Algorithm Exercise "Sum All In Array" Objectives Create a function that adds all numbers of a provided array (arr), accounting for non-integer values in arr. The output should return an integer. Notes Remember your data types? If the element is an integer (8), it should be added. If the element is a string with a number value ("8"), it should be added. If the element is not a number, or if it is a string with a non-number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT