Answer to Question #63742 in C++ for William CE Clark

Question #63742
2. Write a printVector function which takes a vector and prints the contents of the vector.
3. Write a main function that create a vector of integers called grades with contents {100, 95, 87, 92, 79}. Then it prompts the user to enter a number and searches the array for that number. If the number is in the vector, it prints out to the user the position it found it at. If the number is not in the vector, it prints out that it did not find it and prints the contents of the vector using printVector.
1
Expert's answer
2016-12-04T03:55:15-0500
#include <vector>
#include <iostream>

void printVector(std::vector<int> input_vector)
{
for (int i = 0; i < input_vector.size(); i++) {
std::cout << input_vector[i] << " ";
}
std::cout << std::endl;
}

int main()
{
std::vector<int> grades = { 100, 95, 87, 92, 79 };
int searching_number;
std::cout << "Please, enter a number to search:\n";
std::cin >> searching_number;
bool in_vector = false;
int i;
for (i = 0; i < grades.size(); i++) {
if (grades[i] == searching_number) { in_vector = true; break; }
}
if (in_vector) std::cout << "Searching number position: " << i << std::endl;
else {
std::cout << "Serching number isn't found.\n";
printVector(grades);
}
system("pause");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS