Write a generic function int find_lower_bound(T seq[], int n, const T& value). The function returns the index of the largest element in the given sequence that is less than the given value. If multiple elements satisfy, return the one with smallest index. Return -1 if no such element exists.
//main.cpp
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
#include "source.h"
struct Point{
int x,y;
bool operator<(const Point& p) {
return (x<p.x ||
(x==p.x&&y<p.y));
}
};
int main()
{
//freopen("test01.in", "r", stdin);
//freopen("test01.out", "w", stdout);
{
int A[5] = { 2, 10, 8, 6, 9 };
cout<<find_lower_bound(A, 5,
9)<<endl;
}
{
int A[5] = { 2, 10, 8, 2, 9 };
cout<<find_lower_bound(A, 5,
3)<<endl;
}
{
int A[5] = { 2, 1, 2, 6, 9 };
cout<<find_lower_bound(A, 5,
1)<<endl;
}
double B[5] = { 3.0, 4.5, 6.0, 5.1, 1.3 };
cout<<find_lower_bound(B, 5,
5.2)<<endl;
Point C[5] = {
{1,2},{2,3},{4,5},{1,3},{2,4}};
Point c={3,2};
cout<<find_lower_bound(C,5,c)<<endl;
return 0;
}
int find_lower_bound(T seq[], int n, const T& value)//method
finds largest element less than the value
{
int max,f=0;
for(int i=0;i<n;i++)
{
if(f==0)
{
if(a[i]<value)
{
f=1;
max=a[i];
}
}
else
{
if(max<a[i]
&& a[i]<value)//updating max if less than the
value
max=a[i];
}
}
if(f==0)return -1;
return max;
}
Write a generic function int find_lower_bound(T seq[], int n, const T& value). The function returns the...
(a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....
howthe output of the following 4 program segments (a) const int SIZE=8; int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8}; int index; index=0; res = values[index]; for (int j=1; j<SIZE; j++) { if (values[j] > res) { res = values[j]; index = j; cout << index << res << endl; } } cout <<...
Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...
#include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){ int number; int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl; displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl; return 0; } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...
Who could write the array.cpp file ? //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...
Implement the operator +=, -=, +, -, in the class Date class Date { public: Date(int y=0, int m=1, int d=1); static bool leapyear(int year); int getYear() const; int getMonth() const; int getDay() const; // add any member you need here }; You implementation should enable the usage like this: void f() { Date date = d; cout << "date = " << date << endl; cout << "date+1 = " << date+1 << endl; cout << "date-1 = "...
Start by using the starter code provided for the Line class. It implements operator overloading so you can use cin/cout with line objects. A line will be made up of two points. Create an object to implement a “line” class which allows the programmer to store a line. This class must use the “point” class developed in Part B. The object should have two constructors, appropriate set/get functions, and overloaded I/O (cin/cout) functions. It should include functions the return the...
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...
Write the following function: const int MIN_SIZE = 2; const int MAX_SIZE = 8; const int UNKNOWN = 0; const int RED = 1; const int BLUE = 2; const char UNKNOWN_LETTER = '-'; const char RED_LETTER = 'X'; const char BLUE_LETTER = 'O'; const string UNKNOWN_STRING = "unknown"; const string RED_STRING = "X"; const string BLUE_STRING = "O"; /** * Requires: size <= MAX_SIZE and size is a positive even integer, * 0 <= row && row < size....
vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector { public: Vector( int initsize = 0 ) : theSize( initsize ), theCapacity( initsize + SPARE_CAPACITY ) { objects = new T[ theCapacity ]; } Vector( const Vector & rhs ) : theSize( rhs.theSize), theCapacity( rhs.theCapacity ), objects( 0 ) { objects = new T[ theCapacity ]; for( int k = 0; k < theSize; ++k) objects[ k ] = rhs.objects[ k...