Question

Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers.

This program has six required outputs.

Start by initializing an array with the following integers, in this order:

23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49.

Your array input may be hardcoded in your program if you wish, or you may input your data from a file.

After initializing your array in main, create a class instance and pass the array to the instance where the unsorted values are displayed. This is required output #1 of 6 for this program. Be sure to label it as such in your program output.

Using a linear, or sequential, search of the unsorted array, determine and report the 1-relative (i.e. 1st, 2nd, 3rd, 4th, etc.) positions of the following numbers in the array (or “not found”), and the number of searches that were required to locate the numbers: 25, 30, 50, 75, and 92. This is required output #2 of 6. Label this output.

Then display the total number of searches for all five numbers (i.e. the number of searches for 25, plus the number of searches for 30,…, plus the number of searches for 92). If a number wasn’t found, then add in the total elements in the array for that search attempt, since they would all have been searched. This is required output #3 of 6. Label this output.

Then sort the numbers using a bubble sort and display the sorted array. This is required output #4 of 6. Label this output.

Next, using a binary search of the sorted array, determine and report the 1-relative positions of the following numbers in the array (or “not found”), and the number of individual searches required to locate the numbers: 25, 30, 50, 75, and 92. This is required output #5 of 6. Label this output.

Finally, display the total number of searches for all five numbers, in the same manner as Step #3. This is required output #6 of 6. Label this output.

Make sure that you capture all six of your required outputs for this program's execution for full credit!

One strategy to develop this program is to first initialize an array with its values in main(). Then create an instance of a class (possibly named “ArrayHolder”) that receives the array of integers in its constructor. Your main() function could then call functions for each of the necessary search, sort, and display operations.

When passing arrays and results back and forth to your class instance you must use pointer variables.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer :

#include <iostream.h>

int arrayCount::search(std::vector<int> &arr, int term)
{
           for (int i = 0; i < arr.size(); ++i)
           { // iterate through array
               if (arr[i] == term)
               { // if term found
return ++i;
               }
           } // return 1-relative index of found term
return -1;
} // if loop doesn't find anything, return -1

void arrayCount::main(std::vector<std::wstring> &args)
{

   std::vector<int> arrayNum = {23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49};
       int numLocation;
       int numSearch = 0;
   std::wstring str = L"Unsorted values: ";
   // step 1
   for (int i = 0; i < arrayNum.size(); ++i)
   {
       str = str + std::wstring(L" ") + std::to_wstring(arrayNum[i]);
   }
   std::wcout << str << std::endl;
   // step 2
   std::vector<int> searches = {25,30,50, 75, 92}; // initialize numbers to search
   for (int i = 0; i < searches.size(); ++i)
   { // iterate through searches array
       numLocation = search(arrayNum, searches[i]);
       std::wcout << std::wstring(L"Search for ") << searches[i] << std::wstring(L": ") << numLocation << std::endl; // print search term and
               if (numLocation >= 0)
               {
                   numSearch = numSearch + numLocation;
               std::wcout << std::wstring(L"Number of searches ") << numLocation << std::endl;
               }
               else
               {
                   numSearch = numSearch + arrayNum.size();
               std::wcout << std::wstring(L"Number of searches ") << arrayNum.size() << std::endl;
               }
   }
               std::wcout << std::wstring(L"Total Number of Searches ") << numSearch << std::endl; // result of search
           Arrays::sort(arrayNum);
           std::wstring two = L"Sorted values: ";
   // step 4
   for (int i = 0; i < arrayNum.size(); ++i)
   {
       two = two + std::wstring(L" ") + std::to_wstring(arrayNum[i]);
   }
   std::wcout << two << std::endl;
       std::vector<int> searchesTwo = {25,30,50,75,92};
       int numsearchTwo = 0;
       for (int i = 0; i < searchesTwo.size(); ++i)
       {
       numLocation = Arrays::binarySearch(arrayNum, searchesTwo[i]);
       numLocation = numLocation + 1;
       if (numLocation >= 0)
       {
           numsearchTwo = numsearchTwo + numLocation;
               std::wcout << std::wstring(L"Search for ") << searchesTwo[i] << std::wstring(L": ") << numLocation << std::endl;
       std::wcout << std::wstring(L"Number of searches ") << numLocation << std::endl;
       }
               else
               {
                   numsearchTwo = numsearchTwo + arrayNum.size();
               std::wcout << std::wstring(L"Search for ") << searchesTwo[i] << std::wstring(L": -1") << std::endl;
               std::wcout << std::wstring(L"Number of searches ") << arrayNum.size() << std::endl;
               }

       }
               std::wcout << std::wstring(L"Total Number of Searches ") << numsearchTwo << std::endl;

}

Add a comment
Know the answer?
Add Answer to:
Write an object-oriented C++ program (i.e. a class and a main function to use it), using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • cis 112 (java reqire to use JGRASP) Searching and Sorting Assignment This is a non-object oriented...

    cis 112 (java reqire to use JGRASP) Searching and Sorting Assignment This is a non-object oriented assignment. This assignment is due at the start of class on July 16, Create a program that populates and array with 100 random integers between 0 and 99. Then print out the array. Then prompt the user for a number, and do a sequential search on the unsorted array and return whether or not the number was in the array. Then sort the array...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • **C++ only, use standard library, no vectors Write a program to generate a list of 5000...

    **C++ only, use standard library, no vectors Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array. Sorts: Print out the middle 50 numbers of the original list to show the results. Now sort the original numbers using bubble sort. Print out the number of swaps made. Now sort the original numbers using selection sort. Print out the number of swaps made. Now sort the original numbers using insertion sort. Print...

  • 1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching...

    1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching (DFS) by stack (define it with class) to traverse the graph. 6 7 2 4 b. If starting with node 2, when node 7 is printed, what numbers are in the stack (for DFS)? Please draw the stack step by step to show how the numbers are pushed into and popped out of it. 2. a. Given a set of integer numbers as int...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT