【问题标题】:Print out an iterative array at the end of the iteration in C在 C 中的迭代结束时打印出一个迭代数组
【发布时间】:2014-05-08 09:00:47
【问题描述】:

也许这是一个愚蠢的问题,但在我的 C 代码中,我迭代了一个 3X3 数组,并且我需要在迭代后打印出该数组的最终值。

我的问题是:我应该将命令 printf 数组中的值放在哪里?在指定数组之后放置 printf 对我来说真的没有意义,并且无论如何都不起作用。

我对 C 语言比较陌生,因此我们将不胜感激任何帮助 :-D!谢谢!

【问题讨论】:

  • 您有 2 个嵌套的 for 循环。如果要打印每个值,请在第二个。如果要打印行,请在第二个之后。如果要打印总计,请先打印。
  • 显示您为尝试执行此操作而编写的代码。
  • 请出示实际代码; printf 在任何情况下都必须在声明之后执行。

标签: c arrays iteration printf


【解决方案1】:

你必须这样做

for(i=0; i<3;i++)
   for(j=0;j<3;j++)
        printf("%d ", array[i][j];

【讨论】:

    【解决方案2】:

    我猜你使用 2 个嵌套的 for 循环来遍历数组。

    for (/* rows */) {
        for (/* columns */) {
            /* put the printf here for manipulations for individual cells */
        }
        /* put the printf here for manipulations on rows */
    }
    /* put the printf here for manipulations on the table */
    

    【讨论】:

    • /* printf here for rows */ 似乎不正确!!
    • 我不知道他想用代码做什么。可以是总和,可以是平均数,或不同的东西。这是指示他可以在哪里打印适当信息的伪代码。
    • 我同意你的观点,我的意思是它应该是这样的:/*printf here for any specific row */ 你现在编辑它。
    【解决方案3】:

    由于您正在处理二维数组,因此您可能希望在打印每 3 个元素(单行)后换行

    for(i=0;i<3;i++)              /* first for() loop*/
    {
        for(j=0;j<3;j++)          /* second for() loop*/
        {
            /* Your caculations/operations to be done for each element   */
            printf(" %d ",a[i][j]); /* Print the three elements (assuming an integer array a[3][3] with values stored)  */
        }
        printf("\n");               /* A new line is printed after printing 3 elements   */
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-03
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2015-09-29
      相关资源
      最近更新 更多