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.

  • (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...

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

  • 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...

  • MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random...

    MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random words from a training set as explained in the lectures on graphs. Do not hard-code the training set! Read it from a file. A suggested format for the input file: 6 a e m r s t 10 ate eat mate meet rate seat stream tame team tear Here are some suggestions for constants, array declarations, and helper functions #include <iostream> #include <fstream> #include...

  • . Assuming that the following integer array ArrayInts [ ] = {54, 99,-8, 5, 124, 29,...

    . Assuming that the following integer array ArrayInts [ ] = {54, 99,-8, 5, 124, 29, -13,260, 29,-4, 100, 12, 15, 58, 164, 480, 60) is stored in the memory. Write a complete C language program that: computes the sum of all even integers in the array and displays the result on the console, computes the sum of all odd integers in the array and displays the result on the console, and Searches the array for the target value of...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

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