Good evening master programmer Can you solve problem 2 and 3 I will see what I d
ID: 3777244 • Letter: G
Question
Good evening master programmer Can you solve problem 2 and 3 I will see what I did wrong And learn from you master! Thank u Question 2. Use the standard template library vector.h to implement the followings. (Lecture 19) Step 1. Create a vector including numbers 1.1, 2.2, 3.3, 4.4, 5.5 6.6, 7.7, 8.8, 9.9, 1.1, 2.2, 3.3. Display the contents. Step 2. Randomly shuffle the vector's contents. Display the contents Step 3. Sort the vector's elements. Display the contents. Step 4. Search for number 3.3, and display whether it is found in the vector. Step 5. Display the count of number 1.1 in the vector. Step 6. Find the Maximum and Minimum value of the vector and display them. Step 7. Calculate OneHundred TimesPlusOne of the vector and display the result. Question 3. Make a template out of the following file and test on different data types, including int, double and string. (Lecture 18) #include Kiostream> #includeExplanation / Answer
Solution for question 3:
#include <iostream>
template<typename T> T myMax(T one, T two)
{
T bigger;
if(one < two)
{
bigger = two;
}
else
{
bigger = one;
}
return bigger;
}
int main()
{
int i_one = 3, i_two = 5;
cout<<"The max of "<<i_one<<" and "<<i_two<<" is "<<myMax(i_one, i_two)<<endl;
double d_one = 6.7, d_two = 3.4;
cout<<"The max of "<<d_one<<" and "<<d_two<<" is "<<myMax(d_one, d_two)<<endl;
string s_one = "hi", s_two = "hey";
cout<<"The max of "<<s_one<<" and "<<s_two<<" is "<<myMax(s_one, s_two)<<endl;
}
OUTPUT:
The max of 3 and 5 is 5
The max of 6.7 and 3.4 is 6.7
The max of hi and hey is hi
Solution for question 2
/*
* main.cpp
*
* Created on: 26-Nov-2016
* Author: kasturi
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//using a function to print the elements of the vector into the console
void print(vector<float> &items)
{
for (std::vector<float>::iterator it = items.begin() ; it != items.end(); ++it)
std::cout << *it<< ' ' ;
std::cout << ' ';
}
int main()
{
//declaring a vector
vector<float> vector;
float num = 1.1;
//adding values into vector . You can one after the other or through for loop.
for(int i=1; i<=9; i++)
{
vector.push_back(num);
num = 1.1 * (i+1);
}
vector.push_back(1.1);
vector.push_back(2.2);
vector.push_back(3.3);
//printing the vector
print(vector);
//using built-in random shuffle function to shuffle the elements of the vector
random_shuffle(vector.begin(), vector.end());
//printing the vector after shuffling
print(vector);
//sorting the vector
sort(vector.begin(), vector.end());
//printing the vector after sorting
print(vector);
bool isValuePresent = false;
int counter = 0;
for (std::vector<float>::const_iterator it = vector.begin() ; it != vector.end(); ++it)
{
if(*it == 3.3)
isValuePresent = true;
if(*it == 1.1)
counter++;
}
if (isValuePresent)
cout<<"3.3 is found"<<endl;
else cout<<"3.3 is not found"<<endl;
cout<<"Count of 1.1 is "<<counter<<endl;
float max = *max_element(vector.begin(), vector.end());
cout<<"Maximum element is "<<max<<endl;
float min = *min_element(vector.begin(), vector.end());
cout<<"Minimum element is "<<min<<endl;
return 0;
}
OUTPUT:
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 1.1 2.2 3.3
7.7 2.2 8.8 5.5 1.1 9.9 1.1 3.3 2.2 4.4 3.3 6.6
1.1 1.1 2.2 2.2 3.3 3.3 4.4 5.5 6.6 7.7 8.8 9.9
3.3 is found
Count of 1.1 is 2
Maximum element is 9.9
Minimum element is 1.1
// I didnt get what oneHundredTimesPlusOne meant, So didnt implement that.. let me know and will do it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.