【问题标题】:How to insert elements in an array using a function?如何使用函数在数组中插入元素?
【发布时间】:2020-08-06 11:37:23
【问题描述】:

我很难在C 中创建一个函数,该函数有两个参数,一个数组的名称和它的初始大小,并检查数组中的奇数。如果它找到一个,它将insert 其旁边的双倍并移动其余元素。我知道如何在没有函数的情况下做到这一点,但是有了函数,它就行不通了。该程序正在运行,但它什么也不做。请帮忙?

例如。 a[5]= {2, 5, 6, 8, 11};

 a[7]={2, 5, 10, 6, 8, 11, 22}

此外,函数不得返回任何内容,并且数组的索引从 0 到 n-1。

这就是我的函数的样子。

void Insert(int v[], int *n)    
{
    int i,j;
    
    for(i=*n-1; i>=0; i--)     //passing through the array from right to left
    {
        if(v[i]%2==1)       // if the element is odd
        {
            *n++;           // grow the array size by 1 
            int double=v[i]*2;    
            for(j=*n-1; j>=i+1; j--)    // move all elements in the right of the odd number to the right by 1 space
            {
                v[j+1]=v[j];
            }
            v[i+1]=double;    // add the double value next space after the odd number.
        }
    }
}

【问题讨论】:

  • 请说明您遇到的具体问题或错误结果。 “似乎是一个不同的故事”并没有告诉我们任何具体的事情。 Why is “Can someone help me?” not an actual question?
  • double 是一个非常糟糕的变量名称,甚至不确定这是否可以编译
  • 你需要分配一些内存才能增长一个数组

标签: arrays c function insert


【解决方案1】:

问题:

  • double 是 c 中的保留关键字。
  • 其实从这个问题我觉得你没有足够的内存来存储元素,而且你没有增加大小。
  • *n++等价于*(n++),但是你需要做的是(*n)++

好的,如果内存足够大,可以存储所有元素,那么您可以使用其他答案。如果没有,您可以使用此代码:

那你需要不时使用realloc来改变大小。因此,您的数组应该是动态创建的。您应该将指向数组的指针 (int **) 的指针发送到函数,如下面的代码所示。这些更改都是基于realloc 的工作方式完成的。

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

void Insert(int **v, int *n)
{
    int i, j;

    for(i = (*n) - 1; i >= 0; i--)     //passing through the array from right to left
    {
        if((*v)[i] % 2 == 1)       // if the element is odd
        {
            while (1) {
                int *p = realloc(*v, (*n + 1) * sizeof(int));         // grow the array size by 1
                if (p != NULL) {
                    *v = p;
                    (*n)++;
                    break;
                }
                else {
                    printf("Allocation failed .... Reallocating\n");
                    continue;
                }
            }

            int var = (*v)[i] * 2;
            for(j = (*n) - 1; j >= i + 2; j--)    // move all elements in the right of the odd number to the right by 1 space
            {
                (*v)[j] = (*v)[j - 1];
            }
            (*v)[i + 1] = var;    // add the double value next space after the odd number
        }
    }
}

int main()
{
    int size = 3;
    int *a = malloc(sizeof(int) * size);
    for(int i = 0; i < size; ++i)
        a[i] = i + 1;

    Insert(&a, &size);
    for (int i = 0; i < size; ++i) {
        printf("%d\n", a[i]);
    }

    free(a);
    return 0;
}

【讨论】:

  • 这对我来说相当复杂,因为我还没有达到分配内存的主题。但是非常感谢您花时间回答我的问题。当我对这件事有更好的了解时,我会回到这里,这对我有更大的帮助。
【解决方案2】:

如果分配给 v 的内存足够大,可以支持增长(见 main 函数中的 32),你有 2 个问题:

  • double 是保留字
  • *n++ 没有做你需要的,这增加了 n 指针而不是它的值
static void Insert(int v[], int *n)
{
    int i,j;

    for(i=*n-1; i>=0; i--)     //passing through the array from right to left
    {
        if(v[i]%2==1)       // if the element is odd
        {
            (*n)++;           // grow the array size by 1
            int d=v[i]*2;
            for(j=*n-1; j>=i+1; j--)    // move all elements in the right of the odd number to the right by 1 space
            {
                v[j+1]=v[j];
            }
            v[i+1]=d;    // add the d value next space after the odd number.
        }
    }
}

int main(void) {
    int a[32]= {2, 5, 6, 8, 11};
    int len = 5;
    Insert(a, &len);
    for (int i = 0; i < len; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
}

输出是:

2 5 10 6 8 11 22

【讨论】:

  • 哦,该死的。我没有在我的程序中使用 double ,而是用我的母语翻译它,它被 C 接受。然后我将我的整个程序翻译成英文,这样更容易理解,并且忘记了 double 是一种数据类型。所以基本上唯一的错误是我试图增加值的方式。编译器没有complay,所以我认为它没问题。但我现在明白了。非常感谢!
猜你喜欢
  • 2014-03-28
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
相关资源
最近更新 更多