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

Subject Write a complete C++program that utilizes the C++ Vector library and mee

ID: 3717179 • Letter: S

Question

Subject Write a complete C++program that utilizes the C++ Vector library and meets following requirements. As we explained in class, this vector is actually the integration of stack and array. ) Use vector only, not use array and library 2) Must use push back0 and pop_back0 methods from the Vector class. 3) Create a vector object (variable) and read a positive integer from the user with proper prompt. 4) Define a function named palindrome with one parameter that reverses (miors) this input integer using push back0 and pop back0 methods, following algorithm below. The prototype of this function can be either template function or regular function. If it is template, the prototype should be template T palindrome (T n); 20 points bonus will be given when you use this prototype. If regular, the prototype should be int palindrome (int n); (1) Extract digits of the input integer, starting from the right most digit, or the least significant digit. The algorithm should be similar to that use in Expressionl lab. (2) Push the digit into the vector. Substeps (1) and (2) should be placed inside one loop. (3) After all digits are pushed into the vector, pop digits out from the vector one by one. 4) Combine popped digits into an integer using the reversed algorithm of extraction operation in substep (1). Substeps (1) and (2) should be placed inside another loop 5) main() receives the result from the palindrome() function and print it with proper prompt.

Explanation / Answer

#include<iostream>
#include<vector>
using namespace std;
//method that reverse integer using push_back() and
//pop_back() methods if vector,,,
template<typename T>
T palindrome(T n)
{
   vector<T> v;
   int l=0;
   while(n>0)
   {
       int s=n%10;
       v.push_back(s);
       n=n/10;
       l++;  
   }
   int k=0,i=1;//reversing given number
   while(l>0)
   {
       int s=v.back();
       v.pop_back();
       k=k+s*i;
       i=i*10;
       l--;
   }
   return k;//returning reversed number
}


int main()
{
   int n;
   cout<<"Enter a number to check palindrome:";
   cin>>n;
  
   int s=palindrome(n);
  
   cout<<"Reversed :"<<s<<endl;
   if(n==s)
       cout<<" Is palindrome ";
   else cout<<" Not a palindrome ";
  
  
  
   return 0;
}

output:

Enter a number to check palindrome:121
Reversed :121

Is palindrome


Process exited normally.
Press any key to continue . . .