Question

In: Computer Science

#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...

#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).

Solutions

Expert Solution

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.


Related Solutions

#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)        cout << x << " ";    cout << endl; } void println(const vector<string>& v) {    for (const string& x : v)        cout << " "<< x << " ";    cout << endl; } int main() {    vector<int> v0;    cout << "An empty vector of integers: ";    println(v0);    vector<int> v1(3);    cout << "A...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
#include<iostream> using namespace std; void calcSumAndDiff(int ,int, int &, int &); void calcSumAndDiff(int n1,int n2, int...
#include<iostream> using namespace std; void calcSumAndDiff(int ,int, int &, int &); void calcSumAndDiff(int n1,int n2, int &sum, int &diff){ sum = n1 + n2; diff = n1 - n2; } int main() { int n1,n2,sum,diff; n1=30;n2=10; calcSumAndDiff(n1,n2,sum,diff); cout<<"Sum is :"<<sum<<endl; cout<<"Diff is:"<<diff<<endl; system("pause"); }
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int x, int y, int z); void add (int arr [10], int first, int last); void print (int arr [10]); int main () {     int my_arr [10];         cout << "The original array is:\n";     print (my_arr);         start (my_arr);     cout << "\n\nThe array after start is:\n";     print (my_arr);         move (my_arr, 2, 4, 6);     cout << "\n\nThe...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4]...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4] = { begin[0], begin[1], begin[2], begin[3] }; add: goto print; carry: if ( current[index] < end[index] - 1 ) { current[index]++; goto add; } else if ( index > 0 ) { current[index] = begin[index]; index--; goto carry; } else return; print: cout << current[0] << current[1] << current[2] << current[3] << endl; index = 3; goto carry; } int main( ) { int...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){ if(n==0) return; cout<<arr[n-1]<<" "; print(arr,n-1); } int main() { int n; cout<<"Enter the size of the array:"; cin>>n; int arr[n]; int i; cout<<"Enter the elements of the array:"<<endl; for(i=0;i<n;i++){ cin >> arr[i]; } cout<<"Displaying the elements of the array in reverse order:"<<endl; print(arr,n); return 0; } b) #include<iostream> #include<fstream> using namespace std; //student structure struct student { int id; float gpa; }; //node structure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT