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

Rewrite the following function (shown in the text as problem #4 on page 43) to r

ID: 3561658 • Letter: R

Question

Rewrite the following function (shown in the text as problem #4 on page 43) to remove the bad style elements and also incorporate a driver function that creates the array, fills it, queries the user for a target search value, calls the search function, and displays the result. To fill the array you may either hard wire in data elements or allow the user to enter the elements; you can not use a random number generator to fill it.

From text: The following function performs a linear search on a list l of length ll for the item it, returning a 0 or 1 depending on whether it is found. Many principles of good programming are violated. Rewrite the function in an acceptable format.

int LS (int l[ ], int ll, int it)

/* Search for it*/

{int i=0, f=0; A: if (l[i]==it)

goto B; if (i==ll) go to

C;/*Add 1 to i*/ i++; goto A;

B: f=1; C: return F;

}

Explanation / Answer

int LS (int l[ ], int ll, int it){

for(int i=0;i<II;i++)

{

if(I[i]==it)

return 1;

}

return 0;

}

void driverfunc()

{

int length;

cout<<"Enter the length of array";

cin>>length;

int arr[length];

for(int i=0;i<length;i++)

{

cout<<"Enter element "<<i+1<<":-";

cin>>arr[i];

}

cout<<"Enter target value:- ";

int target;

cin>>target;

int result = LS (arr,length, target);

}

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