As you will solve more complex problems, you will find that searching for values in arrays becomes a crucial operation. In this part of the lab, you will input an array from the user, along with a value to search for. Your job is to write a C++ program that will look for the value, using the linear (sequential) search algorithm. In addition, you should show how many steps it took to find (or not find) the value, and indicate whether this was a best or worst case scenario (... remember the lecture). You may refer to the pseudo-code in the lecture, but remember that the details of the output and error conditions are not specified there. Also, note that in pseudo-code arrays are sometimes defined with indices 1 to N, whereas in C++ the indexes span from 0 to N-1.
The program will work as follows. First, you should ask the user to enter the size of the array, by outputting:
"Enter the size of the array: "
to the console. If the user enters an incorrect size, you should output the error message:
"ERROR: you entered an incorrect value for the array size!"
and exit the program. If the input is a valid size for the array, ask the user to enter the data, by outputting
"Enter the numbers in the array, separated by a space, and press enter: "
Let the user enter the numbers of the array and store them into a C++ array. Up to now, this should be exactly the same code as Lab#2. Next, ask the user a value v to search for in the array (called the "key"), by outputting:
"Enter a number to search for in the array: " to the screen, and read the key v.
Search for v by using the linear search algorithm. If your program finds the key, you should output:
"Found value v at index i, which took x checks. "
where i is the index of the array where v is found, and x is the number of search operations taken place.
If you doesn't find the key, then output: "
The value v was not found in the array!"
Then indicate whether you happened to run into a best or worst case scenario. In case you did, output: "
We ran into the best case scenario!" or "We ran into the worst case scenario!"
otherwise, don't output anything.

Answers

Answer 1

Answer:

#include<iostream>

using namespace std;

int main() {

cout<<"Enter The Size Of Array: ";

int size;

bool isBestCase=false;

cin>>size;

if(size<=0){

cout<<"Error: You entered an incorrect value of the array size!"<<endl;

return(0);

}

int array[size], key;

cout<<"Enter the numbers in the array, separated by a space, and press enter:";

// Taking Input In Array

for(int j=0;j<size;j++){

cin>>array[j];

}

//Your Entered Array Is

for(int a=0;a<size;a++){

cout<<"array[ "<<a<<" ] = ";

cout<<array[a]<<endl;

}

cout<<"Enter a number to search for in the array:";

cin>>key;

for(i=0;i<size;i++){

 if(key==array[i]){

   if(i==0){

     isBestCase=true; // best case scenario when key found in 1st iteration

     break;

   }

 }

}

if(i != size){

 cout<<"Found value "<<key<<" at index "<<i<<", which took " <<++i<<" checks."<<endl;

}  else{

 cout<<"The value "<<key<<" was not found in array!"<<endl;

 cout<<"We ran into the worst-case scenario!"; // worst-case scenario when key not found

}

if(isBestCase){

cout<<"We ran into the best case scenario!";

}

return 0;

}

Explanation:

The C++ source dynamically generates an array by prompting the user for the size of the array and fills the array with inputs from the user. A search term is used to determine the best and worst-case scenario of the created array and the index and search time is displayed.


Related Questions

write an algorithm and draw a flowchart to calculate the sum of of the first 10 natural numbers starting from 1​

Answers

Answer:

#include <stdio.h>

void main()

{

int j, sum = 0;

printf("The first 10 natural number is :\n");

for (j = 1; j <= 10; j++)

{

sum = sum + j;

printf("%d ",j);

}

printf("\nThe Sum is : %d\n", sum);

}

Within a loop

Step 1: Initialize a variable to 1 (say x=1)

Step 2: Specifiy the condition

In this case x<=10

Step 3: Increment the variable by 1

using increment operator x++

OR

simply x+=1(x=x+1)

Step 4: Print the value of variable

Terminate the loop

Explanation:

Suppose that an intermixed sequence of push and pop operations are performed on a LIFO stack. The pushes push the numbers 0 through 9 in order; the pops print out the return value. For example"push(0); pop(); push(1)" and "push(0); push(1); pop()" are possible, but "push(1); push(0); pop()" is not possible, because 1 and 0 are out of order. Which of the following output sequence(s) could occur? Select all that are possible.
a) 12543 609 8 7 0
b) 01564 37928
c) 6543210789
d) 02416 75983
e) 46 8 75 32 901

Answers

Answer:

b) 01564 37928

e) 26 8 75 32 901

Explanation:

Pushes and pulls are the computer operations which enable the user to perform numerical tasks. The input commands are interpreted by the computer software and these tasks are converted into numeric values to generate output.


What is the total March Expenditure now? (cell Q2)

Answers

I did a lot to do it lol was

#include
main()
{
int i, n=10, rem, inc=0;
scanf("%d",&n);
for(i=1; i =3)
inc++;
else;
inc--;
}
printf("Final Value=\n",inc);
}
SOLVE THIS PROGRAM...

Answers

Answer:inpossible !889

Explanation: I npossible !889

For which of the following tasks would a for-each loop be appropriate?
A. Reversing the order of the elements in an array.
B. Printing all the elements at even indices of an array.
C. Printing every element in an array.
D. Determining how many elements in an array of doubles are positive.
E. Determining whether an array of Strings is arranged in alphabetical order.
F. Printing every even number in an array of ints.

Answers

Answer:

The answer is "Option B, C,  and F".

Explanation:

In the For-each loop, it is used for traversing items in a sequence is the control flow expression. It typically required a loop becomes referred to it as an enhanced loop for iterating its array as well as the collection elements. This loop is a version shortcut that skips the need for the hasNext() method to get iterator and the loop and over an iterator, and the incorrect choice can be determined as follows:

In choice A, it is wrong because it can't reverse the array element.In choice D, it is wrong because it can't determine the array of positive.In choice E, it is wrong because it can't determine an array of the sting in alphabetical order.
Other Questions
VW = WX = YW = ZX = VX = view image attached thank youuu! God's Wheel by Shel SilversteinGOD says to me with a kindof smile, "Hey how would you liketo be God awhile And steer the world?""Okay," says I, "I'll give it a try.Where do I set?How much do I get?What time is lunch?When can I quit?""Gimme back that wheel," says GOD."I don't think you're quite ready YET."What would be 3 examples of figurative language/poetic devices in this poem? what are the coefficients in the expression 4x+y+6z+12 WILL GIVE BRAINLIST Which of these are rights and freedoms guaranteed by the Georgia Constitution?select the ones that applyfreedom of assemblyright to freedomfreedom of religionright to happinessfreedom of speechright to trial by jury HELP PLEASE!!!See the image! Story #1Once upon a time, there lived a population of mountain people whose members all had wings but differenttypes-either bat wings or bird wings. 40% of the population had bat wings and 60% of the population had birdwings. One day, a storm hit that lasted for several weeks. Those with bat wings were able to fly around andfind food because their wings were water resistant, while those with bird wings were not able to get foodbecause their feathers were not water resistant and got soaked through. Many of the bird-winged people starvedand did not survive, so their alleles decreased in the gene pool, lowering the frequency of the bird wings infuture generations. Over many generations, the environment had more and more long periods of rain and thegenetic makeup of the population shifted to 10% with bird wings and 90% with bat wings.What is the trait being described?What are the variations that exist in this population?3. How did the event lead to the change in allele frequency?Alleles?4. What type of evolutionary mechanism did this story show?5. Was there an adaptation in this story? If so, identify it.6. Did the change make the population more or less similar to other populations?7. Would this increase or decrease reproductive isolation?Increase or decrease speciation?8. Did the new population change for the better, worse, or have no effect? Why? Heat from the sun moves through space by the process ofesA)circulation.B)conductionC)convection.D)radiation Please tell me the answers i am confused and need help Extend the pattern below by four terms write an observation about the pattern pattern 76, 70, 64,58,52 describe some scenes from the day of judgement Which of the following sentences is punctuated correctly?A. We have been studying the moon and it's phases.B. Your science report is much longer than our's.C. Kelly's dog has spots all over its body.D. This globe is your's to keep! The senior class at Clearville ISD is having a fundraiser to raise money for their prom. They are seling coupon books in the local community. The number currently sold by each of the students was used to create the box plot shown below. HHH 8 10 12 14 16 18 20 22 24 26 28 30 32 If one of the students calculated the mean of the data to be 14.5, would this be an appropriate mean? OA. Yes, the mean is appropriate. OB. There is not enough information to determine if the mean is appropriate. O C. No, the mean should be lower. OD. No, the mean should be higher. Reset Submit How Many Equal Parts Are In 4/5 -2y+6=-12 What does y equal? I hope this helps. Good luck HELP ME PLEASEwhich answer is the best example of imagery from crane's "A Great Mistake"?A. elevated trains thundered to the station and the stairway poured people upon the side walks.B. an italian kept a fruit stand on a corner.C. the babe came often to this corner. Kira ,Charlie , and Miguel have a total of $105 in their wallets . Kira has $9 less than Miguel. Charlie has 4 times what Miguel has . How much does each have ? help me plz I dont no 1+1 3x-2y=12 and 3x+5y=33 Never was given anything to help solve they just assume you know it