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

solve in c++ program please i need to copy it from you so please ( type it and p

ID: 3891749 • Letter: S

Question

solve in c++ program please i need to copy it from you so please ( type it and put the solution here ) so i can copy and paste the codes Write a function called small() that takes three arguments: the location of the first element k, the dimension nDim of a vector v, and the vector v itself, the function returns the index of the smallest element in the given vector from the location k to location nDim Write a function called swap that takes two arguments and swap their values. Write a main) program that input the dimension nDim and the elements of the vector, sort the elements of the vector in ascending order, and output the sorted elements. One way to sort the elements of a vector (there are more efficient ways) is to do the following Step 1: use the function small() to find the smallest element (among: X, X2, ..., xn), and use the function swap() to exchange it with the first element of the vector Step 2: use the function small() to find the smallest element (among: X2, Xz, ..., xn), and use the function swap() to exchange it with the second element of the vector Step 3: use the function small() to find the smallest element (among: X3, Xa, ..., xn), and use the function swap() to exchange it with the thircd element of the vector Step n-1: use the function small() to find the smallest element (among: xn1, Xn), and use the function swap() to exchange it with the (n-1)th element of the vector

Explanation / Answer

Hello there,

I have written the cpp code as per above requirements.

Its working fine and correctly.

#########################################

Code.cpp =>>>>>>>

#include<iostream>
#include<vector>

using namespace std;

int small(vector<int> &v,auto k,int nDim)
{
   int small = *k ;
   int index = (k-v.begin());
   int res=index;
   for(auto i=k; (i!=v.end()) && (index<nDim) ;i++,index++)
       if(*i<small) {
           small=*i;
           res=index;
       }
  
   return res;

}

void swap(vector<int>::iterator v1, vector<int>::iterator v2) {
   int temp=*v1;
   *v1=*v2;
   *v2=temp;  

}


int main() {
   int t,index,nDim,temp;
   vector<int>::iterator v1,i,v2;
   vector<int> v;
  
   //READING INPUTS FROM USER
   cout<<"Enter the vector dimension: ";
   cin>>nDim;

   cout<<"Enter the "<<nDim<<" vector elements: ";
   for(t=0;t<nDim;t++) {
       cin>>temp;
       v.push_back(temp);
   }
   ////////////////////////////

   cout<<"Before sorting"<<endl;
   for(t=0;t<v.size();t++)
       cout<<v[t]<<" ";
   cout<<endl;

  
   //SORTING VECTOR BASED ON FINDING SMALL AND SWAPPING IT      
   for(index=0,i=v.begin() ; i!=v.end() ; i++,index++) {
       t=small(v,i,v.size());
       v2=v.begin();
       advance(v2,t);
       swap(i,v2);
   }  
   ////////////////////////////////////////////////////////

   cout<<"After sorting"<<endl;
   for(t=0;t<v.size();t++)
       cout<<v[t]<<" ";
   cout<<endl;

   return 0;
}

######################################

Feel free to ask queries and modifications regarding above question.

I am here to help you!

Thank you !!! :)