Given an array of integers. Find a mountain top element in it. An array element
ID: 3819693 • Letter: G
Question
Given an array of integers. Find a mountain top element in it. An array element is moutain top element if it is NOT smaller than its neighbors.ie if an element is greater than its left and right element. For corner elements, we need to consider only one neighbor. For example, for input array {5, 10, 20, 15}, 20 is the only peak element. For input array {10, 25, 15, 7, 36, 75, 53}, there are two mountain top elements: 25 and 75. Note that we need to return any one of the mountain top element .
Following corner cases give better idea about the problem.
If input array is sorted in strictly increasing order, the last element is always a mountain top element. For example, 50 is mountain top element in {10, 20, 30, 40, 50}.
If input array is sorted in strictly decreasing order, the first element is always a mountain top element. 100 is the mountain top element in {100, 80, 60, 50, 20}.
If all elements of input array are same, every element is a mountain top element. So return an element
Your input should have arrays, of each test case described above and should output the value for each of the test case.
This question is based on Inheritance: -
Create a class Book with the following members: Accession number, Name of the author, Title of the book, Year of Publication, Publisher’s Name, Cost of the book and Availability
Create a class Membership with the following members: Member id, Name, Maxbooks Derive a class Library information_system from Book and Member. Include the following member functions:
1. Issue a book after checking member id, book id, Availability of the book and Maxbooks available for the member.
2. Update availability of book and Maxbooks after each transaction.
3. Display a member’s information given member id
-Should paste the screen shot of your input and output.
-Should post 2-3 sentences of approach on problem
Explanation / Answer
#include<iostream>
using namespace std;
class Book
{
int accessionNo;
char author[30];
char title[50];
int yearOfPublication;
char publisherName[30];
float cost;
bool available;
public:
void set(int id,char author[],char title[],int year,char publisher[],float cost,bool available);
void getInfo();
int getID(){return this.accessionNo;}
bool checkAvailability(){return this.available;}
void setAvilability(bool availability){ this.available=availability; }
};
void Book::set(int id,char author[],char title[],int year,char publisher[],float cost,bool available)
{
this.accessionNo=id;
strcpy(this.author,author);
strcpy(this.title,title);
this.yearOfPublication=year;
strcpy(this.publisherName,publisher);
this.cost=cost;
this.available=available;
}
void Book::getInfo()
{
cout<<" Book Details ";
cout<<"Accession number: "<<this.accessionNo;
cout<<" Author name: "<<this.author;
cout<<" Title: "<<this.title;
cout<<" Year of publication: "<<this.yearOfPublication;
cout<<" Publisher name: "<<this.publisherName;
cout<<" Cost: "<<this.cost;
cout<<" Availability: "<<this.available;
}
class Member
{
int memberId;
char name[30];
int maxbooks;
public:
void set(int id,char name[],int maxbooks);
void get();
int getID();
int getMaxbooks(){return this.maxbooks};
void setMaxbooks(){ this.maxbooks++;}
};
void Member::set(int id,char name[],int maxbooks)
{
this.memberId=id;
strcpy(this.name,name);
this.maxbooks=maxbooks;
}
void Member::get()
{
cout<<" Member Details ";
cout<<"Member Id: "<<this.memberId;
cout<<"Name: "<<this.name;
cout<<"Maximum books: "<<this.maxbooks;
}
void Member::getID()
{
return this.memberId;
}
class LibraryInfoSys:public Book,public Member
{
Member members[NO_OF_MEMBERS];
Book books[NO_OF_BOOKS];
public:
bool issueBook(int bookID,int memID);
void displayMember(int id);
void update(int bookID,int memID);
};
void LibraryInfoSys::displayMember(int id)
{
for(int i=0;i<NO_OF_MEMBERS;i++)
{
if(this.members[i].getID()==id)
this.members[i].get();
}
}
bool LibraryInfoSys::issueBook(int bookID,int memID)
{
int flag=0,flag1=0,i,j;
for(i=0;i<NO_OF_BOOKS;i++)
if(this.books[i].getID()==bookID)
if(this.books[i].checkAvailability())
{
flag=1;
break;
}
for(j=0;i<NO_OF_MEMBERS;i++)
if(this.members[j].getID()==memID)
if(this.members[j].getMaxbooks()<=MAX_BOOKS_ALLOTTED)
{
flag1=1;
break;
}
if(flag==1 && flag1==1)
{
this.members[j-1].setMaxbooks();
this.books[i-1].setAvilability(false);
return true;
}
else
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.