Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write c++ program to do the following: a. Ask the user for the number of element

ID: 3601124 • Letter: W

Question

Write c++ program to do the following:
a. Ask the user for the number of elements in the array, and import them from the keyboard. After you save them in the memory, copy them in similar array which will be sorted. Finally, display all the elements of the vectors, the sorted one and not sorted one.
b. Create a container for at least 100 ints. Then you will be provided with second vector of 10 elements. You need to calculate how many of elements of the second vector are contained in the first one. Also need to calculate percent of how many elements are in vector, and how many are not.


c. There are two vectors for which you don’t know the size in advance. But, the sizes are same all the time. You need to show the message, are those two vectors equal.

d. In the vector of unknown size, and int type, you should count how many times one element is repeated.
e. Merge two sorted vectors into one.

Write c++ program to do the following:
a. Ask the user for the number of elements in the array, and import them from the keyboard. After you save them in the memory, copy them in similar array which will be sorted. Finally, display all the elements of the vectors, the sorted one and not sorted one.
b. Create a container for at least 100 ints. Then you will be provided with second vector of 10 elements. You need to calculate how many of elements of the second vector are contained in the first one. Also need to calculate percent of how many elements are in vector, and how many are not.


c. There are two vectors for which you don’t know the size in advance. But, the sizes are same all the time. You need to show the message, are those two vectors equal.

d. In the vector of unknown size, and int type, you should count how many times one element is repeated.
e. Merge two sorted vectors into one.

Write c++ program to do the following:
a. Ask the user for the number of elements in the array, and import them from the keyboard. After you save them in the memory, copy them in similar array which will be sorted. Finally, display all the elements of the vectors, the sorted one and not sorted one.
b. Create a container for at least 100 ints. Then you will be provided with second vector of 10 elements. You need to calculate how many of elements of the second vector are contained in the first one. Also need to calculate percent of how many elements are in vector, and how many are not.


c. There are two vectors for which you don’t know the size in advance. But, the sizes are same all the time. You need to show the message, are those two vectors equal.

d. In the vector of unknown size, and int type, you should count how many times one element is repeated.
e. Merge two sorted vectors into one.

Write c++ program to do the following:

b. Create a container for at least 100 ints. Then you will be provided with second vector of 10 elements. You need to calculate how many of elements of the second vector are contained in the first one. Also need to calculate percent of how many elements are in vector, and how many are not.


c. There are two vectors for which you don’t know the size in advance. But, the sizes are same all the time. You need to show the message, are those two vectors equal.

d. In the vector of unknown size, and int type, you should count how many times one element is repeated.
e. Merge two sorted vectors into one.

Explanation / Answer

//For every question assuming vector is of type int.

//There are some assumption for every question which is needed to do question. If any querry Please feel free to ask ,Thank you

a)

#include <iostream> //included for cin ,cout stream
#include <algorithm> //included for sorting algorithm
#include<vector> //included for vector container

using namespace std;
int main() {
int size;
cout<<"Enter the number of element in array"<<endl;  
cin>>size; //input from user number of element
int value;
vector<int> arr,arr2; //initilize two vector
while(size--){
cin>>value;
arr.push_back(value); //input vector value from keyboard
}
arr2=arr; //copy vector1 into vector2
for (vector<int>::iterator it = arr.begin() ; it != arr.end(); ++it) { //iterate over first vector
cout << " " << *it; //print element of first vector
}
cout<<endl;
sort(arr2.begin(),arr2.end()); //sort second vector
for (vector<int>::iterator it = arr2.begin() ; it != arr2.end(); ++it) {
cout << " " << *it;
}
return 0;
}

b)

//Here first and second vector should be given and we should write function here .

//first created vector according to requirement and our answer part is called function "fun"

#include <iostream>
#include<vector>
#include <stdlib.h> // use for rand function i.e used for random value generator
using namespace std;

void fun(vector<int> v1,vector<int>v2){
int count =0;//intial count 0
for (std::vector<int>::iterator it2 = v2.begin() ; it2 != v2.end(); ++it2){ //for every element of v2
for (std::vector<int>::iterator it1 = v1.begin() ; it1 != v1.end(); ++it1){ //for every element of v1
if(*it2==*it1){ //check if element of v2 equal v1 if match
count++; //increment count
break; //need to check another element of v2 with v1 :reason for use break
}
  
}
}
cout<<"number of element contained are:"<<count<<endl;  
cout<<"percentage of contained vector:"<<count*10<<"%."<<endl;
cout<<"percentage of absent vector:"<<100-count*10<<"%."<<endl;
  
}

int main() {
vector<int > v1(200);//becuase size is atleast 100 assuming its 200
for (std::vector<int>::iterator it = v1.begin() ; it != v1.end(); ++it){
*it = rand() % 100 + 1; //filled vector with random variable , it should be given in question
}
vector<int > v2(10);
for (std::vector<int>::iterator it = v2.begin() ; it != v2.end(); ++it){
*it = rand() % 100 + 1; //it should also given in question but here taken random values
}
  
fun(v1,v2);
return 0;
}

c)

#include <iostream>
#include<vector>

using namespace std;

void fun(vector<int> v1,vector<int>v2){

if(v1.size()==v2.size())
cout<<"Equal";
else
cout<<"Not Equal";

}


int main() {
vector<int> v1,v2;

//initilize vector with your own values for different result.

fun(v1,v2);
return 0;
}

d)

#include <iostream>
#include<vector>

using namespace std;

//function for count how many times element repeated here in function we don't know size of function here .
int countRepeatedElement(vector<int> v,int element){
int count=0;
for (std::vector<int>::iterator it = v.begin() ; it != v.end(); ++it){
if(*it==element)
count++;
}
return count;
}

int main() {
vector<int > v={1,1,1,1,2,3,4,2,4};
int element=1;
cout<<"count is:"<<countRepeatedElement(v,element)<<endl;
return 0;
}

e)

#include<iostream>
#include<vector>
using namespace std;

void mergeVector(vector<int> v1,vector<int> v2,vector<int> v3)
{
int i = 0, j = 0, k = 0;
int n1=v1.size();
int n2=v2.size();
  
while (i<n1 && j <n2)
{
if (v1[i] < v2[j])
v3[k++] = v1[i++];
else
v3[k++] = v2[j++];
}

// Store remaining elements of first vector
while (i < n1)
v3[k++] = v1[i++];

// Store remaining elements of second vector
while (j < n2)
v3[k++] = v2[j++];
cout << "Vector after merging" <<endl;
for (int i=0; i<v3.size(); i++)
cout << v3[i] << " ";
cout << endl;
}

int main()
{
vector<int> v1 = {1, 3, 5, 7};
vector<int> v2 = {2, 4, 6, 8};
vector<int> v3(v1.size()+v2.size());//creating a vector of size equal to sum of previous two vector
mergeVector(v1,v2,v3);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote