1-D arrays · 1차원 배열
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
DECLARE an array
- An array stores many values under one name.
DECLARE Score : ARRAY[1:5] OF INTEGERmakes room for 5 integers.- The numbers
1:5are the first and last index.
DECLARE Score : ARRAY[1:5] OF INTEGER
Score[1] ← 40
OUTPUT Score[1] // → 40
Arrays count from 1
- The first item is
Score[1], the last isScore[5]. - (This is different from Python and JavaScript, which count from 0.)
DECLARE Name : ARRAY[1:3] OF STRING
Name[1] ← "Ada"
Name[2] ← "Sam"
OUTPUT Name[2] // → Sam
Loop through an array
- A
FORloop visits each index in turn.
DECLARE Score : ARRAY[1:3] OF INTEGER
Score[1] ← 10
Score[2] ← 20
Score[3] ← 30
FOR I ← 1 TO 3
OUTPUT Score[I] // 10, 20, 30
NEXT I
Watch out
- The index must stay inside the declared range:
Score[6]is out of[1:5]. ARRAY[1:5]has 5 cells, numbered 1 to 5 — not 0 to 4.
Common mistakes
- Declare an array with its bounds:
DECLARE A : ARRAY[1:N] OF INTEGER. - Index only within the declared range.
Now you try
- Declare an array, fill it, then read or loop over it.
- Press Check answer to test it.
Declare an array A of 3 integers. Store 10, 20, 30 in positions 1, 2, 3, then output A[2] (the answer is 20). · 정수 3개짜리 배열 A를 Declare합니다. 1, 2, 3번 인덱스에 각각 10, 20, 30을 저장한 후, A[2]을 출력합니다. (정답은 20입니다.)
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
The array A holds 5, 8, 2. Use a FOR loop to add them and output the total (it is 15). · 배열 A에 5, 8, 2이 있습니다. FOR 루프를 사용하여 모두 더한 총합을 출력합니다. (총합은 15입니다.)
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
The array A holds 3, 9, 2, 7. Find the largest value and output it (it is 9). · 배열 A에 3, 9, 2, 7이 있습니다. 가장 큰 값을 찾아서 출력합니다. (그 값은 9입니다.)
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.