We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
Exercises with pair
1. Create and Print a Pair
Write a C++ program that creates a pair of two integers, stores them, and prints both elements. Example Input: (5, 10) Output: First: 5, Second: 10 2. Swap the Values of Two Pairs Write a C++ program that swaps the values of two pairs and prints the result. Example Input: Pair 1: (3, 6) and Pair 2: (7, 2) Output: After swapping, Pair 1: (7, 2), Pair 2: (3, 6) 3. Find the Larger Element in a Pair Write a C++ program that takes a pair of integers and finds the larger element. Example Input: (12, 7) Output: Larger element is 12 4. Create a Pair of Maximum and Minimum Values from Two Numbers Write a C++ program that takes two numbers and creates a pair where the first element is the maximum value and the second element is the minimum value. Example Input: (8, 15) Output: Max: 15, Min: 8 5. Count the Occurrences of a Pair in an Array of Pairs Write a C++ program that counts how many times a specific pair appears in an array of pairs. Example Input: Array of pairs: [(1, 2), (3, 4), (1, 2), (5, 6)], Search pair: (1, 2) Output: The pair (1, 2) appears 2 times. Exercises with vector 1. Sum of Elements in a Vector Write a C++ program that calculates the sum of all elements in a vector of integers. Example Input: {1, 2, 3, 4, 5} Output: Sum of elements: 15 2. Find the Maximum and Minimum Element in a Vector Write a C++ program that finds both the maximum and minimum elements in a vector of integers. Example Input: {10, 20, 5, 100, 25} Output: Maximum: 100, Minimum: 5 3. Reverse a Vector Write a C++ program that reverses the order of elements in a vector. Example Input: {1, 2, 3, 4, 5} Output: {5, 4, 3, 2, 1} 4. Check if a Vector is Sorted in Ascending Order Write a C++ program that checks whether a vector is sorted in ascending order. Example Input: {1, 2, 3, 4, 5} Output: The vector is sorted in ascending order. 5. Remove All Even Numbers from a Vector Write a C++ program that removes all even numbers from a vector and prints the result. Example Input: {1, 2, 3, 4, 5, 6} Output: {1, 3, 5}