Question: Part 1: (I have part 1 code done, i just need help with part 2 of the problem) Write a C++ program that uses a
Part 1: (I have part 1 code done, i just need help with part 2 of the problem)
Write a C++ program that uses a function to search a 2D vector of floats. The function will have 2 parameters:
1. A 2 dimensional vector of floats, v
2. The floating point number that you are searching for, item
The function will return a vector of objects. Each object will contain the row and column of each instance where the item is found in the 2D vector. If the vector does not contain the item that it is searched for, the vector returned will only have 1 object. Within this object, the value -1 will be stored in the row member. Output to the console screen a message indicating that the vector did not contain this item if this is the case
[Below is PART 1]
#include
#include
using namespace std;
struct location
{
int row, col;
};
vector
{
location L;
vector
unsigned int row, col, size;
unsigned int num_of_rows = v.size();
for (row = 0; row < num_of_rows; row++)
{
size = v[row].size();
for (col = 0; col < size; col++)
{
if (v[row][col] == item)
{
L.row = row;
L.col = col;
locations.push_back(L);
}
}
}
if (locations.size() == 0)
{
L.row = -1;
locations.push_back(L);
}
return locations;
}
void Display_1d_Vector(const vector
{
unsigned int size = v.size();
for (unsigned int i = 0; i < size; i++)
cout << v[i] << ' ';
cout << endl;
}
int main()
{
vector
vector
int i;
unsigned int num_of_rows;
float item = 0;
for (i = 0; i < 3; i++)
row.push_back(i);
numbers.push_back(row);
row.clear();
for (i = -2; i < 4; i = i + 2)
row.push_back(i);
numbers.push_back(row);
num_of_rows = numbers.size();
cout << "2D vector contents - >" << endl;
for (i = 0; i < num_of_rows; i++)
Display_1d_Vector(numbers[i]);
cout << "***************" << endl << endl;
vector
locations = Search_2d_Vector(numbers, item);
cout << "The Item that you searched for is: " << item << endl;
if (locations[0].row == -1)
cout << " This vector did not contain the item that you searched for." << endl;
else
{
cout << "The item that you searched for can be found at the following location(s) ->" << endl;
for (int i = 0; i < locations.size(); i++)
cout << locations[i].row << ' ' << locations[i].col << endl;
}
cin.get();
return 0;
}
Part 2:
Rewrite the Search_2D_Vector function so that it can work with any data type. **Show that this function can work with a 2D vector of type float and a 2D vector of type char**
Can anyone help me add the 3rd parameter which is "2D vector of type char" please! Thanks!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
