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

Step 2: In this experiment you will investigate a program that uses a vector. St

ID: 3763032 • Letter: S

Question

Step 2: In this experiment you will investigate a program that uses a vector.

Step 1: In this experiment you will write declarations for different types of vectors.

Enter, save, compile and execute the following program in MSVS. Call the new directory “VectorsExp2” and the program “Vectors2.cpp”.    Answer the questions below:

#include <vector>

#include <iostream>

using namespace std;

class coordinate

{

   public:

      double x,y;

};

int main()

{

      vector<coordinate> moves;

      coordinate C;

      for (unsigned int i=0; i<10; i++)

      {

            C.x = i * 2.386;

            C.y = i * 4.87;

            moves.push_back( C );

      }

      for (unsigned int j=0; j<moves.size( ); j++)

      {

            cout<<"Coordinate in cell "<< j << moves[j].x<<", "<<moves[j].y<<endl;

      }

      // Question 7 code adds here

      moves.clear( );

      return 0;

}

Question 1:Please execute the program in Step 2? Explain any complier messages and the output produced by the program?

Question 2:Please add a “for” or “while” loop to access the elements in the vector using iterator and print out the x, y coordinates.

Your loop should print out the same information as the following code does in the program:

      for (unsigned int j=0; j<moves.size( ); j++)

      {

            cout<<"Coordinate in cell "<< j << moves[j].x<<", "<<moves[j].y<<endl;

      }

Explanation / Answer

#include <vector>

#include <iostream>

using namespace std;

class coordinate

{

public:

double x,y;

};

int main()

{

vector<coordinate> moves;

coordinate C;

  

for (unsigned int i=0; i<10; i++)

{

C.x = i * 2.386;

C.y = i * 4.87;

moves.push_back( C );

}

  

//for (unsigned int j=0; j<moves.size( ); j++)

//{

// cout<<"Coordinate in cell "<< j << moves[j].x<<", "<<moves[j].y<<endl;

//}

  

int j=0;

for (std::vector<coordinate>::iterator it = moves.begin() ; it != moves.end(); ++it) {

cout<<"Coordinate in cell "<< j++ << (*it).x<<", "<<(*it).y<<endl;

}

  

// Question 7 code adds here

  

moves.clear( );

  

return 0;

  

}

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