Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

: Write a C++ program to read a list of values from the user and then search for

ID: 3856421 • Letter: #

Question

: Write a C++ program to read a list of values from the user and then search for
a specific value in the list. Your program should display the position of the search value in the list is
the search value in found in the list else should display “search unsuccessful”. Your program should
essentially perform the following tasks.
First your program should ask the user for the number of values in the list.
Next, you should call a function to read the values from the user – you should define a
function for reading the input values - the function should the array to store user input
values as input parameter. The second parameter should be number of values to input. (You
can use the function readArray )
Your program should ask the user for a search value to searched in the input list.
You should call a function to check if the search value is found in the list of values. The
function should display the position of search value in the list if the search is successful else
should print “search unsuccessful”.

Explanation / Answer

#include <iostream>

using namespace std;

void readArray(int p[], int n)
{
   for (int i = 0; i < n; i++)
   {
       cout << "Enter value: ";
       cin >> p[i];
   }
}

void searchArray(int p[], int n, int x)
{
   int index = -1;
   for (int i = 0; i < n; i++)
   {
       if (p[i] == x)
       {
           index = i;
       }
   }

   if (index > -1)
   {
       cout << "Search Successful! Value found in index position: " << index << endl;
   }
   else
   {
       cout << "Search unsuccessful" << endl;
   }
}


int main()
{
   int *p;
   int n, x;

   cout << "Enter the number of values: ";
   cin >> n;

   // create array and take input
   p = new int[n];
   readArray(p, n);

   cout << "Enter the value to search for: ";
   cin >> x;

   searchArray(p, n, x);

   delete [] p;

   return 0;
}

--------------------

OUTPUT:

$ g++ searchValue.cpp
$ ./a.out
Enter the number of values: 5
Enter value: 87
Enter value: 89
Enter value: 69
Enter value: 98
Enter value: 50
Enter the value to search for: 50
Search Successful! Value found in index position: 4

-------

If helpful, do click on the thumbs up button. Thank you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote