0
0
DSA C++programming~30 mins

Floor and Ceil in Sorted Array in DSA C++ - Build from Scratch

Choose your learning style9 modes available
Floor and Ceil in Sorted Array
📖 Scenario: You are working on a program that helps users find the closest values in a sorted list of numbers. This is useful in many real-life cases, like finding the nearest available price or closest date.
🎯 Goal: Build a program that finds the floor and ceil of a given number in a sorted array. The floor is the greatest number less than or equal to the target, and the ceil is the smallest number greater than or equal to the target.
📋 What You'll Learn
Create a sorted array of integers
Create a target integer to find floor and ceil for
Write logic to find floor and ceil values in the array
Print the floor and ceil values
💡 Why This Matters
🌍 Real World
Finding floor and ceil values is useful in pricing systems, date/time scheduling, and searching nearest values in sorted data.
💼 Career
Understanding floor and ceil concepts helps in algorithm design and optimization tasks common in software development and data analysis roles.
Progress0 / 4 steps
1
Create the sorted array
Create a sorted array of integers called arr with these exact values: 2, 4, 6, 8, 10, 12, 14
DSA C++
Hint

Use C++ array syntax with exact values in ascending order.

2
Set the target number
Create an integer variable called target and set it to 9
DSA C++
Hint

Use a simple integer variable assignment.

3
Find floor and ceil values
Write code to find the floor and ceil of target in arr. Use two integer variables called floor and ceil. Initialize floor to -1 and ceil to -1. Use a for loop with variable i from 0 to 6 to check each element in arr. If arr[i] is less than or equal to target, update floor to arr[i]. If arr[i] is greater than or equal to target and ceil is still -1, update ceil to arr[i].
DSA C++
Hint

Loop through the array and update floor and ceil based on conditions.

4
Print floor and ceil values
Print the floor and ceil values using std::cout in this exact format: Floor: X and Ceil: Y where X and Y are the values of floor and ceil respectively.
DSA C++
Hint

Use std::cout to print the values with the exact text format.