Given the following algorithm and cost for each line what is

Given the following algorithm and cost for each line, what is the cost of the code for this array: A = {45, 32, 34, 34, 12, 23, 35}, and v = 45?

Solution

The detail explaination is given below in table.

Given array has 7 element so

This statement executes from 0 to 6 index of array

and last time when i condition unsatisfied I.e total cost for this statement is 7+1=8

Therefore total cost of code for given array is,

0+1+8+7+1+1=18 unit

Pseudocode Cost of line cost for given array
FindItem(A,v) 0 0
Index= -1 1 1
For i=0 to A.end 1

Given array has 7 element so

This statement executes from 0 to 6 index of array

and last time when i condition unsatisfied I.e total cost for this statement is 7+1=8

If A[i] == v 1 this statement executes for validating each element of array I.e total cost for this statement is 7
Index=i 1 As V is 45. This statement executes when element in array equal to V. In array only one element is having value 45, therefore cost for this statement is 1
return index 1 this statement executes 1 time has total cost= 1
 Given the following algorithm and cost for each line, what is the cost of the code for this array: A = {45, 32, 34, 34, 12, 23, 35}, and v = 45?SolutionThe det

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site