Two sum: Difference between revisions
Content added Content deleted
VincentARM (talk | contribs) (add task to aarch64 assembly for raspberry 3) |
VincentARM (talk | contribs) (add task to zig language) |
||
Line 3,168: | Line 3,168: | ||
<pre>The numbers with indices 2 and 4 sum to 21 |
<pre>The numbers with indices 2 and 4 sum to 21 |
||
---Program done, press RETURN---</pre> |
---Program done, press RETURN---</pre> |
||
=={{header|zig}}== |
|||
<syntaxhighlight lang="zig"> |
|||
const std = @import("std"); |
|||
const print = @import("std").debug.print; |
|||
const retour = struct { |
|||
ok: bool,index1: usize, index2: usize |
|||
}; |
|||
pub fn main() !void { |
|||
const array1 = [_]usize {0, 2, 11, 19, 90}; |
|||
const Res = searchTwoNumbers(&array1,21); |
|||
if (Res.ok) |
|||
print("Result= [ {},{} ] \n", .{ Res.index1,Res.index2 }) |
|||
else |
|||
print("No find ! \n",.{}); |
|||
} |
|||
//***************************** |
|||
// search two numbers for sum |
|||
//***************************** |
|||
fn searchTwoNumbers(a: []const usize,sum: usize) retour { |
|||
var ret = retour{ |
|||
.ok = false, |
|||
.index1 = 0, |
|||
.index2 = 0, |
|||
}; |
|||
for (a, 0..) |value, i| { |
|||
for (a, 0..) |value1, j| { |
|||
const sum1: usize = value + value1; |
|||
if (sum1 == sum) { |
|||
ret.ok = true; |
|||
ret.index1=i; |
|||
ret.index2=j; |
|||
return ret; |
|||
} |
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
Resultat= [ 1,3 ] |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |