COSC-2436-73426
15/03/2013
Project 2
5.2-2
A)
A : Array[1..n]
I: Integer // Temp variable
X: integer // Random integer between 1 and ‘n’
N:Interget // Size of array A
Count: Integer // counts how many elements has been searched
CheckedA:Array[1..n] // it will keep track of index that was already checked
Function int Random-Search(A,x) //Initialize variables For i := 1 to n CheckedA[i] = false N = A.Lenght // gets A length Count := 0
While (count < n) I := Random(1,n) // uses the library function to get a a random integer between 1 and n //assigns it to I If (not CheckedA[I]) // check if the record has been searched before count++ //increment count by 1 CheckedA[ I] := true // set the flag to show this record has been searched if (A[I] = x) return I // if x is found in A[I] the function returns I and terminates return -1 // if the while loop exit without retuning a value it means the value //was not found on A[1..n], so we return -1 to show that no record was found. B)
Expected number of indices into A is a geometric random variable with expectation of 1/p and p equal to 1/n to be right. So, E[X] = n.
C)
Just like the last problem but not with probability equal k/n
E[X] = 1/p = n/k
D)
In order to the function exit with -1 value in case if no result was found, all the items of CheckedA array must be set to true and the count variable must be equal to the number of elements on A (n). Since I is picked randomly and it can be picked multiple times it would have to be E(X) = nlnn + O(n).
You May Also Find These Documents Helpful
-
| A standard 6-sided die is thrown, and the number of dots on the face that turns up is recorded. Each face has an equal chance of turning up.…
- 1420 Words
- 6 Pages
Satisfactory Essays -
3. Create a binary search function that tells whether a given value exists in the tree or not.…
- 381 Words
- 2 Pages
Good Essays -
the 192 cases in the file. To do this you need to use the file Random Sampler…
- 1201 Words
- 5 Pages
Powerful Essays -
: "); ch = getche(); }while(ch == 'y' || stfor == NULL); } void…
- 401 Words
- 2 Pages
Better Essays -
After the highest possible value for a sequence has been reached, previous values can be reused if the NOCYCLE option has been specified.…
- 4188 Words
- 33 Pages
Satisfactory Essays -
2. Without looking, randomly choose nine candies from each container one at a time. Using the letter symbols, record each candy as it is removed. Record the results in a single line like this: RBGGYYGBB.…
- 482 Words
- 2 Pages
Good Essays -
An accumulator is used to keep a running total of numbers. In a loop, a value is usually added to the current value of the accumulator. If it is not properly initialized, it will not contain the correct total.…
- 457 Words
- 4 Pages
Satisfactory Essays -
val test7 = first_answer (fn x => if x > 3 then SOME x else NONE) [1,2,3,4,5] = 4…
- 374 Words
- 2 Pages
Satisfactory Essays -
The "Monte Carlo Method" is a method of solving problems using statistics. Given the probability, P, that an event will occur in certain conditions, a computer can be used to generate those conditions repeatedly. The number of times the event occurs divided by the number of times the conditions are generated should be approximately equal to P.…
- 732 Words
- 3 Pages
Good Essays -
Var (ri ) = σ i2 = Expected value of [ri − E (ri )]2…
- 585 Words
- 3 Pages
Satisfactory Essays -
| Tests for a true or false condition and then returns one value or another…
- 727 Words
- 3 Pages
Good Essays -
In this case we shall say X is a chi-square random variable with n degrees of freedom and write X ~ (n). Usually n is assumed to be an integer, but we only assume n > 0.…
- 882 Words
- 4 Pages
Satisfactory Essays -
Main program is given below whereas subroutines aregiven after this program ends. inp = randn(1,502); p = [0.26 0.93 0.26]; snr = input('enter snr \n'); x=rand(10,30); for i=1:10 for j=1:30 if x(i,j)>0.5 x(i,j)=1; else x(i,j)=0; end end…
- 819 Words
- 4 Pages
Good Essays -
A warning has been added for 1./x and 1.\x syntax clarification. A new output argument has been added to regexp function to retrieve subpatterns matches.…
- 2424 Words
- 10 Pages
Good Essays -
Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals’ method of the first argument.…
- 314 Words
- 2 Pages
Satisfactory Essays