Skip to content
Snippets Groups Projects
Commit 435c0938 authored by simsc266's avatar simsc266
Browse files

commiting 1 and 2

parent 9aff870b
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,14 @@ int main(int argc, char **argv) ...@@ -9,7 +9,14 @@ int main(int argc, char **argv)
for (int i=0; i<1024; i++) { for (int i=0; i<1024; i++) {
int nom = intdist(generator); int nom = intdist(generator);
int denom = intdist(generator); int denom = intdist(generator);
res[i] = nom / denom;
res[i] = nom / denom;
} }
return 0; 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.*/
macro doWhile(block, cond) macro doWhile(block, cond)
println("__source__ ", __source__) println("__source__ ", __source__)
println("cond ", cond) println("cond ", cond)
println("block ", block) println("block ", block)
res = quote res = quote
error("TODO: Your code here") while true
$(esc(block))
if $(esc(cond))
break
end
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 end
"""Template programming is a sort of metaprogramming where you fill """Template programming is a sort of metaprogramming where you fill
in the blanks, but most parts of the code are very similar.""" in the blanks, but most parts of the code are very similar."""
for (name,op) in [(:add, :+), (:sub, :-), (:mul, :*)] for (name,op) in [(:add, :+), (:sub, :-), (:mul, :*)]
# Create a function that is named for example add and returns the # Create a function that is named for example add and returns the
# sum of its two arguments # sum of its two arguments
@eval $name(x,y) = $op(x,y)
end end
@assert 3 == add(1, 2) @assert 3 == add(1, 2)
@assert -1.0 == sub(1.0, 2.0) @assert -1.0 == sub(1.0, 2.0)
@assert "abc" == mul("ab", "c") @assert "abc" == mul("ab", "c")
...@@ -18,7 +25,13 @@ module Problem1 ...@@ -18,7 +25,13 @@ module Problem1
f(a::Int) = 1 f(a::Int) = 1
f(a::Any) = 2 f(a::Any) = 2
end end
# TODO: Delete the method f(a::Int) so that f(1) returns the value 2 instead of 1 # 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) @assert 2 == Problem1.f(1)
# TODO: Make an until block for Julia, similar to do {} while (cond); in C # TODO: Make an until block for Julia, similar to do {} while (cond); in C
...@@ -26,6 +39,9 @@ end ...@@ -26,6 +39,9 @@ end
# Make sure that any assertions and error-messages refer to lines in the original code. # 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. # 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__. # 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") include("dowhile.jl")
function testDoWhile() function testDoWhile()
local a = 5 local a = 5
......
using Profile
using BenchmarkTools using BenchmarkTools
using Random using Random
function f() function f()
lst = rand(50000) lst = rand(50000)
for i in lst 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 if i > 0.99
print(s) print(s)
end end
end end
end end
@profile f()
Profile.print()
f() f()
@time f() @time f()
x = @benchmark f() x = @benchmark f()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment