我做错了什么(C数组)?

时间:2022-09-06 21:36:45

I'm just a beginner at C.
I'm trying to make a simple program to arrange the user-entered digits in ascending order. I have figured out the solution but can't understand why my other code wouldn't work :(

我只是一个初学c的人,我正在尝试做一个简单的程序来按升序排列用户输入的数字。我已经找到了解决方案,但是我不明白为什么我的其他代码不能工作:

-------------------------------------------------------------------------
working code:
-------------------------------------------------------------------------
#include <stdio.h>
int main()
{
    int i,j,num[10];
 printf("Enter 10 numbers\n");
 for (i=0;i<10;i++)
  {scanf("%d",&num[i]);}


    for (i=0;i<9;i++)
     {
         for (j=i+1;j<10;j++)
           {
               if (num[i]>num[j])
                 {
                    num[i]+=num[j];
                    num[j]=num[i]-num[j];
                    num[i]=num[i]-num[j];
                 }
           }
     }
    printf("The numbers in ascending order are:");
    for (i=0;i<10;i++)
      {
          printf(" %d",num[i]);
      }

    return 0;
}
-------------------------------------------------------------------------
code that won't work:
-------------------------------------------------------------------------
#include <stdio.h>
int main()
{
    int i,j,num[10];
 printf("Enter 10 numbers\n");
 for (i=1;i<=10;i++)
  {scanf("%d",&num[i]);}


    for (i=1;i<10;i++)
     {
         for (j=i+1;j<=10;j++)
           {
               if (num[i]>num[j])
                 {
                    num[i]+=num[j];
                    num[j]=num[i]-num[j];
                    num[i]=num[i]-num[j];
                 }
           }
     }
    printf("The numbers in ascending order are:");
    for (i=1;i<=10;i++)
      {
          printf(" %d",num[i]);
      }

    return 0;
}

In the latter program, numbers appear out of order, and there even are numbers that haven't been entered.

在后面的程序中,数字出现了次序,甚至还有没有输入的数字。

My question is, isn't it basically the same code? Just that in the latter program numbers would be stored from num[1] to num[10] instead of num[0] through num[9]?
Does it have something to do with array definitions?
It seems I have serious misconceptions, please help me out!

我的问题是,它不是基本相同的代码吗?在后面的程序中,数字将从num[1]存储到num[10]而不是num[0]到num[9]?它与数组定义有关吗?看来我有严重的误解,请帮助我!

6 个解决方案

#1


0  

for(i=0;i<9;i++)  //**i<9**    
   for (j=i+1 ...)

If i=8 then j=9 , everything is OK.

如果i=8,那么j=9,一切都没问题。

In second code snippet:

在第二个代码片段:

for(i=0;i<10;i++) //**i<10**
   for (j=i+1 ...)

If i=9 then j=10, so you try to access num[10] and it gives you error.

如果i=9,那么j=10,你尝试访问num[10],它会给你错误。

If you want to access num[10] then you must declare array int num[11] and then you can access num[10].

如果您想访问num[10],那么您必须声明数组int num[11],然后您可以访问num[10]。

Array Basics

int num[10]

int num[10]

  • Capacity of array = 10
  • 阵列容量= 10
  • Every element of this array are integer.
  • 这个数组的每个元素都是整数。
  • First element index is 0. So If you want to access first element , num[0]
  • 第一个元素索引是0。如果你想访问第一个元素,num[0]
  • Last element index is 9. So If you want to access last element, index must be length of array - 1 , so num[9]
  • 最后一个元素索引是9。如果你想访问最后一个元素,索引必须是数组的长度- 1,所以num[9]
  • There are 10 elements in the array and they are :
    • num[0] , num[1] , num[2] , num[3] , num[4] , num[5] , num[6] , num[7] , num[8] and num[9]
    • num[0],num[1],num[2],num[3],num[4],num[5],num[6],全国矿工工会num[7],[8]和num[9]
  • 数组中有10个元素,它们是:num[0]、num[1]、num[2]、num[3]、num[4]、num[5]、num[6]、num[7]、num[8]和num[9]

You can learn further at http://www.cplusplus.com/doc/tutorial/arrays/

您可以在http://www.cplusplus.com/doc/tutorial/arrays/上进一步学习。

#2


8  

In C, when you have int num[10];, your indexes need to go from 0 to 9, never to 10. So look over your code, if any i or j ends up with a value of 10 any time during the program run, that's bad news.

在C中,当您有int num[10]时,您的索引需要从0到9,而不是10。看看你的代码,如果任何i或j在程序运行过程中值为10,那就是坏消息。

#3


1  

Indexes in C go start from 0. so when you declare an array of size 10, and you try to get element at index 10, you're actually getting the 11th element. Since you haven't defined the 11th element, the array will most likely get some random numbers from memory, which is why you are noticing numbers you have note entered.

C中的索引从0开始。所以当你声明一个大小为10的数组,并尝试在索引10处获取元素时,你实际上得到了第11个元素。由于还没有定义第11个元素,所以数组很可能会从内存中获得一些随机数,这就是为什么您会注意到已输入的数字。

Since you are new to programming, I would suggest taking the time now to really learn about how C manages memory, and how different data structures access the memory. It might be a little boring now, but you'll save yourself some headaches in the future, and you will start to build good habits and good practices, which will lead to writing good, optimal code

由于您是编程新手,我建议您现在花点时间来真正了解C如何管理内存,以及不同的数据结构如何访问内存。现在它可能有点无聊,但是将来您将省去一些麻烦,并且您将开始构建良好的习惯和良好的实践,这将导致编写好的、最优的代码

#4


0  

In your non-working example, you have invalid memory accesses. For example, when i = 9 and j = 10, you access num[10], which is invalid memory.

在非工作示例中,内存访问无效。例如,当i = 9和j = 10时,您访问num[10],这是无效的内存。

#5


0  

Welcome to programming! I believe you are a bit confused about how arrays are indexed in C.

欢迎来到编程!我相信您对数组在C中如何被索引感到有点困惑。

Arrays in most languages (including C) are known as zero-indexed arrays. This means the start of an array is always at position 0. So if you make int num[10], trying to access num[10] isn't actually a valid call at all, because the start is num[0]. Hence you can only access from num[0] to num[9].

大多数语言(包括C)中的数组被称为零索引数组。这意味着数组的开始总是在位置0。如果你让int num[10],尝试访问num[10]实际上根本不是一个有效的调用,因为开始是num[0]。因此,只能从num[0]访问num[9]。

It's an easy mistake to make, even though I've been programming for years sometimes when it's been a long night I'll still make silly array indexing issues.

这是一个很容易犯的错误,即使我已经编程多年,有时当它是一个漫长的夜晚,我仍然会犯愚蠢的索引数组问题。

#6


0  

In your other code example, you were doing this:

在您的另一个代码示例中,您正在执行以下操作:

int num[10];

for(int i = 1; i <= 10; i++) {
    //...
}

That means you have an array with ten spaces [0-9], and for the last part of your for loop, you were trying to access num[10] which doesn't exist.

这意味着您有一个包含10个空格的数组[0-9],在for循环的最后一部分,您试图访问num[10],而num并不存在。

Your working example goes from 0-9 and never tries to read num[10], so it works.

您的工作示例从0-9开始,并且从不尝试读取num[10],因此它是有效的。

Arrays in C, as in most languages, start with position 0 and count that as position one. So the last element of your array would be the size you entered when you declared the variable, minus one.

C语言中的数组和大多数语言中的数组一样,从位置0开始,并将其计数为位置1。数组的最后一个元素就是你在声明变量- 1时输入的大小。

#1


0  

for(i=0;i<9;i++)  //**i<9**    
   for (j=i+1 ...)

If i=8 then j=9 , everything is OK.

如果i=8,那么j=9,一切都没问题。

In second code snippet:

在第二个代码片段:

for(i=0;i<10;i++) //**i<10**
   for (j=i+1 ...)

If i=9 then j=10, so you try to access num[10] and it gives you error.

如果i=9,那么j=10,你尝试访问num[10],它会给你错误。

If you want to access num[10] then you must declare array int num[11] and then you can access num[10].

如果您想访问num[10],那么您必须声明数组int num[11],然后您可以访问num[10]。

Array Basics

int num[10]

int num[10]

  • Capacity of array = 10
  • 阵列容量= 10
  • Every element of this array are integer.
  • 这个数组的每个元素都是整数。
  • First element index is 0. So If you want to access first element , num[0]
  • 第一个元素索引是0。如果你想访问第一个元素,num[0]
  • Last element index is 9. So If you want to access last element, index must be length of array - 1 , so num[9]
  • 最后一个元素索引是9。如果你想访问最后一个元素,索引必须是数组的长度- 1,所以num[9]
  • There are 10 elements in the array and they are :
    • num[0] , num[1] , num[2] , num[3] , num[4] , num[5] , num[6] , num[7] , num[8] and num[9]
    • num[0],num[1],num[2],num[3],num[4],num[5],num[6],全国矿工工会num[7],[8]和num[9]
  • 数组中有10个元素,它们是:num[0]、num[1]、num[2]、num[3]、num[4]、num[5]、num[6]、num[7]、num[8]和num[9]

You can learn further at http://www.cplusplus.com/doc/tutorial/arrays/

您可以在http://www.cplusplus.com/doc/tutorial/arrays/上进一步学习。

#2


8  

In C, when you have int num[10];, your indexes need to go from 0 to 9, never to 10. So look over your code, if any i or j ends up with a value of 10 any time during the program run, that's bad news.

在C中,当您有int num[10]时,您的索引需要从0到9,而不是10。看看你的代码,如果任何i或j在程序运行过程中值为10,那就是坏消息。

#3


1  

Indexes in C go start from 0. so when you declare an array of size 10, and you try to get element at index 10, you're actually getting the 11th element. Since you haven't defined the 11th element, the array will most likely get some random numbers from memory, which is why you are noticing numbers you have note entered.

C中的索引从0开始。所以当你声明一个大小为10的数组,并尝试在索引10处获取元素时,你实际上得到了第11个元素。由于还没有定义第11个元素,所以数组很可能会从内存中获得一些随机数,这就是为什么您会注意到已输入的数字。

Since you are new to programming, I would suggest taking the time now to really learn about how C manages memory, and how different data structures access the memory. It might be a little boring now, but you'll save yourself some headaches in the future, and you will start to build good habits and good practices, which will lead to writing good, optimal code

由于您是编程新手,我建议您现在花点时间来真正了解C如何管理内存,以及不同的数据结构如何访问内存。现在它可能有点无聊,但是将来您将省去一些麻烦,并且您将开始构建良好的习惯和良好的实践,这将导致编写好的、最优的代码

#4


0  

In your non-working example, you have invalid memory accesses. For example, when i = 9 and j = 10, you access num[10], which is invalid memory.

在非工作示例中,内存访问无效。例如,当i = 9和j = 10时,您访问num[10],这是无效的内存。

#5


0  

Welcome to programming! I believe you are a bit confused about how arrays are indexed in C.

欢迎来到编程!我相信您对数组在C中如何被索引感到有点困惑。

Arrays in most languages (including C) are known as zero-indexed arrays. This means the start of an array is always at position 0. So if you make int num[10], trying to access num[10] isn't actually a valid call at all, because the start is num[0]. Hence you can only access from num[0] to num[9].

大多数语言(包括C)中的数组被称为零索引数组。这意味着数组的开始总是在位置0。如果你让int num[10],尝试访问num[10]实际上根本不是一个有效的调用,因为开始是num[0]。因此,只能从num[0]访问num[9]。

It's an easy mistake to make, even though I've been programming for years sometimes when it's been a long night I'll still make silly array indexing issues.

这是一个很容易犯的错误,即使我已经编程多年,有时当它是一个漫长的夜晚,我仍然会犯愚蠢的索引数组问题。

#6


0  

In your other code example, you were doing this:

在您的另一个代码示例中,您正在执行以下操作:

int num[10];

for(int i = 1; i <= 10; i++) {
    //...
}

That means you have an array with ten spaces [0-9], and for the last part of your for loop, you were trying to access num[10] which doesn't exist.

这意味着您有一个包含10个空格的数组[0-9],在for循环的最后一部分,您试图访问num[10],而num并不存在。

Your working example goes from 0-9 and never tries to read num[10], so it works.

您的工作示例从0-9开始,并且从不尝试读取num[10],因此它是有效的。

Arrays in C, as in most languages, start with position 0 and count that as position one. So the last element of your array would be the size you entered when you declared the variable, minus one.

C语言中的数组和大多数语言中的数组一样,从位置0开始,并将其计数为位置1。数组的最后一个元素就是你在声明变量- 1时输入的大小。