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...
can someone explain what is going on inside the strcut? c++ struct Pos{ int x; int y; // coordinates Pos(const Pos &p, int dx=0, int dy=0){ *this = p; x+=dx; y+=dy;} Pos(int _x, int _y){ x=_x; y=_y; } //new location bool operator<(const Pos & p) const { return (x < p.x) || (x==p.x && y < p.y); } bool operator==(const Pos & p) const { return x==p.x && y==p.y; } Pos(){x=-1;y=-1;} };
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 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....
Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...
(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....
#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:" <<...
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...
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: "...
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 = "...
#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" <<...