Top-Rated Free Essay
Preview

Cosc-2436-73426 Project 2

Satisfactory Essays
306 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Cosc-2436-73426 Project 2
Ronaldo Marques
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

  • Good Essays

    Plant and Diversity Index

    • 482 Words
    • 2 Pages

    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
  • Satisfactory Essays

    06

    • 4188 Words
    • 33 Pages

    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
  • Satisfactory Essays

    The "pseudocode" for such an algorithm is: while the number is bigger than one keep dividing it by two. Additionally keep a count of how many times we do the division.…

    • 391 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Code for an Infinite Loop

    • 457 Words
    • 4 Pages

    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
  • Satisfactory Essays

    Hw3 provided sml file

    • 374 Words
    • 2 Pages

    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
  • Satisfactory Essays

    if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u'){…

    • 277 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Var (ri ) = σ i2 = Expected value of [ri − E (ri )]2…

    • 585 Words
    • 3 Pages
    Satisfactory Essays
  • Good Essays

    Sci Labs Notes

    • 2424 Words
    • 10 Pages

    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
  • Satisfactory Essays

    Thesis on java

    • 314 Words
    • 2 Pages

    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
  • Good 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
  • Good Essays

    end 3) Make a function that calculates RSS for a give vector. function [rss]=problem3(a) rss=sqrt(a*a'); end 4) Make a function that check to see if a number is a prime. function problem4(n) flag=0; for k=[2:1:(n/2)] a=rem(n,k); if a==0 flag=1; end end if flag==1 disp ('Number is not prime'); else disp('Number is prime'); end 5) Calculate a 3-degree nominal to fit the following column.…

    • 1967 Words
    • 8 Pages
    Good Essays
  • Good Essays

    Monte Carlo Simulation

    • 732 Words
    • 3 Pages

    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
  • Good Essays

    For Loop

    • 534 Words
    • 3 Pages

    The condition to test is between the round brackets. We want to keep looping while the variable called loopVal is less than 5. Inside of the curly brackets our code first prints out a line of text. Then we need to increment the loopVal variable. If we don't we'll have an infinite loop, as there is no way for loopVal to get beyond its initial value of 0.…

    • 534 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    Chi-square Distribution

    • 882 Words
    • 4 Pages

    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
  • Good Essays

    | Tests for a true or false condition and then returns one value or another…

    • 727 Words
    • 3 Pages
    Good Essays