【问题标题】:Error in code for inserting an element in an array在数组中插入元素的代码错误
【发布时间】:2022-01-17 11:33:00
【问题描述】:

这里我写了一个代码,它接受一个适合用户的任何大小的数组,用户同意在集合中插入一个新整数的位置,并与要插入的整数坐标。

#include <stdio.h>
#include <stdlib.h>

void insert(int barray_size, int *A, int new_place, int new_int)
{
    for (int i = barray_size; i >= new_place; i = i - 1)
    {
        *(A + i) = *(A + i - 1); //copies value of cell A[i-1] to cell A[i]
        *(A + i - 1) = 0; //empties cell A[i-1] so that it can take the value A[i-2] in next loop
    }

    *(A + new_place - 1) = new_int; //putting the required integer at A[k]
}

int main()
{
    int n, k, l;
    printf("Please enter array size: ");
    scanf("%d", &n);
    printf("\n");
    int A[n + 1];

    for (int i = 0; i < n; i++)
    {
        int temp;
        printf("Enter [%d] element: ", i);
        scanf("%d", &temp);
        A[i] = temp;
        printf("\n");
    }

    A[n] = 0;
    printf("Enter the place where new integer is to be inserted: ");
    scanf("%d", &k);

    if (k < n + 1)
    {
        printf("Enter the integer to be inserted: ");
        scanf("%d", &l);
        int t = n + 1;
        insert(t, A, k, l);
        printf("The new array is: ");

        for (int i = 0; i < t; ++i)
        {
            printf("%d ", A[i]);
        }
    }
    else
    {
        printf("Please enter a valid address.");
    }
}

但是当我运行这段代码时,它在接受整数位置后终止,但不接受要插入的整数。 您介意帮助检测错误吗?

谢谢

编辑1:修正scanf中地址的小错误。

现在程序或多或少地将整数正确地添加到数组中(尽管如果在k 处请求,则在位置k + 1,但可以更正)。但是它会打印除A[i + 1] 之外的所有数组值以及垃圾值,尽管最后使用了 for 循环,它应该在A[n + 1] 处停止打印循环。为什么会这样?

(如果我的疑问是琐碎或愚蠢的,我深表歉意。我最近开始学习C语言,由于缺乏实践,我很容易出现这种琐碎的错误,并且在练习时需要帮助澄清这些疑问,我寻求在SO)

编辑2:在初始化A[n] = 0(之前缺少的)后,程序可以完美运行并正常终止。

感谢大家的帮助。

【问题讨论】:

  • int A[n+1]; 您还没有为n 赋值。接受n 的值后,需要为数组分配空间。
  • 哦,对不起。已编辑和更正。但是程序仍然在同一个地方终止。
  • 想象一下,如果您使用有意义的变量名称而不是 insert(t, A, k, l);,这将是多么容易阅读。似乎insert 函数索引超出了分配数组的范围。考虑不要在此处添加+1int t=n+1; 它可能仍然超出范围,但我已经尝试阅读此内容。只需知道数组 int someArray[10]; 的有效索引是 0 - 9。
  • 您还假设来自用户的整体是有效的。如果用户决定创建一个大小为 10 的数组,然后决定在位置 2000 插入一个新整数怎么办。这是不允许的,因为它将超出数组的范围。
  • @whiplash 我很抱歉。我试图通过 if-else 语句来纠正该错误。请检查。

标签: arrays c


【解决方案1】:

你忘记在scanf和你的中指定变量kl的地址

变化:

scanf("%d", k);
scanf("%d", l);

收件人:

scanf("%d", &k);
scanf("%d", &l);

变化:

for (int i = 0; i < n+1; i++)

收件人:

for (int i = 0; i < n; i++)

下面是修改后的代码:

#include <stdio.h>
#include <stdlib.h>

void insert(int arrSize, int *A, int newPlace, int newInt)
{
    for(int i = arrSize; i > newPlace; i = i - 1)
    {
        *(A + i) = *( A + (i - 1));
        *(A + (i - 1)) = 0; 
    }
    *(A + newPlace) = newInt; 
}

int main()
{
    int no, place, newdata;
    printf("Please enter array size: ");
    scanf("%d", &no);
    printf("\n");
    int arr[no];
    for (int index = 0; index < no; index++)
    {
        int temp;
        printf("Enter [%d] element: ", index);
        scanf("%d", &temp);
        arr[index] = temp;
        printf("\n");
    }
    
    printf("Enter the place where new integer is to be inserted: ");
    scanf("%d", &place);
    
    if(place < no)
    {
        printf("Enter the integer to be inserted: ");
        scanf("%d", &newdata);
        
        insert(no, arr, place, newdata);
        printf("The new array is: ");
        for (int index2 = 0; index2 <= no; index2++)
        {
            printf("%d ", arr[index2]);
        }
    }
    else
    {
        printf("Please enter a valid address.");
    }
}

输出

Please enter array size: 7

Enter [0] element: 1

Enter [1] element: 2

Enter [2] element: 3

Enter [3] element: 4

Enter [4] element: 5

Enter [5] element: 6

Enter [6] element: 7

Enter the place where new integer is to be inserted: 5
Enter the integer to be inserted: 10
The new array is: 1 2 3 4 5 10 6 7 

【讨论】:

  • 抱歉错误。更正了地址问题。
  • @DevanshKamra 检查我修改后的答案
  • 我在数组大小 = 7 且 new_place=5 和 new_place=4 的在线编译器上检查了您的代码,但在这两种情况下都显示“请输入有效地址”。
  • 现在可以完美运行了。我想当我们比较使用 n+1 或 sizeof(n) 时它不起作用。感谢您的帮助!
  • 这可能只是一个小众的东西,但你在else 中的声明应该是Entered address is not valid. It should be between 0 and n 而不是Please enter a valid address. 这是因为你的错误消息不在循环中,所以用户不会有除非他重新运行程序,否则选择输入有效地址。
【解决方案2】:

在您的代码中,当您执行scanf("%d", ) 时,您需要传入变量的地址。不是变量本身。您可以尝试以下方法,看看是否有效:

int main()
{
    // CODE TO READ ARRAY AS YOU HAVE ALREADY WRITTEN
    printf(("Enter the place where new integer is to be inserted: ");
    scanf("%d", &k); // CHANGE HERE from k to &k
    printf("Enter the integer to be inserted: ");
    scanf("%d", &l); // CHANGE HERE FROM l to &l
    /* CHANGE HERE. 
    You don't need n+1 because when you create an array of size n+1, the indices are from 0 to n. 
    You are reading values only for 0 to n-1 in your loop.
    In your insert function, you are starting at i=n which is the last free space in the array.
    */
    insert(n, A, k, l)
    // REST OF YOUR CODE TO PRINT THE ARRAY
}

【讨论】:

    猜你喜欢
    • 2015-11-25
    • 2020-08-12
    • 2019-08-19
    • 2018-10-31
    • 2021-11-26
    • 1970-01-01
    • 2012-04-14
    • 2013-06-01
    • 2020-05-19
    相关资源
    最近更新 更多