C++ programming A user wants to store a set of N number - value of N is input by
ID: 3762596 • Letter: C
Question
C++ programming
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, 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 and the reversed vector. [ use vector reverse operation – Refer to this URL ]
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 and print the elements.
Please referr to the following address for more info on this question.
http://students.cse.tamu.edu/jdass/coursematerials/assignments/Assignment_04.pdf
Its question number 5
Explanation / Answer
#include <iostream>
#include<cstdlib>
#include<ctime>
#include<vector>
#include <algorithm>
using namespace std;
int main() {
// your code goes here
cout<<"Please enter the number of numbers in the array: ";
int len;
cin>>len;
cout<<"Please enter "<<len<<" numbers (separated by spaces): ";
int i,temp,r;
vector<int> my,rev;
for(i=0;i<len;i++)
{
cin>>temp;
my.push_back(temp);
rev.push_back(temp);
}
cout<<' ';
cout<<' ';
cout<<"The elements of the vector are:"<<' ';
for(i=0;i<len;i++)
{
cout<<my[i];
if(i<len-1)
{
cout<<",";
}
}
cout<<' ';
cout<<"-------------------------"<<' ';
srand((unsigned)time(NULL));
cout<<"Updating Vector elements"<<' ';
for(i=0;i<3;i++)
{
r = rand()%len;
cout<<"Random number "<<r<<' ';
cout<<"Element at index "<<my[i]<<' ';
my[i] += 10;
rev[i] += 10;
cout<<"Updated element "<<my[i]<<' ';
cout<<"-------------------------"<<' ';
}
reverse(rev.begin(),rev.end());
cout<<' ';
cout<<"The updated vector is"<<' ';
for(i=0;i<len;i++)
{
cout<<my[i];
if(i<len-1)
{
cout<<",";
}
}
cout<<' ';
cout<<' ';
cout<<"Computing difference vector"<<' ';
cout<<' ';
cout<<"Original vector"<<' ';
for(i=0;i<len;i++)
{
cout<<my[i];
if(i<len-1)
{
cout<<",";
}
}
cout<<' ';
cout<<' ';
cout<<"Reversed vector"<<' ';
for(i=0;i<len;i++)
{
cout<<rev[i];
if(i<len-1)
{
cout<<",";
}
}
cout<<' ';
cout<<' ';
cout<<"Difference vector"<<' ';
for(i=0;i<len;i++)
{
cout<<abs(rev[i]-my[i]);
if(i<len-1)
{
cout<<",";
}
}
cout<<' ';
cout<<' ';
cout<<"After trimming the second half"<<' ';
for(i=0;i*2<len+1;i++)
{
cout<<abs(rev[i]-my[i]);
if((i+1)*2<len)
{
cout<<",";
}
}
cout<<' ';
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.