配列のコピー
明解C言語 入門編 > 5. 配列 >
配列のコピー
C
#include <stdio.h>
int main(int argc, char* argv[])
{
int i;
int va[5] = {1, 2, 3, 4, 5};
int vb[5];for (i = 0; i < 5; i++)
vb[i] = va[i];puts(" va vb");
puts("-------");
for (i = 0; i < 5; i++)
printf("%3d %3d\n", va[i], vb[i]);return 0;
}
実行結果
R:\>lesson033\project1.exe
va vb
-
-
-
-
-
- -
-
-
-
-
1 1
2 2
3 3
4 4
5 5
Delphi
program Project1;{$APPTYPE CONSOLE}
uses
SysUtils;
var
i: Integer;
va: array[1..5] of Integer = (1, 2, 3, 4, 5);
vb: array[1..5] of Integer;
begin
for i := 1 to 5 do
vb[i] := va[i];writeln(' va vb');
writeln('-------');
for i := 1 to 5 do
writeln(format('%3d %3d', [va[i], vb[i]]));
end.
実行結果
S:\>lesson033\project1.exe
va vb
-
-
-
-
-
- -
-
-
-
-
1 1
2 2
3 3
4 4
5 5
Perl
@va = (1, 2, 3, 4, 5); @vb = @va; $vb[4] = 6; print " va vb\n"; print "-------\n"; for (0..4) { printf("%3d %3d\n", $va[$_], $vb[$_]); }
実行結果
L:\>perl lesson_05_033.pl
va vb
-
-
-
-
-
- -
-
-
-
-
1 1
2 2
3 3
4 4
5 6
Ruby
va = [1, 2, 3, 4, 5] vb = va vb[4] = 6 puts " va vb" puts "-------" for i in 0..4 printf("%3d %3d\n", va[i], vb[i]) end puts "" va = [1, 2, 3, 4, 5] vc = [] va.each_with_index do |a, i| vc[i] = a end vc[4] = 7 puts " va vc" puts "-------" va.zip(vc) do |a, c| printf("%3d %3d\n", a, c) end
実行結果
L:\>ruby l:\lesson_05_033.rb
va vb
-
-
-
-
-
- -
-
-
-
-
1 1
2 2
3 3
4 4
6 6
va vc
-
-
-
-
-
- -
-
-
-
-
1 1
2 2
3 3
4 4
5 7