Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Merge to number

$
0
0

hi all
i have two arrays a and b
find the maximum between a[i] and b[i] and return it
if i have a = [1,5,9,2] and b =[3,3,7,8,6,4]
the output will be
res = [3,5,9,8,6,4]
it is easy i know but i don’t know why my code not 100% working.

it is not issue of julia forgive me but if some one can fix it i appreciate that

thanks

this is my code…

function MaxPair(a,b)
   
    if length(a) >= length(b)
        c = length(a)
        sh = length(b)
    else  
        c = length(b)
        sh = length(a)
    end
    println(c , sh)
    res = Array{Int64}(undef,c)
    i=1
    while i<=sh
        if a[i] >= b[i]
            res[i] = a[i]
            i+=1
        else
            res[i] = b[i]
            i +=1
        end
    end
    if length(a) >= length(b)
        while sh<=length(a)
            res[sh] = a[sh]
            sh+=1
        end
    end
    if length(b) >= length(a) 
        while sh <= length(b)
            res[sh] = b[sh]
            sh+=1
        end
    end
    return res
end
a = [1,5,880,9999]
b = [3,2,9,99999]
println(MaxPair(a,b))

5 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles