-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_1.cpp
49 lines (45 loc) · 974 Bytes
/
code_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// code_1.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
using namespace std;
int floorSearch(int arr[], int n, int x) {
if (x >= arr[n-1]) {
return n-1;
}
if (x < arr[0]) {
return -1;
}
for (int i=1; i<n; i++) {
if (arr[i] > x) {
return (i-1);
}
}
return -1;
}
int main() {
cout << "\nEnter Size\t:\t";
int n;
cin >> n;
int *arr = new int[n];
cout << "Enter Sorted Array\n";
for ( int i = 0; i < n; i++ ) {
cin >> arr[i];
}
cout << "\nEnter Value to find floor\t:\t";
int x;
cin >> x;
int index = floorSearch(arr, n-1, x);
if (index == -1) {
cout << "\nFloor of " << x << " doesn't exist in array\n";
}
else {
cout << "\nFloor of " << x << "\t:\t" << arr[index] << "\n";
}
delete []arr;
return 0;
}