Please do this program for C++. Make sure it works. Please separate your project
ID: 3685116 • Letter: P
Question
Please do this program for C++. Make sure it works. Please separate your project into 3 files. interface.h , implementation.cpp and test.cpp.
CS 1119-91 C++ Programming Project 9
Use a one-dimensional six-element vector or array to solve the
following problem.
Read in whole numbers, each of which must be from 10 to 101,
inclusive. As each number is read, store it in the vector only if
it is between 10 and 101, inclusive, and it isn’t a duplicate of a
number already stored in the vector. If an out of range number
is supplied, display it, reject it, and prompt the user to enter a
different number. Stop accepting input only when all six vector
positions have been filled, and then display all six unique
numbers stored in the vector, sorted in ascending order.
After that, prompt the user for one number at a time, and use
a built-in search function to tell the user whether or not the
number that was entered is present in the structure. End the
program, and display “Goodbye” when the user enters a
negative integer.
As usual, your solution must be separated into, at least, three
files (Interface,Implementation and Test files), and the
solution you submit must be your own original work.
There should only be minimal code in the Test file, just enough
to instantiate an object, and invoke the appropriate
function(s).
Submit your source files to the dropbox no later than the
deadline (11:59 pm on Wednesday, April 4th
, 2016). Late
submission penalty: 50 points per day. Only .cpp and .h files
will be accepted. If you submit other types of files, they will be
rejected, and you’ll receive no points for the project.
Please don't include all your codes in the test file There should only be minimal code in the Test file, just enough
to instantiate an object, and invoke the appropriate
function(s)
Grading Criteria:
Explanation / Answer
Elements.h
#include <vector>
using namespace std;
class Elements
{
public:
void addElement(int element);
bool searchElement(int element);
int getSize();
void display();
private:
vector<int> numbers;
};
Elements.cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include "Elements.h"
using namespace std;
void Elements::addElement(int element)
{
//checking if the number is within the range
if(element<10 || element>110)
{
cout<<element<<endl;
cout<<"Number is out of range"<<endl;
cout<<"Enter a number with in the range 10 and 110 inclusive"<<endl;
return;
}
//checking if number is already present in the vector
if(searchElement(element))
{
cout<<element<<endl;
cout<<"Enter a distinct number,this is a duplicate number already present in the vector"<<endl;
return;
}
numbers.push_back(element);
}
bool Elements::searchElement(int element)
{
//iterating over the vector and searching for the input number
for(vector<int>::iterator it=numbers.begin();it!=numbers.end();++it)
{
if(element==*it)
{
return true;
}
}
return false;
}
void Elements::display()
{
//sorting the numbers before displaying
sort(numbers.begin(),numbers.end());
for(int i=0;i<numbers.size();++i)
{
cout<<numbers[i]<<endl;
}
}
int Elements::getSize()
{
return numbers.size();
}
Test.cpp
#include <iostream>
#include "Elements.h"
using namespace std;
int main()
{
Elements e;
int element;
while(e.getSize()<6)
{
cout<<"Enter a element"<<endl;
cin>>element;
e.addElement(element);
}
//six elements have been entered into the vector
//displaying the sortd vector in ascending order
e.display();
int s;
while(true)
{
cout<<"Enter the number you want to search"<<endl;
cin>>s;
if(s<0)
{
break;
}
if(e.searchElement(s))
{
cout<<"Number is present in the vector"<<endl;
}
else
{
cout<<"Number not found in the vector"<<endl;
}
}
cout<<"Goodbye"<<endl;
return 0;
}
Sample output:
Enter a element
11
Enter a element
-1
-1
Number is out of range
Enter a number with in the range 10 and 110 inclusive
Enter a element
23
Enter a element
45
Enter a element
67
Enter a element
111
111
Number is out of range
Enter a number with in the range 10 and 110 inclusive
Enter a element
32
Enter a element
34
11
23
32
34
45
67
Enter the number you want to search
34
Number is present in the vector
Enter the number you want to search
43
Number not found in the vector
Enter the number you want to search
-12
Goodbye