Question

In: Computer Science

create a linear search function in a c++ comparing 2 arrays that are unsorted and has...

create a linear search function in a c++ comparing 2 arrays that are unsorted and has random numbers. count how many numbers they have similiar

Solutions

Expert Solution

Code:

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4.    int arr[5]={1,2,3,4,5};
  5.    int i, num, n, c=0, count=0;
  6.    int pos=100;
  7.    int arr2[5]={3,4,5,6,7};
  8.    cout<<"Linear search : ";
  9.    for(int j =0 ; j<5 ;j++){
  10.         num=arr2[j];
  11.    c=0;
  12.    pos=100;
  13.    for(i=0; i<5; i++)
  14.    {
  15.        if(arr[i]==num)
  16.        {
  17.            c=1;
  18.            pos=i+1;
  19.            break;
  20.        }
  21.    }
  22.    if(c!=0)
  23.    {
  24.           count++;
  25.    }
  26.    }
  27.    cout<<"The no.of similar numbers in 2 arrays are "<<count;
  28.    return 0;
  29. }

Related Solutions

Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
c++ LINEAR SEARCH ARRAYS OF OBJECTS Use the Item class defined in question #12. 1. (3...
c++ LINEAR SEARCH ARRAYS OF OBJECTS Use the Item class defined in question #12. 1. (3 pts) Declare an array of 1024 Item objects. Assuming the array is now sorted by Value (price per item * quantity). 2. Write a function (with both declaration/prototype (3 pts) and definition (6 pts) ) that takes an array of Item objects, an integer as array size and two references to integers that represents the start_index and end_index of Item object elements whose Values...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
Using C++: Create a function to search an undirected weighted graph and find the highest weighted...
Using C++: Create a function to search an undirected weighted graph and find the highest weighted edge between two specific values. This should include a class declaration and the definition for all required members that are needed to support the search. NO NEED to code those members. You can assume any other data structures such as stack, heap, or linked list is defined (so you can use their standard functions without declaring or defining them).
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these...
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these structure emplate <typename T> class Tree {    struct TreeNode    {        T mData;        TreeNode* mLeft = nullptr;        TreeNode* mRight = nullptr;        TreeNode* mParent = nullptr;        bool mIsDead = false;        TreeNode()        {        }        TreeNode(T tData) : TreeNode()        {            mData = tData;...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
In C++ write a function to find a product of two matrices using arrays. The function...
In C++ write a function to find a product of two matrices using arrays. The function should be general and should accept any size matrices.
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one...
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one dimensional array, Ages, which will hold 4 ages. Each age is an int.        b. Write a for loop and print, in reverse order the 4 values stored in memory assuming that the ages in the previous question have already been entered with a space between each value. Use subscript notation.                                     short cnt; c. Do the same as above, but use pointer...
C++ Create a vector of student records (minimum 15+ records) which is unsorted (For Vector, refer...
C++ Create a vector of student records (minimum 15+ records) which is unsorted (For Vector, refer to Chapter 7.11 or 17.3, or you may use Array of structures) - You should read student records from a file into the vector in C++ or in Java program. - Display the unsorted 10+ student records (entire record, not just ID) and associated test scores. - Each Student record should include Student ID, Name, GPA, Student Address, and a pointer which points to...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT