
Given the C++ code:
// toh.cpp
#include <iostream>
#include <
using namespace std;
int main() {
vector<int> t[3];
int n;
cout << "Enter the number of rings: ";
cin >> n;
int from = 0;
int candidate = 1;
int to, move = 0;
if ( n % 2 == 0)
to = 2; // (from + to)%mod3, counter clockwise
else
to = 1; // (from + to)%mod3, clockwise
for ( int i = n + 1 ; i >= 1; i-- )
t[0].push_back(i);
t[1].push_back(n+1);
t[2].push_back(n+1);
if ( n % 2 == 0 )
{
while ( t[1].size() < (n+1) ) {
cout << "move " << ++move << ":" << " Move candidate " << candidate;
cout << " from tower " << (char) (from + 'A');
cout << " to tower " << (char)(to + 'A') << endl;
t[to].push_back(t[from].back());
t[from].pop_back();
if (t[(to+2)%3].back() < t[(to+1)%3].back())
from = (to + 2)%3;
else
from = (to + 1)%3;
if (t[(from)%3].back() < t[(from+2)%3].back())
to = (from + 2)%3;
else
to = (from + 1)%3;
candidate = t[from].back();
}
return 0;
} else {
while ( t[1].size() < ( n+1 ) ) {
cout << "move " << ++move << ":" << " Move candidate " << candidate;
cout << " from tower " << (char) (from + 'A');
cout << " to tower " << (char)(to + 'A') << endl;
t[to].push_back(t[from].back());
t[from].pop_back();
if (t[(to+1)%3].back() < t[(to+2)%3].back())
from = ( to + 1 )%3;
else
from = ( to + 2 )%3;
//cout << "from = " << from << endl;
if (t[(from)%3].back() < t[(from+1)%3].back())
to = (from + 1)%3;
else
to = (from + 2)%3;
//cout << "to = " << to <<endl;
candidate = t[from].back();
}
return 0;
}
return 0;
}
Given the while loop:
while ( t[1].size() < ( n+1 ) ) {
cout << "move " << ++move << ":" << " Move candidate " << candidate;
cout << " from tower " << (char) (from + 'A');
cout << " to tower " << (char)(to + 'A') << endl;
t[to].push_back(t[from].back());
t[from].pop_back();
if (t[(to+1)%3].back() < t[(to+2)%3].back())
from = ( to + 1 )%3;
else
from = ( to + 2 )%3;
//cout << "from = " << from << endl;
if (t[(from)%3].back() < t[(from+1)%3].back())
to = (from + 1)%3;
else
to = (from + 2)%3;
//cout << "to = " << to <<endl;
candidate = t[from].back();
}
return 0;
Question: Explain what each instruction is doing.

Step by stepSolved in 2 steps

- C++ need help with part d to g only Assume the Product structure is declared as follows: struct Product { string description; // Product description int partNum; // Part number double cost; // Product cost }; (c) Declare a dynamic array of Products of size 5 and name it ”items”. Then initilize it with user input values. (d) Write a print function (not as a member of the struct) and pass a pointer to the pointer that points to the array (double pointer) and print all the items of the array in that function. (e) Define a max function (not as a member of the struct) that gets an array of items as an input and returns a pointer to the max element of the array. (f) Declare a 3 by 3 two dimensional dynamic array and populate it. (g) Define an output function that takes a stream object and a pointer to a 2D array as arguments and outputs data members of objects in format of 3*3 table into the given stream. Test your function both with an output file…arrow_forward//the language is c++ Implement function to tokenize a string. std::vector<std::string> tokenize(std::string& line) { } int main(){ std::string ts = "This is a string with no commas semicolons or fullstops"; std::vector<std::string> tok = tokenize(ts); for (auto x : tok) { std::cout<<x<<std::endl; } return 0; }arrow_forwardUsing C++ Assume proper includes have been executed, but not using directive or declaration. Write a definition of an iterator for a vector named vec of int values. Write a for loop that will display the contents vec on the screen, separated by spaces. Use iterators for the loop control.arrow_forward
- C++ Given code is #pragma once#include <iostream>#include "ourvector.h"using namespace std; ourvector<int> intersect(ourvector<int> &v1, ourvector<int> &v2) { // TO DO: write this function return {};}arrow_forward>> IN C PROGRAMMING LANGUAGE ONLY << COPY OF DEFAULT CODE, ADD SOLUTION INTO CODE IN C #include <stdio.h>#include <stdlib.h>#include <string.h> #include "GVDie.h" int RollSpecificNumber(GVDie die, int num, int goal) {/* Type your code here. */} int main() {GVDie die = InitGVDie(); // Create a GVDie variabledie = SetSeed(15, die); // Set the GVDie variable with seed value 15int num;int goal;int rolls; scanf("%d", &num);scanf("%d", &goal);rolls = RollSpecificNumber(die, num, goal); // Should return the number of rolls to reach total.printf("It took %d rolls to get a \"%d\" %d times.\n", rolls, num, goal); return 0;}arrow_forwardC++ programming task. main.cc file: #include <iostream>#include <map>#include <vector> #include "plane.h" int main() { std::vector<double> weights{3.2, 4.7, 2.1, 5.5, 9.8, 7.4, 1.6, 9.3}; std::cout << "Printing out all the weights: " << std::endl; // ======= YOUR CODE HERE ======== // 1. Using an iterator, print out all the elements in // the weights vector on one line, separated by spaces. // Hint: see the README for the for loop syntax using // an iterator, .begin(), and .end() // ========================== std::cout << std::endl; std::map<std::string, std::string> abbrevs{{"AL", "Alabama"}, {"CA", "California"}, {"GA", "Georgia"}, {"TX", "Texas"}}; std::map<std::string, double> populations{ {"CA", 39.2}, {"GA", 10.8}, {"AL", 5.1}, {"TX", 29.5}}; std::cout <<…arrow_forward
- this C++ code is pre-existing and cannot be changed or modified. for it to work, the program has to use this information. #include <iostream>#include <vector>using namespace std; int main() { const int NUM_VALS = 4; vector<int> courseGrades(NUM_VALS); int i; for (i = 0; i < courseGrades.size(); ++i) { cin >> courseGrades.at(i); } //enter code here// return 0; }arrow_forwardUsing C++ Programming language: Assume you define a vector in the following way: vector<int> vec; Assign the value 10 to the first element of this vector. What is the statement you would use?arrow_forward#include <bits/stdc++.h>using namespace std;int main() { double matrix[4][3]={{2.5,3.2,6.0},{5.5, 7.5, 12.6},{11.25, 16.85, 13.45},{8.75, 35.65, 19.45}}; cout<<"Input no in first row of matrix"<<endl; for(int i=0;i<3;i++){ double t; cin>>t; matrix[0][i]=t; } cout<<"Contents of the last column in matrix"<<endl; for(int i=0;i<4;i++){ cout<<matrix[i][2]<<" "; } cout<<"Content of first row and last column element in matrix is: "<<matrix[0][3]<<endl; matrix[3][2]=13.6; cout<<"Updated matrix is :"<<endl; for(int i=0;i<4;i++){ for(int j=0;j<3;j++){ cout<<matrix[i][j]<<" "; }cout<<endl; } return 0;} Please explain this codearrow_forward
- C++arrow_forwardCreate a flowchart for this program in c++, #include <iostream>#include <vector> // for vectors#include <algorithm>#include <cmath> // math for function like pow ,sin, log#include <numeric>using std::vector;using namespace std;int main(){ vector <float> x, y;//vector x for x and y for y float x_tmp = -2.5; // initial value of x float my_function(float x); while (x_tmp <= 2.5) // the last value of x { x.push_back(x_tmp); y.push_back(my_function(x_tmp)); // calculate function's value for given x x_tmp += 1;// add step } cout << "my name's khaled , my variant is 21 ," << " my function is y = 0.05 * x^3 + 6sin(3x) + 4 " << endl; cout << "x\t"; for (auto x_tmp1 : x) cout << '\t' << x_tmp1;//printing x values with tops cout << endl; cout << "y\t"; for (auto y_tmp1 : y) cout << '\t' << y_tmp1;//printing y values with tops…arrow_forwardComplete the C++ code below#include "std_lib_facilities.h"/*1. Define a function which calculates the outer product of two vectors. The function return is a matrix. */vector<vector<int>> outerProduct(vector<int>& A, vector<int>& B){//implement here}/*2. Define a function which transposes a matrix. */vector<vector<int>> transpose(vector<vector<int>>& A){//implementation}/*3. Define a function which calculates the multiplication of a matrix and it's transpose. */vector<vector<int>> product(vector<vector<int>>& A){// implementation}/*4. Define a print out function that will print out a matrix in the following format:Example: a 3 by 4 matrix should have the print out:1 2 3 42 3 4 53 4 5 6*/void printMatrix(vector<vector<int>>& A){// implementation}int main(){// Define both vector A and vector Bvector<int> A = {1 ,2, 3, 4, 5};vector<int> B = {1, 2, 3};/*Test outerProduct…arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





