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

write a program with a function to fill a vector with 100 random numbers between

ID: 3767012 • Letter: W

Question

write a program with a function to fill a vector with 100 random numbers between 1 and 500. then pass the vector to a function that will sort it (sort(V.begin(), V.end());). you will need # include <algorithm>. then pass it to a function that will remove all duplicates. then pass to a function which will display the vector with 10 numbers on a line. write a program with a function to fill a vector with 100 random numbers between 1 and 500. then pass the vector to a function that will sort it (sort(V.begin(), V.end());). you will need # include <algorithm>. then pass it to a function that will remove all duplicates. then pass to a function which will display the vector with 10 numbers on a line. write a program with a function to fill a vector with 100 random numbers between 1 and 500. then pass the vector to a function that will sort it (sort(V.begin(), V.end());). you will need # include <algorithm>. then pass it to a function that will remove all duplicates. then pass to a function which will display the vector with 10 numbers on a line.

Explanation / Answer

#include <bits/stdc++.h>
// #include <iostream>
// #include <algorithm>
using namespace std;

void fillvector(vector<int> * vp){
   for(int i=0;i<100;i++){
       vp->push_back(1+rand()%500);
   }
}

void sort(vector<int> * vp){
   sort((*vp).begin(),(*vp).end());
}

void removedupl(vector<int> * vp){
   (*vp).erase( unique( (*vp).begin(), (*vp).end() ), (*vp).end() );
}
void display(vector<int> v){
   int count=0;
   int l=v.size();
   while(l>10){
       for(int i=0;i<10;i++){
           cout<<v[count]<<" ";
           count++;
       }
       cout<<endl;
       l=l-10;
   }
   for(int i=0;i<l;i++){
       cout<<v[count]<<" ";
       count++;
   }
   cout<<endl;
}

int main(){
   srand(time(NULL));
   vector<int> v;
   fillvector(&v);
   sort(&v);
   removedupl(&v);
   display(v);


   return 0;
}