Help With Output - Arrays, Pointers, Functions

Hey all, my assignment was to write a program that asks a user for the number of inputs that they want, then for an array of that length. Using functions, and pointers, the program will split the array input into odds and evens, and output the odd numbers, even numbers and then the number of zeros. At the moment everything works correctly, my outputs are just a little wonky with extra zeros and I can't figure out where they are coming from.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <cstdlib>
#include <iostream>

using namespace std;

int count(const int numbers[], int all, int & even, int & odd);

void split(int numbers[], int all, int odds[], int odd, int evens[], int even);

void print_list(int num[], int n);

int main()
{

  int all, even, odd, zr;
  int * numbers;
  int * odds;
  int * evens;

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

  numbers = new int[all];
  
  cout << "Enter list:" << endl;

  for (int i=0; i<all; i++)
  {
    cin >> numbers[i];
  }

  zr = count(numbers,all,even,odd);
  
  count(numbers,all,even,odd);

  odds = new int[odd];
  evens = new int[even];

  split(numbers,all, odds, odd, evens, even);

  cout << "Even elements: ";
  print_list(evens, even);
  cout << "Odd elements: ";
  print_list(odds, odd);

 

  cout << "There were " << zr << " zeros in the list.";

  delete[] evens;
  delete[] odds;
  delete[] numbers;

  return 0;

}

int count(const int numbers[], int all, int & even, int & odd)
{
  int i, zer(0);
  even = 0;
  odd = 0;
  


  for(i=0;i<all;i++)
  {
	if(numbers[i]%2==0)
	{
          even++;
	}

	if(numbers[i]==0)
	{
          zer++;
	}
	else
	{
	  odd++;
	}
  }

return zer;
}

void split(int numbers[], int all, int odds[], int odd, int evens[], int even)
{
  int i,e=0,o=0;

  for(i=0;i<all;i++)
  if(numbers[i]%2==0)
  {
	evens[e]=numbers[i];
        e++;
  }
  else
  {
  	odds[o]=numbers[i];
        o++;
   }

   if(e!=even||o!=odd)
   {
	cout<<"Error\n";
    	system("exit");
    }
}
void print(int num[],int n)
{

  for(int i=0;i<n;i++)
  {
	cout<<num[i]<<" ";
  }

  cout<<endl;
}

void print_list(int num[], int n)
{
  int i;
  for(i=0;i<n;i++)
    cout<<num[i]<<" ";
    cout<<endl;
}


And here is an output if I input "4" then make my array "0 1 0 2".


Enter number of elements: 4
Enter list:
0
1
0
2
Error
Even elements: 0 0 2 
Odd elements: 1 0 
There were 2 zeros in the list.


Something seems to be setting off my Error and that is why I just output "error" in the output and I can't figure out what it might be.
That's pretty nice code. It's easy to read, well organized and does what it needs to do. My only suggestion would be to use vector<> instead of dynamic arrays. This would make the memory management easier and you wouldn't have to pass the array and it's size separately.

The bug is in count(). The else at line 77 matches the if at line 73, so even is actually counting the number of non-zero elements, not the number of odd elements. The contents of the loop should be :
1
2
3
4
5
6
7
8
	if(numbers[i]%2==0) {
          even++;
	} else {
	  odd++;
	}
	if(numbers[i]==0) {
          zer++;
	}

Actually you could move the check for zero inside the code that detects that it's even to save a few cycles.

One other comment, you have a redundant call to count() at line 34
Topic archived. No new replies allowed.