Pattern Finder
Part of the Fundamentals section of Coddy's C++ journey — lesson 73 of 74.
Challenge
EasyCreate a program that receives two integers that indicate the following size of two arrays of integers as input and determines if the second array appears as a pattern within the first array (in consecutive order).
A pattern exists when all elements of the second array appear together, in the same order, somewhere within the first array, like finding a substring within a string.
For example:
int arr1[] = {1, 2, 3, 4, 5, 6, 7};
int arr2[] = {3, 4, 5};
// Should return trueint arr1[] = {5, 6, 7, 8, 9};
int arr2[] = {6, 8};
// Should return false (not consecutive)Try it yourself
#include <iostream>
#include <vector>
#include <string>
int main() {
int n1;
int n2;
std::cin >> n1;
std::cin >> n2;
std::cin.ignore();
int arr1[n1];
int arr2[n2];
for (int i = 0; i < n1; i++) {
int val;
std::cin >> val;
arr1[i] = val;
}
for (int i = 0; i < n2; i++) {
int val;
std::cin >> val;
arr2[i] = val;
}
// Write your code below using arr1, arr2, n1, n2
return 0;
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison3Variables Part 2
Type DeclarationNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementConditional OperatorRecap - If ElseNested If - Else