Question

Can someone explain how this C++ program runs? A line by line explanation/commentation would be great,...

Can someone explain how this C++ program runs?

A line by line explanation/commentation would be great, as well as the purpose of the program and functions/classes involved.

#include <iostream>

#include <vector>

using namespace std;

// template function

vector<int> removeEvenIndexedVals(vector<int> vec);

// main

int main() {

       static const int arr[] = { 2,5,7,9,1,3,6 };

       vector<int> vec(arr, arr + sizeof(arr) / sizeof(arr[0]));

       // call function

       vec = removeEvenIndexedVals(vec);

       // print

       cout << "Displaying the Vector Elements:" << endl;

       cout << "[ ";

       for (int i = 0; i<vec.size(); i++) {

              cout << vec[i] << " ";

       }

       cout << "]" << endl;

       system("pause");

       return 0;

}

// template function receives vector as a param

// return another vector that hold elements in odd index only

vector<int> removeEvenIndexedVals(vector<int> vec) {

       vector<int> v;

       for (int i = 0; i<vec.size(); i++) {

              if (i % 2 != 0) {

                     v.push_back(vec[i]);

              }

       }

       return v;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <vector>

using namespace std;

// template function

vector<int> removeEvenIndexedVals(vector<int> vec);

// main

int main() {
  
//Creating an array of type int
static const int arr[] = { 2,5,7,9,1,3,6 };

//Creating a Vector and initialize with array elements(arr)
vector<int> vec(arr, arr + sizeof(arr) / sizeof(arr[0]));

// call function

vec = removeEvenIndexedVals(vec);

// Displaying the elements in the vecor

cout << "Displaying the Vector Elements:" << endl;

cout << "[ ";

for (int i = 0; i<vec.size(); i++) {

cout << vec[i] << " ";

}

cout << "]" << endl;

system("pause");

return 0;

}

/* template function receives vector as a param
* This function will removes the even indexed values from
* vector(like 0,2,4,6 indexed values of vector)
* return another vector that hold elements in odd index only
*/

vector<int> removeEvenIndexedVals(vector<int> vec) {
  
//Creating a vector which holds integer values
vector<int> v;

/* Iterating over the Vector which passed as input
* and getting the values in the oodd indexes like (1,2,5...)
* and storing those values into another vector
*/
for (int i = 0; i<vec.size(); i++) {
  
/* Checking whether the index is odd or not
* if not odd discard it.
*/
if (i % 2 != 0) {

v.push_back(vec[i]);

}

}
//Returnig the new vector to the main()
return v;

}

______________

Output:

__________Thank You

Add a comment
Know the answer?
Add Answer to:
Can someone explain how this C++ program runs? A line by line explanation/commentation would be great,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5.35 LAB: Sort a vector Write a program that gets a list of integers from input,...

    5.35 LAB: Sort a vector Write a program that gets a list of integers from input, and outputs the integers in ascending order (lowest to highest). The first integer indicates how many numbers are in the list. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 10 4 39 12 2 the output is: 2 4 10 12 39 For coding simplicity, follow every output value by a space, including the last...

  • Can someone help me solve this problem? Everything works but I keep getting an error "expression...

    Can someone help me solve this problem? Everything works but I keep getting an error "expression must have a constant value". Its the Extended Euclidean Algorithm #include <iostream> using namespace std; #include<vector> void TwoLargest(int a[], int x) {    int largeOne = a[0];    int largeTwo = a[0];    for (int i = 1; i < x; i++)    {        if (a[i] > largeOne)        {            largeTwo = largeOne;            largeOne =...

  • Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an...

    Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Transform the find function of Question 4 into a function template. Here is the program used...

    Transform the find function of Question 4 into a function template. Here is the program used to test your template, followed by the output of that program: #include <iostream> #include <string> #include "find.h" using namespace std; #define NUM_ELEMENTS(a) (sizeof(a) / sizeof(a[0])) int main() {         cout << "int" << endl;         cout << "---" << endl;         int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};         cout << "3 is at location " << find(arr1, NUM_ELEMENTS(arr1),...

  • Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number...

    Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input: 1 15 18 40 30 50 ^D The output should be: -1 2 -1 7 5 -1 int binary_search(vector<int> v, int from, int to, int value) { if (from > to) return -1; int mid = (from + to) / 2;...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • Create a new file (in Dev C++) Modify program above to use a vector instead of...

    Create a new file (in Dev C++) Modify program above to use a vector instead of an array. Header comments must be present Prototypes must be present if functions are used Hello and goodbye messages must be shown Vector(s) must be used for implementation Use comments and good style practices ====================================================================================================== #include <iostream> using namespace std; int main() { cout<<"Welcome! This program will find the minimum and maximum numbers in an array."<<endl; cout<<"You will be asked to enter a number...

  • C++ Create a program that finds the dot product of two vectors. I'm currently trying to...

    C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT