C A user wants to store a set of N number value of N is inp

C++

A user wants to store a set of N number - value of N is input by the user such that N >= 10 [Validate Input].

The system first reads the value of “N”, prompts the user to enter “N” elements, and stores the elements in a vector by name numbers. [ use vector push operation ]

Print the elements of the vector by passing the vector numbers to a function printVector () that prints the elements of a vector in row order (line).

Now, we need to add a constant value to three elements in the vector, the positions of which are decided by three random numbers between 0 and N-1.

Print the random number (index), print the element at index (random number), print the new value element +10, and update the vector. [ use element at index operation] Print the updated vector.

Now, the user wants to create a difference vector diffVect that holds elementwise absolute differences between the original vector (numbers) and the reversed vector ( revnumbers). [ use vector reverse operation ]

Print the original vector, reversed vector, and the difference vector.

Lastly, since the difference vector is symmetrical the user wants to trim the vector by deleting second half of the vector. [ use vector pop operation ] Trim the difference vector diffVect and print the elements.

Output:

Enter the number of elements to store ? 12

Enter the elements > [ User enters 12 elements]

The elements of the vector are 45, 47, 12, 90, 24, 1, 57, 28, 93, 17, 11, 30

Updating Vector elements

Random Index Number : 2

Element at the index 2: 12

New Value: 22

-----------------------------------

Random Index Number 5

Element at the index 5: 1

New Value: 11

-----------------------------------

Random Index Number: 10

Element at the index 10: 11

New Value: 21

-----------------------------------

The updated Vector is 45, 47, 22, 90, 24, 11, 57, 28, 93, 17, 21, 30

Computing Difference Vector

Original Vector 45, 47, 22, 90, 24, 11, 57, 28, 93, 17, 21, 30

Reversed Vector 30, 21, 17, 93, 28, 57, 11, 24, 90, 22, 47, 45

Difference Vector 5, 26, 5, 3, 4, 46, 46, 4, 3, 5, 26, 5

After trimming the second half 5, 26, 5, 3, 4, 46

Solution

#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;

void printVector(vector<int> v) {
int i;
for (i = 0; i < v.size(); ++i) {
cout << v.at(i) << \" , \";
}
cout << endl;
}
int main()
{
  
cout<<endl<<\"Enter the number of elements to store ? \";
int N;
cin>>N;
vector <int> vec(N);

cout<<endl<<\"Enter the elements >\";
for(int i=0;i<N;i++)
{
   cin>>vec.push_back(i);
}
cout<<\" [ User enters \"<<N<<\" elements]\";
cout<<endl<<\"The elements of the vector are \"<<printVector (vec);

cout<<endl<<\"Updating Vector elements\";
srand (time(NULL));
for(int i=0;i<3;i++)
{
int r =(N *rand()/RAND_MAX);
cout<<endl<<\"Random Index Number : \"<<r;
cout<<endl<<\"Element at the index 2: \"<<vec.at(r);
numsList.insert(numsList.begin()+r,(vec.at(r)+10);
cout<<endl<<\"New Value: \"<<vec.at(r);
cout<<endl<<\"-----------------------------------\";
}

cout<<endl<<\"The updated Vector is \"<<printVector (vec);
cout<<endl<<\"Computing Difference Vector\";
cout<<endl<<\"Original Vector \"<<printVector (vec);
vector <int> vecRev(N);
std::copy ( vec, vec+N, vec.begin() );
std::reverse(vecRev.begin(), vecRev.end());
cout<<endl<<\"Reversed Vector \"<<printVector (vecRev);
vector <int> diffVec(N);
cout<<endl<<\"Difference Vector \";
for(int i=0;i<vec.size();i++)
{
   diffVec.push_back(vec.at(i)-vecRev.at(i));
}
cout<<printVector(diffVec);

}

C++ A user wants to store a set of N number - value of N is input by the user such that N >= 10 [Validate Input]. The system first reads the value of “N”, pr
C++ A user wants to store a set of N number - value of N is input by the user such that N >= 10 [Validate Input]. The system first reads the value of “N”, pr
C++ A user wants to store a set of N number - value of N is input by the user such that N >= 10 [Validate Input]. The system first reads the value of “N”, pr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site