Question

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 vof int. The function returns the largest int that is in the vectorv int maximum(const vector<int & 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 vby 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ívector<int> & v, const vector<int> & 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. vector<int> factor(int n) Exercise 5: Vector insertion (10 points) Suppose a vector vcontains a sequence of integers ordered from lowest to highest. For example, vmight contain the values 3, 6, 7, 12. Write a function called insert that takes this vector vas a first argument and an integer kas the second argument. The function inserts kinto the vector so that it preserves the order of elements. For example, if vwere (3, 6, 7, 12) and kwere 5, then after calling the function, vwould be (3, 5, 6, 7, 12). In your implementation of insert, do not use the insert function of the vector class (v.insert).

Example program

#include <string>
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

vector<int> factor(int n)
{
    vector <int> 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 <= sqrt(n); i = i+2)
    {
        // While i divides n, print i and divide n
        while (n%i == 0)
        {
            cout << i << " ";
            //push something
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        cout << n << " ";
        //push something
    
    return v;
   
}


int maximum(const vector<int> & v)
{
int max = v[0];
for (int j= 0; j<v.size() ; j++)
{
    if (v[j]<max) max = v[j];  
}
return max;
}

int main()
{
int num;
vector <int> v1 = {1,2,3};
int max = maximum(v1);
cout << "Max is: " << max;
cout << "Please enter a number: "<<endl;
cin >>num;
// call the function for prime factors



}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Flow chart of this program #include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0,...

    Flow chart of this program #include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0, ave-ee, int locmx, locmn int n; cout <<"Enter the number of students: "<<endl; cin >> ni double listln]; double max-0; double min-e; for (int i-e ; i<n ;i++) cout<s enter the gpa: "<cendli cin>>listli]; while (listlile i listli1>4) cout<s error,Try again "<cendl; cin>listlil: sun+=list[i]; N1 if (listli]>max) for(int isin itt) max=list [i]; 10cmx=1+1 ; else if (list [i] min) min=list [i]; locmn-i+1; ave sum/n;...

  • Need a FLOW Chart for that code. #include <iostream > using namespace std; int PowerFive(int); //Function...

    Need a FLOW Chart for that code. #include <iostream > using namespace std; int PowerFive(int); //Function prototype declaration int main() for (int i=-10 ; i(z10; 1++) {//for each # cout<<"("<< ǐ<<") ^5. "<<PowerFive(1)くくendl;// calling the defined Function int PowerFive (int a) //Function definition return a*a*a*a*a;

  • #include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS =...

    #include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS = 8; vector <double> inverse(NUM_ITEMS); int j; double temp; for (int i = 0; i < NUM_ITEMS; i++) { inverse.at(i) = 1 / (i + 1.0); } cout << fixed << setprecision(2); cout << "Original vector..." << endl; for (int i = 0; i < NUM_ITEMS; i++) { cout << inverse.at(i) << " "; } cout << endl; cout << "Reversed vector..." << endl; for...

  • Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std;...

    Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std; /* * Calculate the square of a number */ float square (float x) { return x*x; } /* * Calculate the average from a list of floating point numbers */ float average(vector<float>& values) { float sum = 0.0f; float average; for (float x : values) sum += x; average = sum / values.size(); return average; } /** Calculate the standard deviation from a vector...

  • #include <iostream> using namespace std; int main() {    int i,n;    int counter=0;    cin...

    #include <iostream> using namespace std; int main() {    int i,n;    int counter=0;    cin >> n;    for (i = 1; i <= n; i++)    {        if (n % i == 0 )        {            counter++;        }        /*else        {            counter++;            i++;        }*/           }    if (counter == 2)        cout << n <<...

  • #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const;...

    #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const; protected: int distance; }; Creature::Creature(): distance(10) {} void Creature::run() const { cout << "running " << distance << " meters!\n"; } class Wizard : public Creature { public: Wizard(); void hover() const; private: int distFactor; }; Wizard::Wizard() : distFactor(3) {} void Wizard::hover() const { cout << "hovering " << (distFactor * distance) << " meters!\n"; } //Created new derived class from Creature class Widget...

  • C++ Standard Deviation with arrays and vectors: #include <vector> #include <cmath> #include <iostream> using namespace std;...

    C++ Standard Deviation with arrays and vectors: #include <vector> #include <cmath> #include <iostream> using namespace std; void fillVector(vector <double> &); double average(const vector <double> &); int main() { return 0; } void fillVector(vector <double> &v) { double d; while (cin >> d) { v.push_back(d); } } double average(const vector <double> &v) { double sum = 0.; for (int i = 0; i < v.size(); i++) { sum += v.at(i); } return sum / v.size(); } standardDeviation() //stuck on this part...

  • #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { int...

    #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { int N = nums.size(); vector<int> result; vector<int> a(101); vector<int> b(101); for (int i = 0; i < N; i++) { a[nums[i]]++; // what does this mean? } for (int i = 1; i < 101; i++) { b[i] = a[i - 1] + b[i - 1]; } for (int i = 0; i < N; i++) { result.push_back(b[nums[i]]); } for (int i = 0; i...

  • #include "stdafx.h" #include <iostream> #include <vector> #include <cassert> using namespace std; // function prototypes int loadDisk(vector<int>...

    #include "stdafx.h" #include <iostream> #include <vector> #include <cassert> using namespace std; // function prototypes int loadDisk(vector<int> &firstPeg, int numDisks); void printPeg(vector<int> &peg); int hanoi(struct pegType &startPeg, struct pegType &swapPeg, struct pegType &endPeg, int numDisk); void moveDisk(struct pegType &startPeg, struct pegType &endPeg); struct pegType {    vector <int> diskStack;    int name; }; int main() {    int numDisk = 7;    int i;    pegType peg1, peg2, peg3;    peg1.name = 1;    peg2.name = 2;    peg3.name = 3;...

  • #include <iostream> #include <sstream> #include <string> using namespace std; int main() {    const int index...

    #include <iostream> #include <sstream> #include <string> using namespace std; int main() {    const int index = 5;    int head = 0;    string s[index];    int flag = 1;    int choice;    while (flag)    {        cout << "\n1. Add an Item in the Chores List.";        cout << "\n2. How many Chores are in the list.";        cout << "\n3. Show the list of Chores.";        cout << "\n4. Delete an...

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