【发布时间】: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 是一个非常糟糕的变量名称,甚至不确定这是否可以编译
-
你需要分配一些内存才能增长一个数组