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
  • 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 =...

  • 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...

  • 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....

  • 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),...

  • 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...

  • 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;...

  • 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...

  • Consider the following C++code snippet and what is the output of this program? # include<iostream> using...

    Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

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
ADVERTISEMENT