<p><span lang=\"EN\"> <p>Please complete the following program utilizing C++ for
ID: 3626248 • Letter: #
Question
<p><span lang="EN"><p>Please complete the following program utilizing C++ format</p>
</span></p>
<h3> Basic vector exercise</h3>
<p>Write a program that reads in 10 integers from the user into a vector and then performs the following actions:</p>
<ul>
<li>Output on one line all 10 integers in the order they were input (forward). </li>
<li>Output on one line all 10 integer in reverse of the order they were input (reverse). </li>
<li>Output on one line only the integers with a value greater than or equal to 10(>= 10). </li>
<li>Output the largest integer (max). </li>
<li>Output the smallest integer (min). </li>
</ul>
<p>For example, if the user enters:</p>
<pre> 11 22 33 4 55 6 77 8 9 10
</pre>
<p>Then you program should output:</p>
<pre>
Forward: 11 22 33 4 55 6 77 8 9 10
Reverse: 10 9 8 77 6 55 4 33 22 11
>= 10: 11 22 33 55 77 10
Max: 77
Min: 4
</pre>
<p>Since this is an exercise for practicing with vectors, you must find the max and min values <strong>AFTER</strong> you read the values into the vector.</p>
Explanation / Answer
Here's the solution to the problem with comments. Please let me know if you have questions or problems.
Cameron
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector <int> numbers; // the vector of ints to hold the data
int i; // needed for the for loops
int max, min; // going to hold the max and min
numbers.resize(10); // resizes the vector to size of 10, with index 0 ... 9
for (i = 0; i < 10; i++) { // this for loop will read in all 10 values for the vector
cin >> numbers[i];
}
// printing the numbers in order
cout << "Forward:";
for (i = 0; i < 10; i++) {
cout << " " << numbers[i];
}
cout << endl;
// end printing the numbers in order
// begin printing the numbers in reverse order
cout << "Reverse:";
for (i = 9; i > -1; i--) {
cout << " " << numbers[i];
}
cout << endl;
// end printing the numbers in reverse order
// begining printing numbers >= 10
cout << ">= 10:";
for (i = 0; i < 10; i++) {
if (numbers[i] >= 10) {
cout << " " << numbers[i];
}
}
cout << endl;
// end printing numbers >= 10
// begin finding the max;
max = numbers[0];
for (i = 1; i < 10; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
cout << "Max: " << max << endl;
// end finding the max
// begin finding the min
min = numbers[0];
for (i = 0; i < 10; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
}
cout << "Min: " << min << endl;
// end finding the min
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.