Welcome to our programming practice lesson! Are you ready for a challenging yet exciting task involving nested loops and arrays? We will be unraveling the skill of using nested loops to search through two arrays. Brace yourself for a remarkable journey of practical learning. Let's get started!
Imagine a scenario where you are given two vectors of integers. Your task is to write a function that retrieves and returns pairs of integers. The first item of the pair will be from the first vector, while the second one will come from the second vector. It's crucial to remember that the first element must be less than the second.
The sequence of pairs in your output should align with the order they appear in the input vectors. For instance, given the vectors {1, 3, 7}
and {2, 8, 9}
, the function should return {{1, 2}, {1, 8}, {1, 9}, {3, 8}, {3, 9}, {7, 8}, {7, 9}}
. It will pose a challenge if no pairs exist, or if any input vector is empty. Let's delve into this task step by step to uncover the solution!
Before venturing into the code, let's decode the problem. Nested looping fits perfectly here.
Start by creating an empty vector named result
to store our pairs.
C++1#include <vector> 2 3std::vector<std::pair<int,int>> solution(std::vector<int> vec1, std::vector<int> vec2) { 4 std::vector<std::pair<int,int>> result;
Creating your function and data structure first is a wise strategy!
Now, the focus turns to forming the nested loops. You need to iterate over both vectors, and for this, you'll need nested loops. An outer loop will select one element from the first vector, and an inner loop will scan through each element of the second vector.
C++1#include <vector> 2 3std::vector<std::pair<int,int>> solution(std::vector<int> vec1, std::vector<int> vec2) { 4 std::vector<std::pair<int,int>> result; 5 for(auto i : vec1){ 6 for(auto j : vec2){ 7 // Our logic goes here 8 } 9 } 10 return result; 11}
In this setup, every element in vec1
is represented by i
, and for each i
, j
represents an element in vec2
.
With our loops ready, it's time to incorporate the logic. We run a check at this point: is the element i
from vec1
less than the element j
from vec2
? If it's true, we insert the pair {i, j}
into our result
vector.
C++1#include <iostream> 2#include <vector> 3 4std::vector<std::pair<int,int>> solution(std::vector<int> vec1, std::vector<int> vec2) { 5 std::vector<std::pair<int,int>> result; 6 for(auto i : vec1){ 7 for(auto j : vec2){ 8 if(i < j){ 9 result.push_back({i, j}); 10 } 11 } 12 } 13 return result; 14} 15 16int main() { 17 std::vector<int> vec1 = {1, 3, 7}, vec2 = {2, 8, 9}; 18 std::vector<std::pair<int, int>> res = solution(vec1, vec2); 19 for (auto i : res) { 20 std::cout << i.first << ' ' << i.second << std::endl; 21 } 22 return 0; 23}
During each execution of our inner loop, we perform this check and store the pairs that are compliant with our condition.
Fantastic job! You have successfully performed a complex task using nested loops to search through two arrays in C++. You now possess the ability to traverse and manipulate two vectors effectively for a given purpose. Keep practicing and continue challenging yourself with more tasks to solidify your understanding. In your upcoming practice sessions, you will come across similar tasks which will further sharpen your programming skills. Remember, practice is the key to mastering any concept. Happy coding!