用 C 語言列印陣列的程式



此程式將指引您瞭解如何在 C 中列印陣列。我們需要宣告並定義一個數組,然後迴圈陣列長度。在每次迭代中,我們將列印一個數組的索引值。我們可以從迭代本身獲取此索引值。

演算法

我們先來看看這個程式的分步步驟:

START
   Step 1 → Take an array A and define its values
   Step 2 → Loop for each value of A
   Step 3 → Display A[n] where n is the value of current iteration
STOP

虛擬碼

我們現在來看看這個演算法的虛擬碼:

procedure print_array(A)

   FOR EACH value in A DO
      DISPLAY A[n]
   END FOR

end procedure

實現

源自上述虛擬碼的實現如下:

#include <stdio.h>

int main() {
   int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
   int loop;

   for(loop = 0; loop < 10; loop++)
      printf("%d ", array[loop]);
      
   return 0;
}

輸出應如下所示:

1 2 3 4 5 6 7 8 9 0
array_examples_in_c.htm
廣告
© . All rights reserved.