1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Bess [88]
3 years ago
8

Write a C++ program that overloads a function named LinearSearch that searches an array of data of either integer data type, flo

at, or double for a specific data. Test it in main by creating and assigning values to an array of integers, float, and double and searching each of them for some specific data.Please circle the part that is doing function/operator overloading and explain the advantages of using function/operator overloading in your code.
Computers and Technology
1 answer:
frutty [35]3 years ago
3 0

Explanation:

#include <iostream>

#include <vector>

#include <iomanip>

using namespace std;

/*

Overloading means equal function name with different call prototype each.

It's a useful tool that lets you think once and write similar role code twice or more times keeping equal function name.

Please look at lines 12-14: they are overloaded call prototype versions of LinearSearch with identical mission (to know if a value is present in a set)

*/

//#define GENERICS

#ifndef GENERICS                                                                // Conditional compilation statement

bool LinearSearch(vector<int>   ,int);

bool LinearSearch(vector<float> ,float);

bool LinearSearch(vector<double>,double);

#else                                                                           // Conditional compilation statement (uncomment line 10 statement)

template <typename genType>

bool LinearSearch(vector<genType> v, genType value);                            // LinearSearch (generic type parameters version)

#endif

int main(int argc, char *argv[]) {                                              // Test code follows:    

vector<int> vInt    {1,                     -2,     3,          -4,     5};     // Test data (int)

vector<float> vFlo  {1.23456f,              -2.35f, 3.1416f,    -4.7f,  5.55f}; // Test data (float)

vector<double> vDou {1.234567890123456789,  -2.35,  3.1415927,  -4.7,   5.55};  // Test data (double)

cout <<3 << "        is " << (LinearSearch(vInt,3)      ?"":"not ") << "in: {";for(auto x:vInt) cout << setw(11) << x;cout << "}\n";

cout <<3.1416f<< "   is " << (LinearSearch(vFlo,3.1416f)?"":"not ") << "in: {";for(auto x:vFlo) cout << setw(11) << x;cout << "}\n";

cout <<3.14   << " is " << (LinearSearch(vDou,3.1416) ?"":"not ") << "in: {";for(auto x:vDou) cout << /*setprecision(9)<<*/setw(11) << x;cout << "}\n";

return 0;

}

#ifndef GENERICS

// Overloaded function code follows:

bool LinearSearch(vector<int> v, int value) {                                   // LinearSearch (integer parameters version)

bool found = false;                                                             // Until now we haven't found the value we are searching for,

for(auto x:v) found = found || x == value;                                      // but if it is here found becomes true.

return(found);

}

bool LinearSearch(vector<float> v, float value) {                               // LinearSearch (float parameters version)

bool found = false;

for(auto x:v) found = found || x == value;

return(found);

}

bool LinearSearch(vector<double> v, double value) {                             // LinearSearch (double float parameters version)

bool found = false;

for(auto x:v) found = found || x == value;

return(found);

}

/*

So far, so good. It works! But, despite there was little effort involved, redundant code remains.

Is there any solution? Yes! It's called generic programming.

*/

#else

/*

Consider the following code:

   - Template statement at first accepts a generic type as a parameter (genType)

   - genType is used for both parameters (array and value to search)

   - Template statements located after generic version of LinearSearch instructs compiler to generate each type version

Conclusions:    

   - Compared with overloaded versions generic code is more elegant and avoids completely redundancy

   - It enhances overloading while keeping its advantages

*/

template <typename genType>

bool LinearSearch(vector<genType> v, genType value) {   // LinearSearch (generic type parameters version)

bool found = false;                                     // Until now we haven't found the value we are searching for,

for(auto x:v) found = found || x == value;              // but if it is here found becomes true.

return(found);

}

template bool LinearSearch(vector<int>   ,int);

template bool LinearSearch(vector<float> ,float);

template bool LinearSearch(vector<double>,double);

#endif

Download cpp
You might be interested in
Once the reaction time experiment in your MakeCode micro:bit program is
scZoUnD [109]

Once the reaction time experiment in your MakeCode micro:bit program is complete, Review the tutorial video to make sure you've completed all the steps correctly.

<h3>What is reaction time experiment?</h3>

A  reaction time experiment is known to be one that has different set of neural processes. This experiment is known to be one that has been set up to measure or look into the response time for something that one can see.

Note that when the reaction time experiment in your MakeCode micro:bit program is complete, it is better to go over the tutorial video to be sure you that you have completed all the steps correctly.

learn more about reaction time from

brainly.com/question/6167212

5 0
2 years ago
Which types of charts are examples found in Excel? Check all that apply.
marysya [2.9K]

Answer: 1,2,4,5,6

Explanation:

columns

bar

histogram

pie

scatterplot

5 0
3 years ago
Which logical operators perform short-circuit evaluation?.
Sergio039 [100]

Answer:

Short-circuit evaluation is performed with the not operator.

8 0
3 years ago
Which system procedure is normally done with both hand valves opened on a gauge manifold connected to a refrigeration system?
mars1129 [50]

Answer:

The answer is "Evacuation".

Explanation:

The evacuation in coolants was its removal from the coolant of heat, air, and non-condensable toxic gases. Its seal HVAC system is drawn to a vacuum and use a pressure washer throughout this process.  

  • It loses the air and water vapor throughout the coolant point is called it.
  • The two primary goals are accomplished by evacuating, that are eliminates content and hydrates.
8 0
3 years ago
In the glare of the sun, it is hard to see and be seen. Name six precautions
BartSMP [9]
<span>In the glare of the sun, it is hard to see and be seen. Some precautions would be :
1) </span><span>Have a polarized sunglasses ready, they can help reduce glare.
2) Use your sun visor – it can help to block out the sun. 
3) Leave more following space between you and the next vehicle. 
4) Drive with your headlights on to increase your visibility to other drivers. 
</span>5) Keep your windshield clean, inside and out, avoid storing papers or
     other items on the dashboard. 
6) If having a difficult time seeing the road, use the  lane markings to help
     guide you.
7 0
4 years ago
Other questions:
  • What year did bill gates form traf-o-data?
    8·1 answer
  • In the year of our Lord 66, the Emperor Nero, being at that time in the twenty-ninth year of his life and the thirteenth of his
    10·1 answer
  • The java compiler requires that a source file use the ________ filename extension question 3 options: 1) .class 2) .h 3) .java
    8·1 answer
  • Select the correct answer from each drop-down menu.
    8·1 answer
  • Commercial applications are never free<br><br> -True<br><br> -False
    9·1 answer
  • What does gps stand for ?
    15·2 answers
  • Who invented the machine known as colossus?
    9·2 answers
  • Assume n represents the number of inputs. The possible number of states within a truth table can be calculated using which of th
    10·1 answer
  • Compare and contrast Charles bebbage and Blaise Pascal inventions<br>​
    14·1 answer
  • True or False. A Windows Server 2016 that was installed in Desktop Experience mode can be converted to Server Core mode.
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!