Question

Assuming that the array upc declared below has already been filled with MAX_LENGTH values, write a...

Assuming that the array upc declared below has already been filled with MAX_LENGTH values, write a function that will ask to input an integer from the keyboard and perform a sequential search of the array for the input integer. If the input integer is found, print the index of the matching value in the array. Otherwise, print "Not found". Include declarations for all variables that you use.

            const int MAX_LENGTH = 25;

            int upc[MAX_LENGTH];

Please write in C++

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

This problem asks to write a function using c++ programming language. Code is commented properly. In case if you find any difficulties let me know in comment section.

HOPE YOU LIKE THE SOLUTION!!!!!

Function implementation:

void sequential_search(){
int search_ele; // variable to store search element
int n=sizeof(upc)/sizeof(upc[0]); // calculates the size of the array.
cout<<"Enter the search element: ";
cin>>search_ele;
cout<<search_ele<<endl;
// loop iterates over the array and compare each element with the search element and if match is found it prints its index.
for(int i=0;i<n;i++){
if(search_ele==upc[i]){
cout<<"Element found"<<" and index of element is "<<i<<endl;
return;
}
}
// if search element is not found then it prints the message "Not found".
cout<<"Not Found"<<endl;
}

Full program implementation:

#include <iostream>
using namespace std;
const int MAX_LENGTH=25;
int upc[MAX_LENGTH];
void sequential_search(){
int search_ele; // variable to store search element
int n=sizeof(upc)/sizeof(upc[0]); // calculates the size of the array.
cout<<"Enter the search element: ";
cin>>search_ele;
cout<<search_ele<<endl;
/* loop iterates over the array and compare each element with the searchelement and if match is found it prints its index. */
for(int i=0;i<n;i++){
if(search_ele==upc[i]){
cout<<"Element found"<<" and index of element is "<<i<<endl;
return;
}
}
// if search element is not found then it prints the message "Not found".
cout<<"Not Found"<<endl;
}
int main()
{
cout<<"Enter the number of values you want to enter: "<<endl;
int n;
cin>>n; // asks user for the size of array.
cout<<n<<endl;
cout<<"Enter "<<n<<" values"<<endl;
for(int i=0;i<n;i++){
cin>>upc[i];
cout<<upc[i]<<" ";
}
cout<<endl;
sequential_search(); // function call
return 0;
}

Code screenshot:

} نها 1 2 #include <iostream> 3 using namespace std; 4 const int MAX_LENGTH=25; 5 int upc [MAX_LENGTH]; 6- void sequential_se

Output screenshot:

Sg++ -o main.cpp 5 1 2 3 4 5 4 Smain Enter the number of values you want to enter: 5 Enter 5 values 1 2 3 4 5 Enter the searc

Add a comment
Know the answer?
Add Answer to:
Assuming that the array upc declared below has already been filled with MAX_LENGTH values, write a...
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
  • assume that input is an int array. the array is already declared and initialized wnd every...

    assume that input is an int array. the array is already declared and initialized wnd every element is assigned to an integer number. in addition assume that the array contains at least 1 element. write a code that contains one while loop abd no other loops to find both maximum and minimum numbers from input. then outout the maximum minus the minumum to the console window. (using java eclipse coding)

  • In C An array ints of integers has already been declared and initialized. A function printint...

    In C An array ints of integers has already been declared and initialized. A function printint has been defined with the following prototype: void printint(int *); Write code that will call printint with a pointer to the second value in the array ints. Note: you will not need to declare an int variable.

  • Write a C++ function binsearch that carries out the binary search algorithm on a sorted array...

    Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...

  • (a) Write a recursive function int find(const int A[], int n, int x); which returns the...

    (a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....

  • the coding language is in c++ An array named testScores has already been declared. -size= 5...

    the coding language is in c++ An array named testScores has already been declared. -size= 5 -data type = float - the array has already been initialized these values: 77, 88.5, 90, 93, 71.5 -Write one statement to declare a pointer named: ptr - in the same statement, assign the address to the testScores array to the pointer -write one statement to output the memory address of the array element at testScores[0] -Write a for loop to output the values...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • Write a program (a3.py) that has 3 function definitions: magic-sequential(), magic-binary(), and main(). You will write...

    Write a program (a3.py) that has 3 function definitions: magic-sequential(), magic-binary(), and main(). You will write the code for the above functions. Both magic functions (magic-sequential() and magic-binary()) will look for a magic index in a given sorted list of distinct integers. A magic index in a list myList[0 ... n-1] is defined to be an index i such that myList[i] = i . Both functions receive the list as a parameter and return an integer: either the magic index,...

  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • follow the instructions inside. You are to implement the sequential search of array elements. User will...

    follow the instructions inside. You are to implement the sequential search of array elements. User will enter a value (size) which represents the number of values to process The values entered will be stored in an array of type short that has 1000 elements User will enter size numbers The user will enter a search value The program will search the data for a specific value program will display a message in which element the value was found or display...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

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