Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Example program #include #include #include #include using namespace std; vector factor(int n) { vector v1; // Print the number of 2s that divide n

// Example program

#include #include #include #include

using namespace std;

vector factor(int n) { vector v1; // Print the number of 2s that divide n while (n%2 == 0) { printf("%d ", 2); n = n/2; v1.push_back(2); } // n must be odd at this point. So we can skip // one element (Note i = i +2) for (int i = 3; i 2) cout

int maximum(const vector & v) { int max = v[0]; for (int j= 0; j

int main() { int num; vector v1 = {1,2,3}; int max = maximum(v1); cout >num; // call the function for prime factors }

image text in transcribed

Exercise 1: Finding the maximum value (10 points) Provide an implementation of the maximum function whose declaration is shown below. The only argument of the function is a vector v of int. The function returns the largest int that is in the vector v int maximum(const vectorcint> & v) Exercise 2: Appending vectors (10 points) Provide an implementation of the appendVector function whose declaration is shown below. Both arguments of the function are vectors of int. The function should modify vector v by appending to it all the elements of w. For example, if v (4, 2, 5) and w (11, 3), then v will become (4, 2, 5, 11, 3) as a result of calling the function. Hint: the vector class has a function called push back that appends values passed into it. void appendVector (vectorint> & v, const vectorcint> & w); Exercise 4: Prime factors (10 points) Implement a function that takes an integer as its sole argument and returns a vector containing the prime factors of that number. For example, if you pass in 100, then function returns the vector (2, 2, 5, 5). The function signature should look like the following. vectorcint> factor (int n) Exercise 5: Vector insertion (10 points) Suppose a vector v contains a sequence of integers ordered from lowest to highest. For example, v might contain the values 3, 6, 7,12. Write a function called insert that takes this vector v as a irst argument and an integer k as the second argument. The function inserts k into the vector so that it preserves the order of elements. For example, if v were (3, 6, 7, 12) and k were 5, then after calling the function, v would be (3, 5, 6, 7, 12). In your implementation of insert, do not use the insert function of the vector class (v.insert)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

ISBN: 1844804526, 978-1844804528

More Books

Students also viewed these Databases questions