编辑:根据偶数和奇数元素将数组拆分为两个

时间:2013-11-12 22:12:36

标签: c++

我想编写这个程序来读取用户的元素和列表的数量,打印两个数组一个用于偶数,一个打印用于奇数,并显示第一个数组中的零个数。

我纠正了错误,现在没有错误,但输出错误。 这是代码:

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

// Function prototypes
int count(const int list[],  int size, int & odd_num, int & even_num);
void split(const int list[], int size, int list_even[], int even_num, int list_odd[], int odd_num );
void print_list(int array[], int length);

int main()
{
  int size; // llength of the array, user input
  int zero_num; // number of zeros
  int even_num;
  int odd_num;
  int * list; // array to store the user inputs

  // Prompt and read number of elements from the user into the variable size
  cout << "Enter number of elements: ";
  cin >> size;

  // Create a new array of size length and assign it to variable list
  list = new int [size];

  // Prompt and read the elements from the user
  cout << "Enter list: "<<endl;
  for (int i=0; i<size; i++) 
    {
      cin>> list[i];
    }

  // Call function count and save its return value in variable num_zero
  zero_num = count(list,size,odd_num,even_num);

  // allocate an array for even numbers
  int * list_even;
  list_even = new int [even_num];

  // allocate an array for odd numbers
  int * list_odd;
  list_odd= new int [odd_num];

  //

  split(list, size, list_even, even_num, list_odd, odd_num);
  cout << "Even elements: ";
  print_list(list_even, even_num);
  cout<< endl;
  cout << "Odd elements: ";
  print_list(list_odd, odd_num); 
  cout << endl;


  // Delete the lists
  delete[]list;
  delete[]list_even;
  delete[]list_odd;


  return 0;
}

//functions

//function to count the odd and even numbers and th enumber od zeros
int count(const int list[],  int size, int & odd_num, int & even_num)

{
  int i(0); //while loop variable
  int even(0); // variable to count even numbers
  int odd (0); // variable to count odd numbers
  int zero(0); // variable to count zeros
  while (i<size)
     {
       if (list[i] == 0)
     {
       zero++;
     }
       else if (list[i]% 2 ==0)
     {
       even++;
     }
       else 
     {
       odd++;
     }
       i++;
     }
 return  zero;

}

// function to copy odd and even number to seperate arrays
void split(const int list[], int size, int list_even[], int even_num, int list_odd[], int odd_num )
{
  int j(0);
  int k(0);
  while (j<even_num && k<odd_num)
    {
      for (int i(0); i<size; i++)
    {
      if(list[i]%2 == 0)
        {
          list_even[j]= list[i];
          j++;
        }
      if(list[i]%2 == 1)
        {
          list_odd[k]= list[i];
          k++;
        }
    }
    }
}

// function to print an  array
void print_list(int array[], int length)
{
  cout << array << endl;
}

这是输入和输出的例子:

Enter number of elements: 6
Enter list: 
1
2
3
4
5
6
Even elements: 0x988030

Odd elements: 0x7f601e480010

4 个答案:

答案 0 :(得分:5)

您以这种方式致电split

void split(list[], size, list_even[], even_num, list_odd[], odd_num);

这是错的。它的功能是声明。它应该是(以下两行也有这个问题)

split(list, size, list_even, even_num, list_odd, odd_num);

另一个问题是,您按void打印cout,删除了cout代码:

split(list, size, list_even, even_num, list_odd, odd_num);
print_list(list_even, even_num);
print_list(list_odd, odd_num);

最后,要打印一个数组int x[],你应该通过循环对项进行迭代,所以这段代码是错误的:

void print_list(int array[], int length)
{
    cout << "Even elements: " << array[] << endl;
}

请改用:

void print_list(int array[], int length)
{
    cout << "Even elements: " << endl;
    for (int i=0; i<length; i++)
    cout << array[i] << " ";
}

Here is a working code (at least it compiles)


虽然你std::vector忘了那些硬阵列。

答案 1 :(得分:1)

你的电话前面有一个void在第58行分开。删除“无效”一词。

答案 2 :(得分:1)

在函数调用中删除void

void split(list[], size, list_even[], even_num, list_odd[], odd_num);

答案 3 :(得分:1)

或者您可以使用标准容器和算法:

int main()
{
    std::vector<int> numbers;
    std::size_t count;

    std::cout << "Enter the number of elements:  ";
    std::cin >> count;

    while (numbers.size() < count)
    {
        int i;
        if (std::cin >> i)
        {
            numbers.push_back(i);
        }
        else
        {
            std::cin.clear();
        }
    }

    std::vector<int> evens, odds;
    std::partition_copy(numbers.begin(), numbers.end(), std::back_inserter(evens), std::back_inserter(odds), [](int i)
    {
        return 0 == i % 2;
    });

    std::size_t numZeros = std::count(numbers.begin(), numbers.end(), 0);

    std::cout << "Evens (" << evens.size() << "):  ";
    std::copy(evens.begin(), evens.end(), std::ostream_iterator<int>(std::cout, ", "));
    std::cout << std::endl;

    std::cout << "Odds (" << odds.size() << "):  ";
    std::copy(odds.begin(), odds.end(), std::ostream_iterator<int>(std::cout, ", "));
    std::cout << std::endl;

    return 0;
}