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
What type of formatting does the clip below demonstrate
Alex17521 [72]

In order to answer the question i would need a screenshot of the clip.

5 0
4 years ago
Which command displays the status of the ipsec installation?
Alexandra [31]
Character command shows the status.
8 0
3 years ago
Greg wants to check the amount of requests his website receives. What test can he use?
Alenkinab [10]

Answer:

D - Website load test

Explanation:

Just got it right

4 0
3 years ago
there are these numbers that I've seen before but I don't know what they mean can someone help. I will give brainiest
Ne4ueva [31]

Answer:

Those numberss are binary code

Explanation:

Computers use binary - the digits 0 and 1 - to store data. A binary digit, or bit , is the smallest unit of data in computing. It is represented by a 0 or a 1. Binary numbers are made up of binary digits (bits), eg the binary number 1001.

5 0
3 years ago
Read 2 more answers
How to write a program to draw 1000 random points on a frame in python language for coding?
harina [27]

Answer:

You could generate random numbers between the pixel amount of the height and width than place the dots at those random points

Explanation:

Do you need the code or can you make it using my "headstart"

8 0
3 years ago
Other questions:
  • Which one of the following features can control left and right indents using markers?
    5·2 answers
  • I need help to find out what is wrong with this program and my variable, "exponent" comes out as 0 in the output instead of the
    14·1 answer
  • The network of satellites monitoring continental movement is referred to as
    7·1 answer
  • Attempting to determine a password that is not known to the attacker is often called ____.
    13·1 answer
  • To generate a report with exact results based on specific criteria it is best to base the report on a(n) ____________________ cr
    6·1 answer
  • Write a try block in which you prompt the user for an integer and display the name in the requested position in the names array.
    11·1 answer
  • 1.printer is an example of......... device.<br><br>​
    11·2 answers
  • FREEEEE 100 POINTS COME TAKE COME COME COME<br><br> only if u like da black panther
    8·2 answers
  • TRUE OR FALSE
    14·1 answer
  • Write an algorithm to calculate the average<br> value of array of integer elements
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!