Question

All to be done in C++ no user input is required. Just need to create vectors and create functions to manipulate them as described.

Search 2D Vector - Part 2 Write a C++ program that uses a function to search a 2D vector of floats. The function will have 2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Part1

#include<iostream>
#include<vector>

using namespace std;
vector<vector<int>> search2DVector(vector<vector<float>> vec, float item);

int main()
{
   vector<vector<float>> vec = { { 0, 1, 2 } ,{-2, 0 , 2} };
   int rows, cols;
   cout << "2D vector contents ->\n";
   for (int i = 0; i < vec.size(); i++)
   {
       for (int j = 0; j < vec[i].size(); j++)
           cout << vec[i][j]<<" ";
       cout << endl;
   }
   float search;
   cout << "Item you searched for is : ";
   cin >> search;
   vector<vector<int>> ret = search2DVector(vec, search);
   cout << "the item you searched can be found at the following location(s)->\n";
   for (int i = 0; i < ret.size(); i++)
   {
       for (int j = 0; j < ret[i].size(); j++)
       {
           cout << ret[i][j] << " ";
       }
       cout << endl;
   }
   system("pause");
}

vector<vector<int>> search2DVector(vector<vector<float>> vec, float item)
{
   vector<vector<int>> retObject;
   vector<int> x,y1;
   for (int i = 0; i < vec.size(); i++)
   {
       for (int j = 0; j < vec[i].size(); j++)
       {
           if (item == vec[i][j])
           {
               x.push_back(i);
               x.push_back(j);
           }
       }
       retObject.push_back(x);
       x.clear();
   }
   return retObject;
}

=================================

//Output

2D vector contents ->
0 1 2
-2 0 2
Item you searched for is : 0
the item you searched can be found at the following location(s)->
0 0
1 1
Press any key to continue . . .

===========================

//Part2

#include<iostream>
#include<vector>

using namespace std;
template<typename T>
vector<vector<int>> search2DVector(vector<vector<T>> vec, T item);
//Function to erase the item at some location
template<typename T>
void Erase_vector_items(vector<vector<T>> &vec, T item);

int main()
{
   vector<vector<float>> vec = { { 0, 1, 2 } ,{-2, 0 , 2} };
   vector<vector<char>> chars = { { 'a', 'e', 'c' } ,{ 'd', 'u' , 'i' } };
   int rows, cols;
   cout << "2D vector contents ->\n";
   for (int i = 0; i < vec.size(); i++)
   {
       for (int j = 0; j < vec[i].size(); j++)
           cout << vec[i][j]<<" ";
       cout << endl;
   }
   float search;
   cout << "Item you searched for is : ";
   cin >> search;
   vector<vector<int>> ret = search2DVector(vec, search);
   cout << "the item you searched can be found at the following location(s)->\n";
   for (int i = 0; i < ret.size(); i++)
   {
       for (int j = 0; j < ret[i].size(); j++)
       {
           cout << ret[i][j] << " ";
       }
       cout << endl;
   }
   char s;
   cout << "2D vector contents ->\n";
   for (int i = 0; i < chars.size(); i++)
   {
       for (int j = 0; j < chars[i].size(); j++)
           cout << chars[i][j] << " ";
       cout << endl;
   }
   cout << "Character you searched for is : ";
   cin >> s;
ret = search2DVector(chars, s);
   cout << "the character you searched can be found at the following location(s)->\n";
   for (int i = 0; i < ret.size(); i++)
   {
       for (int j = 0; j < ret[i].size(); j++)
       {
           cout << ret[i][j] << " ";
       }
       cout << endl;
   }
   cout << "Enter an item to erase: ";
   cin >> search;
   Erase_vector_items(vec, search);
   cout << "2D vector contents after eraseing ->\n";
   for (int i = 0; i < vec.size(); i++)
   {
       for (int j = 0; j < vec[i].size(); j++)
           cout << vec[i][j] << " ";
       cout << endl;
   }
   cout << "Enter an character to erase: ";
   cin >> s;
   Erase_vector_items(chars, s);
   cout << "2D vector contents after eraseing ->\n";
   for (int i = 0; i < chars.size(); i++)
   {
       for (int j = 0; j < chars[i].size(); j++)
           cout << chars[i][j] << " ";
       cout << endl;
   }
   system("pause");
}
template<typename T>
vector<vector<int>> search2DVector<T>(vector<vector<T>> vec, T item)
{
   vector<vector<int>> retObject;
   vector<int> x,y1;
   for (int i = 0; i < vec.size(); i++)
   {
       for (int j = 0; j < vec[i].size(); j++)
       {
           if (item == vec[i][j])
           {
               x.push_back(i);
               x.push_back(j);
           }
       }
       retObject.push_back(x);
       x.clear();
   }
   return retObject;
}
template<typename T>
void Erase_vector_items(vector<vector<T>> &vec, T item)
{
   for (int i = 0; i < vec.size(); i++)
   {
       for (int j = 0; j < vec[i].size(); j++)
       {
           if (item == vec[i][j])
           {
               vec[i].erase(vec[i].begin() + j);
           }
       }
   }
}
/*Output
2D vector contents ->
0 1 2
-2 0 2
Item you searched for is : 0
the item you searched can be found at the following location(s)->
0 0
1 1
2D vector contents ->
a e c
d u i
Character you searched for is : c
the character you searched can be found at the following location(s)->
0 2

Enter an item to erase: 0
2D vector contents after eraseing ->
1 2
-2 2
Enter an character to erase: c
2D vector contents after eraseing ->
a e
d u i
Press any key to continue . . .
*/

Add a comment
Know the answer?
Add Answer to:
All to be done in C++ no user input is required. Just need to create vectors...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ Write a program that will use vectors to allow the user to input as many...

    c++ Write a program that will use vectors to allow the user to input as many values as desired, and pass that vector to a function to search for a number. The function returns the index of where the number is found or that it didn't find the number. Uses Vectors and searching.

  • c++ The program will be recieved from the user as input name of an input file...

    c++ The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to search for, when it find...

  • Any programming language may be used. Please indicate what language you have chosen. You are an...

    Any programming language may be used. Please indicate what language you have chosen. You are an analytics developer, and you need to write the searching algorithm to find the element. Your program should perform the following: Implement the Binary Search function. Write a random number generator that creates 1,000 elements, and store them in the array. Write a random number generator that generates a single element called searched value. Pass the searched value and array into the Binary Search function....

  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...

  • c++ I need help! create Student.h In this class, you are provided with a class skeleton...

    c++ I need help! create Student.h In this class, you are provided with a class skeleton for the Student type. This type should contain two member variables: a string called name a vector of doubles called grades Additionally, you should declare the following functions: A constructor which accepts a single string parameter called name A void function, addGrade which accepts a single double parameter and adds it to the grades vector A function which accepts no parameters and returns the...

  • Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit...

    Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit 1. For Erase-ArrayContent option, write complete code for the C++ function Erase described below. The prototype for Erase function is as follow: void Erase( int a[ ], int * N, int * Search-Element) The function Erase should remove all occurrences of Search-Element from the array al). Note that array a[ ] is loaded with integer numbers entered by the user through the keyboard. N...

  • .Your solution must include header, implementation file, and test files .In C++ write a code to...

    .Your solution must include header, implementation file, and test files .In C++ write a code to Consider a graphics system that has classes for various figures rectangles, squares, triangles, circles, and so on. For example, a rectangle might have data members for Height, Width and center point, while a square and circle might have only a center point and an edge length or radius. In a well-designed system, these would be derived from a common class, Figure. You are to...

  • I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...

    I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...

  • (Must be written in Python) You've been asked to write several functions. Here they are: yourName()...

    (Must be written in Python) You've been asked to write several functions. Here they are: yourName() -- this function takes no parameters. It returns a string containing YOUR name. For example, if Homer Simpson wrote this function it would return "Homer Simpson" quadster(m) -- this function takes a value m that you pass it then returns m times 4. For example, if you passed quadster the value 7, it would return 28. isRratedMovieOK(age) -- this function takes a value age...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT