目录

程序以相反的顺序打印数组(Program to print an array in reverse order)

要以相反的顺序打印数组,我们应事先知道数组的长度。 然后我们可以从数组的长度值开始迭代到零,并且在每次迭代中我们可以打印数组索引的值。 该数组索引应直接从迭代本身派生。

算法 (Algorithm)

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

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

伪代码 (Pseudocode)

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

procedure print_array(A)
   FOR from array_length(A) to 0
      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 = 9; loop >= 0; loop--)
      printf("%d ", array[loop]);
   return 0;
}

输出应该是这样的 -

0 9 8 7 6 5 4 3 2 1
↑回到顶部↑
WIKI教程 @2018