Question

C++ question. Introduce a vector of integers that return the biggest value of that vector.

C++ question.

Introduce a vector of integers that return the biggest value of that vector.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <vector>

using namespace std;

int biggest(vector<int> v) {
    int max = v[0]; // set first element of vector as maximum element
    for (int i = 0; i < v.size(); ++i) {    // then, go through all elements of vector
        if (v[i] > max) {   // if element is greater than the maximum element found till now
            max = v[i]; // then update max
        }
    }
    return max; // finally return the maximum element found in the vector
}

void printVector(vector<int> v) {
    cout << "Vector is ";
    for (size_t i = 0; i < v.size(); i++) { // go through all elements of vector
        cout << v[i] << " ";    // print element
    }
    cout << endl;   // finally print a new line
}

int main() {
    vector<int> v = {1, 4, 9, 16, 9, 7, 4, 9, 11};  // create a vector
    printVector(v); // print the vector
    cout << "Largest value in vector is " << biggest(v) << endl;    // find and print biggest element in vector
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ question. Introduce a vector of integers that return the biggest value of that vector.
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
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