diff --git a/debugging/dividearrays.cpp b/debugging/dividearrays.cpp index a674586d3cf8a98c873b49ccf52db3ba17429ed6..1e133261a9ef6fb84be2d42dbe99efc70e59422a 100644 --- a/debugging/dividearrays.cpp +++ b/debugging/dividearrays.cpp @@ -9,7 +9,14 @@ int main(int argc, char **argv) for (int i=0; i<1024; i++) { int nom = intdist(generator); int denom = intdist(generator); - res[i] = nom / denom; + + res[i] = nom / denom; } return 0; } +/*Program received signal SIGFPE, Arithmetic exception. +0x0000555555554bb9 in main (argc=1, argv=0x7fffffffd688) at dividearrays.cpp:13 +13 res[i] = nom / denom; + +cant divide if denom = 0. Solve by checking if denom == 0.*/ + diff --git a/julia/dowhile.jl b/julia/dowhile.jl index 96d3fb56ba8f599ca9e75f5399d4c2db2a3bf61f..084bb245ba34a416f631755bc19537cf12d36b74 100644 --- a/julia/dowhile.jl +++ b/julia/dowhile.jl @@ -1,10 +1,21 @@ macro doWhile(block, cond) + println("__source__ ", __source__) println("cond ", cond) println("block ", block) res = quote - error("TODO: Your code here") + while true + $(esc(block)) + if $(esc(cond)) + break + end + end end - println(res) # Should not print any lines referencing dowhile.jl - res + + + + # println(res) # Should not print any lines referencing dowhile.jl + res.args[1].striplines + + end diff --git a/julia/run.jl b/julia/run.jl index 3728a8fd904b525f62928f11252efcc8ea4eca79..bf6be0002f996664ca2553513b96ba100d701f9a 100644 --- a/julia/run.jl +++ b/julia/run.jl @@ -1,11 +1,18 @@ """Template programming is a sort of metaprogramming where you fill in the blanks, but most parts of the code are very similar.""" + + for (name,op) in [(:add, :+), (:sub, :-), (:mul, :*)] # Create a function that is named for example add and returns the # sum of its two arguments + @eval $name(x,y) = $op(x,y) + end + + + @assert 3 == add(1, 2) @assert -1.0 == sub(1.0, 2.0) @assert "abc" == mul("ab", "c") @@ -18,7 +25,13 @@ module Problem1 f(a::Int) = 1 f(a::Any) = 2 end + + # TODO: Delete the method f(a::Int) so that f(1) returns the value 2 instead of 1 + +Base.delete_method(which(Problem1.f,(Int,))) + + @assert 2 == Problem1.f(1) # TODO: Make an until block for Julia, similar to do {} while (cond); in C @@ -26,6 +39,9 @@ end # Make sure that any assertions and error-messages refer to lines in the original code. # Hint: You can access a hidden input __source__ in a macro and use that to replace source information. # By having the macro in a separate file, you can check source locations that match dowhile.jl and replace them with __source__. + + + include("dowhile.jl") function testDoWhile() local a = 5 diff --git a/profiling/test.jl b/profiling/test.jl index cdb378af883f1daec6287794c049dde9b9ce8d05..9ac19b19f16b39f154c3800f23077ab3103d6343 100644 --- a/profiling/test.jl +++ b/profiling/test.jl @@ -1,16 +1,23 @@ +using Profile using BenchmarkTools using Random function f() + lst = rand(50000) + for i in lst - s = "List entry $(i)\r" # The \r makes it so the terminal isn't filled + s = "List entry $(i)\r" # The \r makes it so the terminal isn't filled if i > 0.99 + print(s) end end end +@profile f() +Profile.print() + f() @time f() x = @benchmark f()