简体   繁体   English

C ++将数组拆分为2个独立的数组

[英]C++ Splitting an array into 2 separate arrays

I need to write a program that takes a given array and then splits it into two separate arrays with one array's elements being the positive elements of the main array and the other's elements being the negative elements of the main array. 我需要编写一个程序,它接受一个给定的数组,然后将它分成两个独立的数组,其中一个数组的元素是主数组的正元素,另一个元素是主数组的负元素。

After doing my best with the code, I got about a million lines of errors when trying to compile it. 在充分利用代码之后,我在尝试编译时遇到了大约一百万行错误。 Is there a problem with how I am deleting the three dynamically allocated arrays? 我如何删除三个动态分配的数组有问题吗? What huge error is preventing compiling? 什么巨大的错误阻止编译? Here is my code: 这是我的代码:

#include <iostream>
using namespace std;


void count(int ARRAY[], int SIZE, int& NEG, int& POS);
void split(int ARRAY[], int SIZE, int& NEG_ARRAY, int NEG, int& POS_ARRAY, int POS);
void print_array(int ARRAY[], int SIZE);


int main()
{

  int SIZE(0);
  int* ARRAY;

  cout << "Enter number of elements: ";
  cin >> SIZE ;

  ARRAY = new int[SIZE];
  int x(0);
  int numEle(0);

  cout << "Enter list: " << endl;

  while (numEle < SIZE)
  {
      ARRAY[numEle] = x;
      numEle++;
      cin >> x;
  }

  int POS(0), NEG(0);
  count(ARRAY, SIZE, NEG, POS);

  int* NEG_ARRAY;
  NEG_ARRAY = new int[NEG];

  int* POS_ARRAY;
  POS_ARRAY = new int[POS];


  split(ARRAY, SIZE, NEG_ARRAY, NEG, POS_ARRAY, POS);

  cout << "Negative elements: " << endl;
  cout << print_array(NEG_ARRAY, NEG) << endl;

  cout << "Non-negative elements: " << endl;
  cout << print_array(POS_ARRAY, POS) << endl;


  delete [] ARRAY;
  delete [] NEG_ARRAY;
  delete [] POS_ARRAY;

  return 0;
}

void count(int ARRAY[], int SIZE, int& NEG, int& POS)
{
    for (int x=0; x < SIZE; x++)
    {
        if (ARRAY[x] >= 0)
    {
        POS = POS + 1;
    }
        if (ARRAY[x] < 0)
    {
        NEG = NEG + 1;
    }
    }
}

void split(int ARRAY[], int SIZE, int& NEG_ARRAY, int NEG, int& POS_ARRAY, int POS)
{
    NEG = POS = 0;
    for (int x = 0; x < SIZE; x++)
    {
        if (ARRAY[x] < 0)
    {
            NEG_ARRAY[NEG++] = ARRAY[x];
        }
        else
        {
            POS_ARRAY[POS++] = ARRAY[x];
        }
    }
}

void print_array(int ARRAY[], int SIZE)
{
    for (int i = 0; i < SIZE; i++)
    {
        cout << ARRAY[i] << " ";
    }
    cout << endl;
}

The code is supposed to read in the array and display a new negative and a new positive array. 该代码应该在数组中读取并显示一个新的负数和一个新的正数组。 Thanks in advance! 提前致谢!

You have the following bugs: 您有以下错误:

void split(int ARRAY[], int SIZE, int&NEG_ARRAY, int NEG, int&POS_ARRAY, int POS);

change to : 改成 :

void split(int ARRAY[], int SIZE, int*NEG_ARRAY, int NEG, int*POS_ARRAY, int POS);

also the : 还有:

void split(int ARRAY[], int SIZE, int&NEG_ARRAY, int NEG, int&POS_ARRAY, int POS){..}

change to : 改成 :

void split(int ARRAY[], int SIZE, int*NEG_ARRAY, int NEG, int*POS_ARRAY, int POS){..}

and

cout<<print_array(NEG_ARRAY, NEG) <<endl
cout<<print_array(NEG_ARRAY, POS) <<endl;

to : 至 :

print_array(NEG_ARRAY, NEG);
print_array(NEG_ARRAY, POS);

After fixed these bugs, it can compile and run well. 修复这些错误后,它可以编译并运行良好。

There is a bunch of errors in your code. 您的代码中存在大量错误。 The worst one is passing the arrays by references in the declaration and definition of the split function. 最糟糕的是通过声明和split函数定义中的引用传递数组。 Change both to void split(int ARRAY[], int SIZE, int *NEG_ARRAY, int NEG, int *POS_ARRAY, int POS); 将两者都更改为void split(int ARRAY[], int SIZE, int *NEG_ARRAY, int NEG, int *POS_ARRAY, int POS); , and most of the errors will be gone. ,大多数错误都将消失。

The rest is from the two lines in which you print the array in your main : 其余部分来自您在main打印数组的两行:

cout<<print_array(NEG_ARRAY, NEG) <<endl;

You don't want to print the function, you want to use the function to print inside it (which you do correctly). 您不想打印该功能,您想使用该功能在其中打印(您正确执行)。 You need to change the calls to simply: 您需要将调用更改为:

print_array(NEG_ARRAY, NEG);

And that'll make your code compile. 这将使您的代码编译。

Hovewer there's one more error, which will make the whole app work in an improper way. Hovewer还有一个错误,这将使整个应用程序以不正确的方式工作。 In the place you input the values, you need to get the input from cin before inputting it in the array. 在输入值的位置,您需要输入数组之前cin获取输入。 Like this: 像这样:

while(numEle<SIZE) {
  cin>>x;
  ARRAY[numEle] = x ;
  numEle++;
}

First of all, using a std::vector is almost always nicer than using dynamically allocated C arrays. 首先,使用std::vector几乎总是比使用动态分配的C数组更好。 You don't get the horrible mixture of pointers and square bracket array access, and you don't need to pass round extra size variables. 你没有获得指针和方括号数组访问的可怕混合,并且你不需要传递额外的大小变量。

Secondly, the standard library has some nice algorithms to help do what you want to do. 其次,标准库有一些很好的算法来帮助你做你想做的事情。 Let's assume that you write the given numbers into a vector called vec . 让我们假设你将给定的数字写入一个名为vec的向量中。 You can then use std::partition to move all the elements less than zero to the first half of the vector, and all the elements greater than or equal to zero to the second half, like so: 然后,您可以使用std::partition将小于零的所有元素移动到向量的前半部分,将所有大于或等于零的元素移动到后半部分,如下所示:

inline bool less_than_zero(int a)
{
    return a < 0;
}

std::vector<int>::iterator midpoint = std::partition(vec.begin(),
                                                     vec.end(),
                                                     less_than_zero);

(There are other ways of specifying the predicate, but a simple function definition like this is easiest for demonstration purposes.) (还有其他指定谓词的方法,但是这样的简单函数定义最容易用于演示目的。)

The returned iterator points to the first item in the vector which is non-negative. 返回的迭代器指向向量中的第一个非负数项。 So now you can easily copy the values into two new vectors: 所以现在您可以轻松地将值复制到两个新向量中:

std::vector<int> negative(vec.begin(), midpoint);
std::vector<int> positive(midpoint, vec.end());

And that's it! 就是这样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM