Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write C++ code 3 Finding common number in two arrays You are given two (unordered) arrays A and B. You want to find the smallest

write C++ code

3 Finding common number in two arrays You are given two (unordered) arrays A and B. You want to find the smallest common number in A and B. For this, you are implement a function called ECCommonNumber, which takes five parameters and return a boolean (true if there is a common number) and false otherwise. The five parameters are (from the first to the last): 1. A: the first array (of integers) 2. szA: size of the first array 3. B: the second array (of integers) 4. szB: size of the second array 1 5. val: the (smallest) common integer found. Note: this number should be part of returned value to the caller. That is, after calling ECCommonNumber, val will be the found common integer. If there is no common integer, val can be of any value. The following lists things to consider when you implement this function. 1. First, you should define the function prototype (i.e, the signature of the fuction: the types of the parameters). Note: the two arrays cannot be changed by your function (we will have test to ensure that). This is because usually the caller doesnt expect the arrays will change (so called side effect) after calling the function. 2. You must implement the function as efficient as possible. Recall that efficiency is one of the most important aspects of C++ programming. You should carefully think about the algorithm you are to use. The most straightforward algorithm is simply comparing each pair of numbers in the two lists. However, this is very slow when the arrays get large. We will have run time check to ensure your code is fast when the sizes of array increase. Hint: I would suggest using binary search on sorted array; do you see why this is faster? 3. Dont reinvent the wheel! If you can, try to use the provided standard library functions. These include: (a) Sort: C++ has a function for sorting. To sort an array arr, you can simply call: std::sort(arr, arr+sz); where sz is the size of the array. Try it yourself! (b) Binary search: C++ has the binary search implemented for you. Google to see how it works.

starter code

// Given two arrays of integers, return true (and also store the smallest integer that is in both arrays). Return false if no such integer exists

// For example, suppose A1={1, 5, 3, 1} and A2={3, 7, 2}. Then ECCommonNumber(A1, 4, A2, 3, val) would return true (and val would be 3 after function call returns). Here, A1 is the first array, 4 is the size of the first array, A2 is the second array and 3 is the size of the second array.

// Note: you must implement your algorithm as efficiently as possible

// Also, you need to define the function yourself: how are you going to take

// Tip: don't reinvent the wheel; try to use C++ standard library functions

#include

#include

using namespace std;

bool ECCommonNumber(your code here)

{

// your code here...

}

//

#include

using namespace std;

#include "ECCommonNumber.cpp"

template

void ASSERT_EQ(T x, T y)

{

if( x == y )

{

cout << "Test passed: equal: " << x << " " << y << endl;

}

else

{

cout << "Test FAILED: equal: " << x << " " << y << endl;

}

}

int main()

{

int arr1[] = {1, 3, 5, 1};

int arr2[] = {3, 7, 2};

int valCommon;

bool f1 = ECCommonNumber(arr1, 4, arr2, 3, valCommon);

ASSERT_EQ(f1, true);

ASSERT_EQ(valCommon, 3);

int arr3[] = {7, 2, 100, -3, 0};

int valCommon2;

bool f2 = ECCommonNumber(arr1, 4, arr3, 5, valCommon2);

ASSERT_EQ(f2, false);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions