【问题标题】:C101: the best way to fill an array from user input?C101:从用户输入中填充数组的最佳方式?
【发布时间】:2010-09-11 20:56:16
【问题描述】:

我很难理解,因此很难在 C 中手动管理数组和索引。这是我的两种经典方法,但它们似乎不起作用,因为它们在达到条件时会继续循环:

#include<stdio.h>
#define MAX 255

int main(){

    int arr[MAX]={0};
    int idx=0;

    /* Approach #1 */

    printf("Enter elements, -1 to finish:\n");
    scanf("%d", &arr[idx]);

    while(arr[idx-1] != -1 && idx < MAX){
        printf("Enter elements, -1 to finish:\n");
        scanf("%d", &arr[idx]);
        idx++;        
    }

    /* Approach #2 */

    do{
        printf("Enter elements, -1 to finish:\n");
        scanf("%d", &arr[idx]);
        idx++;
    }while(arr[idx-1] != -1 && idx < MAX);

    // Main func continues here.

}

任何建议将不胜感激!

更新:

现在可以了!非常感谢所有你们的即时回复。这绝对是一个很棒的社区,它对我帮助很大。

【问题讨论】:

    标签: c arrays indexing


    【解决方案1】:
    arr[idx] <= MAX
    

    应该是

    idx <= MAX
    

    【讨论】:

    • 仍然绕过条件。
    【解决方案2】:
    while(arr[idx] != -1 && idx <= MAX){ // Fixed by sklivvz
        printf("Enter elements, -1 to finish:\n");
        scanf("%d", &arr[idx]);
        idx++;        
    }
    

    首先,您应该检查索引变量 idx 是否小于 MAX(不小于或等于)。如果您的索引等于 MAX,您将超出范围。 MAX = 10 的数组的索引值为 0 到 9,包括 9,但不是 10。

    其次,将第一个元素添加到 arr[0],将索引从 0 增加到 1,然后跳回 while 条件并检查 arr[1] == -1,它不是.所以检查 arr[idx-1] != -1。但是请注意,第一次进入 while 循环时,您实际上会检查 arr[-1] != -1,这也超出了范围。 ;) 所以你需要弄清楚如何解决这个问题。

    【讨论】:

      【解决方案3】:

      转罗马 M:

      首先,问这个问题的人刚开始上编程课程,可能还没有学过指针。其次,您现在同时处理计数器和指针。我不确定我是否看到这样做与使用这样的索引相比的好处:

      for(idx=0;idx

      scanf("%d", &arr[idx]);
      if(arr[idx] == -1)
          break;
      

      }

      【讨论】:

      • 你说的没错,虽然我“研究”过指针,但我肯定还不熟悉,我还在练习基础,没有指针、结构和更高级的主题.谢谢!
      • 是的,但是:它 a 正确的答案,它是否或多或少是有争议的,此外我喜欢你在这个答案中的建议,如果有人在此之前发布过类似的东西我什至不会回答这个问题。但是没有人这样做:)
      【解决方案4】:

      使用 for 循环可以消除对 idx-1 检查代码的麻烦:

      /* Approach #3*/
      int i;
      int value;
      
      for (i = 0; i < MAX; ++i)
      {
        printf("Enter elements, -1 to finish:\n");
        scanf("%d", &value);
        if (value == -1) break;
        arr[i] = value;
      }
      

      【讨论】:

        【解决方案5】:
        arr[idx] <= MAX
        

        应该是

        idx < MAX
        

        除非您检查的是项目而不是索引。

        您还总是检查“下一个”元素的 -1 (arr[idx] != -1),因为您在检查附加值之前增加了 idx。

        如果你有

        arr[idx-1] != -1
        

        你会没事的。

        【讨论】:

        • 你说得对,我正在检查“下一个”元素,而不是我输入的那个。非常感谢!
        【解决方案6】:

        C 数组从 0 开始计数。

        如果您分配一个大小为 MAX 的数组,则访问 MAX 处的元素将是一个错误。 将循环更改为;

        int arr[MAX];
        for ( .... && idx < MAX )
        

        【讨论】:

        • 仍然绕过条件。
        【解决方案7】:

        在您的第一个 while 循环中,

        arr[idx] <= MAX
        

        行应该是

        idx <= MAX
        

        在你的第二个循环中,你在测试之前增加 idx - 它应该以

        结尾
        } while ((arr[idx-1] != -1) && (idx-1 <= MAX));
        

        我也倾向于将所有内部条件括起来,以绝对确定优先级是正确的(因此上面有额外的括号)。

        【讨论】:

          【解决方案8】:

          我会选择这样的。

          您不必担心数组边界和其他令人困惑的情况。

          int cnt = MAX;        // how many elements in the array, in this case MAX
          int * p = &arr[0];    // p is a pointer to an integer and is initialize to the address of the first
                                // element of the array. So now *p is the same as arr[0] and p is same as &arr[0]
          
          // iterate over all elements. stop when cnt == 0
          while (cnt) {
          
              // do somthing
              scanf("%d", *p); // remember  that *p is same as arr[some index]
              if (*p == -1)    // inspect element to see what user entered
                  break;
          
              cnt --;  // loop counter
              p++;     // incrementing p to point to next element in the array
          }
          

          【讨论】:

          • 有趣!你介意评论一下吗,不幸的是我不能无缝地跟随它。
          猜你喜欢
          • 1970-01-01
          • 2018-04-24
          • 1970-01-01
          • 2011-08-21
          • 2018-07-30
          • 2012-12-02
          • 1970-01-01
          • 2021-02-05
          • 2013-05-03
          相关资源
          最近更新 更多