Please do this program for C++. Make sure it works. Please separate your project
ID: 3685959 • 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.
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) look similar to this
#include
"Elements.h"
int main() { Elements myElements; myElements.Elements();
myElements.Elements(); }
please do it similar to this format in the testing file
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),
There should only be minimal code in the Test file, just enough
to instantiate an object, and invoke the appropriate
function(s).
Only .cpp and .h files
will be accepted.
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)
Explanation / Answer
/*
* wholenumbersSearch.cpp
*
* Created on: Apr 5, 2016
* Author: Satish-Ravi
*/
#include "wholenumbersSearch.h"
wholenumbersSearch::wholenumbersSearch() {
// TODO Auto-generated constructor stub;
for(int i = 0; i < 6; i++)
arr[i] = -1;
}
void wholenumbersSearch::readNumbers() {
cout << "Please enter 6 numbers in the range [10,101]" <<endl;
int num;
int i = 0;
while(i < 6) {
cout << " Enter a positive number [10,101]" <<endl;
cin >> num;
if(num < 10 || num > 101) {
cout << "Invalid number" <<endl;
}
else
arr[i++] = num;
}
//sort the array
int temp;
for(int i = 1; i < 6; i++)
for(int j = 0; j <= i ; j++) {
if(arr[j] > arr[i]) {
temp = arr[j];
arr[j] =arr[i];
arr[i] = temp;
}
}
}
void wholenumbersSearch::search() {
for(int i = 0; i < 6; i++)
cout << arr[i] << " ";
cout <<endl;
int num = 0;
while(num >= 0) {
cout << "Enter a number to search (Negative number to exit):" <<endl;
cin >> num;
if(num < 0)
break;
//search in the loop
bool found = false;
for(int i = 0; i < 6; i++) {
if(arr[i] == num) {
found = true;
break;
}
}
if(found)
cout <<"Given number #" <<num <<" is found in the array" <<endl;
else
cout <<"Given number #" <<num <<" is not found in the array" <<endl;
}
cout << "GoodBye" <<endl;
}
wholenumbersSearch::~wholenumbersSearch() {
// TODO Auto-generated destructor stub
}
/*
* wholenumbersSearch.h
*
* Created on: Apr 5, 2016
* Author: Satish-Ravi
*/
#ifndef WHOLENUMBERSSEARCH_H_
#define WHOLENUMBERSSEARCH_H_
#include <iostream>
using namespace std;
class wholenumbersSearch {
private:
int arr[6];
public:
wholenumbersSearch();
virtual ~wholenumbersSearch();
void readNumbers();
void search();
};
#endif /* WHOLENUMBERSSEARCH_H_ */
#include "wholenumbersSearch.h"
int main() {
wholenumbersSearch search;//wholenumbersSearch();
search.readNumbers();
search.search();
return 1;
}
----------------output-----------------------------
Please enter 6 numbers in the range [10,101]
Enter a positive number [10,101]
5
Invalid number
Enter a positive number [10,101]
101
Enter a positive number [10,101]
25
Enter a positive number [10,101]
33
Enter a positive number [10,101]
45
Enter a positive number [10,101]
6
Invalid number
Enter a positive number [10,101]
65
Enter a positive number [10,101]
71
25 33 45 65 71 101
Enter a number to search (Negative number to exit):
25
Given number #25 is found in the array
Enter a number to search (Negative number to exit):
35
Given number #35 is not found in the array
Enter a number to search (Negative number to exit):
-1
GoodBye