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

C++ program used to swap the values about vector. Please fix the problem. Please

ID: 3940028 • Letter: C

Question

C++ program used to swap the values about vector. Please fix the problem. Please highlight the code which you rewrite.

This the error message.

lab8.cpp: In function ‘void printVec(const std::vector<T>&)’:
lab8.cpp:15:16: error: ‘iter’ does not name a type
for ( auto iter = vec.gegin();iter != vec.end() ; iter++ ){
^
lab8.cpp:15:35: error: expected ‘;’ before ‘iter’
for ( auto iter = vec.gegin();iter != vec.end() ; iter++ ){

#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

//Function that prints vector elements
template<class T>
void printVec( const vector<T> & vec){
//Initializing iterator
   typename vector<T>::iterator iter ;

   for ( auto iter = vec.gegin();iter != vec.end() ; iter++ ){
  
        cout << *iter << endl;
   }
}

//Function that shuffles vector elements
template<class B>
void shuffleVec(vector<B> & vec)
{

   int len = vec.size();
   int i, rPos;

    for (i=len-1; i>=0; i--){
       rPos = rand()%len;
       swap(vec.at(i), vec.at(rPos));
   }
}

//Main function
int main()
{
   //Initializing random function
   srand (time(NULL));

   cout<<" Testing with double values: ";

   //GenericVector object of type double
   vector<double> doubleVec;

   //Inserting values into vector
   doubleVec.push_back(23.64);
   doubleVec.push_back(18.41);
   doubleVec.push_back(14.33);
   doubleVec.push_back(85.18);
   doubleVec.push_back(10.90);

   //Printing vector elements
   printVec(doubleVec);
  
   cout<<" Testing with string values: ";
  
   //GenericVector object of type string
   vector<string> stringVec;
  
   //Insert three double values into vector
   stringVec.push_back("Anil");
   stringVec.push_back("Manu");
   stringVec.push_back("Varnitha");
   stringVec.push_back("Sanjana");
   stringVec.push_back("Tanvi");

   //Printing vector elements
   printVec(stringVec);

   cout << " String vector - After shuffle 1: ";

   //Shuffling elements
   shuffleVec(stringVec);

   //Printing vector elements
   printVec(stringVec);

   cout << " String vector - After shuffle 2: ";

   //Shuffling elements
   shuffleVec(stringVec);
  
   //Printing vector elements
   printVec(stringVec);

   cout << " ";
return 0;
}

Explanation / Answer

There is only one mistake in the code.

In the method printVec, it needs to be vec.begin(). I guess that's just the typo.

Otherwise, the code is fine.

You are getting that error because you are not using c++ 11. To use the c++11 library, you need to compile it by typing in the terminal, g++ -std=c++11 filename -o output.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote