目录

程序打印数组(Program to print an array)

这个程序将让你了解如何在C中打印数组。我们需要声明和定义一个数组,然后循环到数组的长度。 在每次迭代时,我们将打印一个数组的索引值。 我们可以从迭代本身获取此索引值。

算法 (Algorithm)

让我们首先看看该程序的逐步程序应该是什么 -

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

伪代码 (Pseudocode)

现在让我们看看这个算法的伪代码 -

procedure print_array(A)
   FOR EACH value in A DO
      DISPLAY A[n]
   END FOR
end procedure

实现 (Implementation)

上述派生伪代码的实现如下 -

#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
↑回到顶部↑
WIKI教程 @2018