Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在c+;中删除数组中没有任何内置函数的元素+;_C++_Arrays - Fatal编程技术网

C++ 在c+;中删除数组中没有任何内置函数的元素+;

C++ 在c+;中删除数组中没有任何内置函数的元素+;,c++,arrays,C++,Arrays,问题是: “编写并测试以下从数组中删除项的函数: void removeAll(float a[], int& n, float x); 该函数用于删除数组前n个元素中出现的所有x,并将n的值减去删除的数量 我已经编写了一个代码,它工作得非常好。基本上,我用零标记了用户希望删除其出现次数的值,当它遇到零时,代码将跳过打印。基本上,我们不是从数组中删除该值,而是用其他值替换它。如果用户在数组中输入零怎么办?然后是另一个值oes也将被跳过,我也不想要它们。我如何才能做到这一点 这是我的密码

问题是: “编写并测试以下从数组中删除项的函数:

void removeAll(float a[], int& n, float x);
该函数用于删除数组前n个元素中出现的所有x,并将n的值减去删除的数量

我已经编写了一个代码,它工作得非常好。基本上,我用零标记了用户希望删除其出现次数的值,当它遇到零时,代码将跳过打印。基本上,我们不是从数组中删除该值,而是用其他值替换它。如果用户在数组中输入零怎么办?然后是另一个值oes也将被跳过,我也不想要它们。我如何才能做到这一点

这是我的密码:

不要使用任何类、对象、向量、字符串,也不使用C++中的任何函数,如J../P>中的数组。

 #include<iostream>
using namespace std;


void removeAll(float a[], int &n, float x)
{
    for (int i = 0; i < n; i++)
    {
        if (a[i] == x)
        {
            a[i] = 0;
        }
    }
    for (int i = 0; i < 10; i++)
    {
        if (a[i]!=0)
            cout << a[i] << endl;
    }
}

int main()
{
    float a[10], x;
    int n;
    cout << "Enter values in array: \n";
    for (int i = 0; i < 10; i++)
    {
        cin >> a[i];
    }
    cout << "Enter the value whose occurrence you want to remove from the array: ";
    cin >> x;
    cout << "Enter serial no. uptill which you want to remove the occurences";
    cin >> n;
    removeAll(a, n, x);
}
#包括
使用名称空间std;
void removeAll(浮点a[],整数与否,浮点x)
{
对于(int i=0;in;
移除所有(a,n,x);
}

如果您只想在没有选择变量的情况下打印数组,您可以使用一个条件:

void removeAll(float a[], int &n, float x)
{
    for (int i = 0; i < n; i++)
    {
        if (a[i] == x)
        {
            // Don't print it
        }
        else
        {
            cout << a[i] << endl;
        }
    }
}

要做到这一点,您需要做4件事:

  • 复制
    n
    以保存要迭代的元素数:
    int size=n
  • 更改循环条件以考虑此新元素而不是<代码> N< /代码>:<代码> i <大小<代码> < /LI>
  • 每次替换嵌套的
    if
    -语句中的值时,减量
    n
  • 删除第二个
    for
    -循环(如果要打印
    n
    下的所有内容,但替换的数字在
    else
    -条件下执行此操作。)

  • 从技术上讲,您无法删除数组中的元素。数组将始终具有固定大小和固定数量的元素。不要试图欺骗代码的调用者,他们会很容易看到您所做的只是“在数组中打个洞”"。与其试图变得过于聪明,不如找到元素,然后将所有剩余元素复制到左侧的一个位置。最后,您显示的代码有什么问题?您的问题是什么?您的代码是否执行了它应该执行的操作?输出是否符合您的预期?请花一些时间。您正在反对这个问题。问题是:stion说:“从数组中删除项目”,你不是从数组中删除项目。你的老师要求你做的是编写一个函数,该函数与
    erase
    一起执行(erase-remove成语)。
    array: 2,3,4,5,3,7
    number to remove: 3
    
    1. Iterate through the array and find '3'
    2. If you find one, move all elements on the right side on step to left:
       2 3 4 5 3 7  => 2 4 5 3 7 X
         ^
       2 4 5 3 7 X  => 2 4 5 7 X X
             ^
    3. Count how many times you have done step two and return the new length/new array