配列の各要素に先頭から順に 1,2,3,4 を代入して表示 (初期化子の足りない要素)

明解C言語 入門編 > 5. 配列 >

配列の各要素に先頭から順に 1,2,3,4 を代入して表示 (初期化子の足りない要素)

C


#include <stdio.h>
int main(int argc, char* argv[])
{
int i;
int score[5] = {1, 2, 3, 4};

for (i = 0; i < 5; i++)
printf("点数%d = %d\n", i + 1, score[i]);

return 0;
}

実行結果


R:\>lesson032\project1.exe
点数1 = 1
点数2 = 2
点数3 = 3
点数4 = 4
点数5 = 0

Delphi


program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;
var
i: Integer;
score: array[1..5] of Integer = (1, 2, 3, 4, 0);
begin
for i := 1 to 5 do
writeln(format('点数%d = %d', [i, score[i]]));
end.

実行結果


S:\>lesson032\project1.exe
点数1 = 1
点数2 = 2
点数3 = 3
点数4 = 4
点数5 = 0

Perl
@score = (1, 2, 3, 4);

for (0..4)
{
    printf("点数%d = %d\n", $_ + 1, $score[$_]);
}

実行結果

L:\>perl lesson_05_032.pl
点数1 = 1
点数2 = 2
点数3 = 3
点数4 = 4
点数5 = 0

Ruby
score = [1, 2, 3, 4]

for i in 0..4
    printf("点数%d = %d\n", i + 1, score[i])
end

実行結果

L:\>ruby l:\lesson_05_032.rb
点数1 = 1
点数2 = 2
点数3 = 3
点数4 = 4
点数5 = 0