In: Computer Science
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) { int index = 0; bool found = false; while ((!found) && (index < numberUsed)) if (target == a[index]) found = true; else index++; if (found) return index; else return -1; } int main( ) { int arr[DECLARED_SIZE], listSize, target; fillArray(arr, DECLARED_SIZE, listSize); char ans; int result; do { cout << "Enter a number to search for: "; cin >> target; result = search(arr, listSize, target); if (result == -1) cout << target << " is not on the list.\n"; else cout << target << " is stored in array position " << result << endl << "(Remember: The first position is 0.)\n"; cout << "Search again?(y/n followed by return): "; cin >> ans; } while ((ans != 'n') && (ans != 'N')); cout << "End of program.\n"; return 0; }
////////
Change the code “^^” to OO-style by create a class, named “OneDArray”, with an array of int and numberUsed as data members and the existing function as a method. Add more data members or methods if needed. Simplify the main function as a class driver (the shorter the better).
SOURCE CODE
#include <iostream>
using namespace std;
const int DECLARED_SIZE = 10;
class OneDArray
{
private:
int a[DECLARED_SIZE],
size=DECLARED_SIZE, numberUsed,target,result;
public:
void fillArray();
int search(int target);
void searching();
void display();
};
void OneDArray::fillArray() {
cout << "Enter up to " << size << " nonnegative
whole numbers.\n"
<< "Mark the end of the list with a negative number.\n";
int next, index = 0;
cin >> next;
while ((next >= 0) && (index < size))
{
a[index] = next;
index++;
cin >> next;
}
numberUsed = index;
}
int OneDArray::search(int target)
{
int index = 0;
bool found = false;
while ((!found) && (index < numberUsed))
if (target == a[index])
found = true;
else
index++;
if (found)
return index;
else
return -1;
}
void OneDArray::display()
{
cout<<"Array elements are: ";
for(int i=0;i<numberUsed;i++)
cout<<a[i]<<" ";
}
void OneDArray::searching()
{
char ans;
int result,target;
do
{
cout << "\nEnter a number to search for: ";
cin >> target;
result = this->search(target);
//this pointer holds the current object.Here
this holds a1
if (result == -1)
cout << target << " is not on the list.\n";
else
cout << target << " is stored in array position "
<< result << endl
<< "(Remember: The first position is 0.)\n";
cout << "Search again?(y/n followed by return): ";
cin >> ans;
} while ((ans != 'n') && (ans != 'N'));
cout << "End of program.\n";
}
int main( )
{
OneDArray a1;
a1.fillArray();
a1.display();
a1.searching();
return 0;
}
OUTPUT SCREENSHOT
please give a upvote if u feel helpful.