Pattern Identifier
Make a program that takes a random number and creates an array of size 5 from the multiplication of it.
Then insert a random element that doesn't fit the pattern at a random location.
Make sure to check that the random element you're about to insert isn't the same as the values adjacent to it. This would lead to 2 different inputs yielding the same vector.
Ask the user to select which element is the missfit.
Use std::erase to remove the user selected element.
Using a function, determine if the new array matches the "base" vector (if the correct element was removed).
Lastly tell the user if they removed the right element.
Then output all relevant information (The pattern, an the mutliplied number.)
Your application should include these functions:
std::vector<int> create_random_multiplication_vector() // Creates a random vector of size 5, where each element is some number multiplied by the elements index+1
void print_vector(std::vector<int> vec) // Prints each element of the vector seperated by space
bool matching_adjacent(std::vector<int> vec, int insertion_index, int value) // Returns true if the element holding value, with index insertion_index, would be the same as the previous or next element
Answer
#include <iostream>
#include <vector>
std::vector<int> create_random_multiplication_vector()
{
// Selects a random number from 0 to 99.
int multiplication = rand() % 100;
std::vector<int> vec;
for (int i = 1; i < 6; i++)
{
vec.push_back(i * multiplication);
}
return vec;
}
void print_vector(std::vector<int> vec)
{
for (int i = 0; i < vec.size(); i++)
{
std::cout << vec[i] << " ";
}
std::cout << std::endl;
}
// Used to check if the number that's about to be inserted with have adjecent numbers of the same value
bool matching_adjacent(std::vector<int> vec, int insertion_index, int value)
{
// If the insertion index is 0, then we only need to check the current 0 index element (which will get index 1 when we insert something to index 0).
if (insertion_index == 0)
return vec.at(insertion_index) == value;
// Otherwise we have to check the element before and after the insertion index.
return vec.at(insertion_index - 1) == value || vec.at(insertion_index) == value;
}
int main()
{
srand(time(NULL)); // Initialize random seed so that we can get different random numbers each time from rand().
// This is our baseline vector.
// We will compare the user's modified vector to this one to see if they removed the correct element.
std::vector<int> starting_vec = create_random_multiplication_vector();
std::vector<int> modified_vec = starting_vec;
// Get a random integer that is within the range 0 to highest-index to insert the random element.
int random_index = rand() % starting_vec.size();
// Insert the random number at the random index (and make sure it isn't an already present number).
int random_element;
// Continually generate a random number until it is not the same as the element before or after where it would be inserted.
do
{
random_element = rand() % 100;
} while (matching_adjacent(modified_vec, random_index, random_element));
// Add the random element to the vector.
modified_vec.insert(modified_vec.begin() + random_index, random_element);
// Show the user the pattern.
std::cout << "The pattern is: ";
print_vector(modified_vec);
// Ask the user to select which element is the missfit.
int user_input;
do
{
std::cout << "Which element is the misfit? (1-6): ";
std::cin >> user_input;
} while (user_input < 1 || user_input > 6);
// Remove the user selected element.
modified_vec.erase(modified_vec.begin() + user_input - 1);
// Tell the user if they removed the right element.
if (starting_vec == modified_vec)
{
std::cout << "You removed the right element!" << std::endl;
}
// Tell the user if they removed the wrong element.
else
{
std::cout << "You removed the wrong element!" << std::endl;
std::cout << "Correct vector:" << std::endl;
print_vector(starting_vec);
}
// Show the user the vector they created.
std::cout << "Your vector:" << std::endl;
print_vector(modified_vec);
}