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

Scholars propose that effective leadership behaviour varies from one situation to another and that a leader...
Scholars propose that effective leadership behaviour varies from one situation to another and that a leader needs to take situational factors into account. Based on your studies in this course, explain the Normative Decision model.
We are given an array of n numbers A in an arbitrary order. Design an algorithm...
We are given an array of n numbers A in an arbitrary order. Design an algorithm to find the largest and second largest number in A using at most 3/2n -2 comparisons. (i) describe the idea behind your algorithm in English (3 points); (ii) provide pseudocode (5 points); (iii) analyze the number of comparisons used in your algorithm (2 points).
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...
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...
(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...
Translate this algorithm to code on C compare(char array[ ]) word = split(array, " "); //gets...
Translate this algorithm to code on C compare(char array[ ]) word = split(array, " "); //gets the first word before the blank space if word == Hello { print("I am saying hello"); }else if (word == Bye) print("I am saying goodbye"); } Example--------------------- char array = "Hello Ana"; compare(array); char array1 = "Bye Ana" compare(array1); result "I am saying hello" "I am saying goodbye"
Scholars propose that effective leadership behaviour varies from one situation to another. Based on your studies...
Scholars propose that effective leadership behaviour varies from one situation to another. Based on your studies in this course, describe and explain the Hersey-Blanchard situational leadership model.
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.
Searching an Array for an Exact Match C++ This question is on MindTap Cengage Summary In...
Searching an Array for an Exact Match C++ This question is on MindTap Cengage Summary In this lab, you use what you have learned about searching an array to find an exact match to complete a partially prewritten C++ program. The program uses an array that contains valid names for 10 cities in Michigan. You ask the user to enter a city name; your program then searches the array for that city name. If it is not found, the program...
(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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT