even odd

I am new to c++ and i am having issues formatting code to output the number of even and odd integers in a randomized array. I have the code for the randomized array I just cant figure out how to add the even and odd part of it. I have a feeling I am just doing something obvious and dumb.

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

void print_array(int[], const int&);
void total_array(int[], const int&, int&);



int main()
{
	const int arraysize = 5;
	srand(unsigned(time(0)));   // seed the random number generator
	int num_arr[arraysize] = { 0 }, total = 0;   // array declaration

	for (int index = 0; index < arraysize; index++)
		num_arr[index] = rand() % 100 + 1;   // array update
	
	print_array(num_arr, arraysize);
	total_array(num_arr, arraysize, total);

	
	int n = sizeof(arr) / sizeof(arr[0]);
	CountingEvenOdd(arr, n);
	
	cout << "The total = " << total << endl;
	cout << "Number of even elements = " << even_count;
	cout << "\nNumber of odd elements = " << odd_count << endl;
	
	return 0;
} 

void print_array(int n[], const int &s)
{
	int spaces = 0;
	for (int i = 0; i < s; i++)
	{                                   // formaulates table 66 spaces wide
		cout << setw(4) << n[i] << " ";
		spaces += 6;

		if (spaces % 66 == 0)
			cout << endl;
	}

}

void total_array(int n[], const int &s, int &t)
{
	for (int i = 0; i<s; i++)
		t += n[i];
}


void CountingEvenOdd(int arr[], int arr_size)
{
	int even_count = 0;
	int odd_count = 0;
	
	for (int i = 0; i < arr_size; i++)

	{
		if (arr[i] & 1 == 1)
			odd_count++;
		else
			even_count++;
	}
	
}
You could define even_count and odd_count globally, i.e. outside and above of your functions.
I get that you don't know how to update two values. There are three ways.
(i) Global variables (or static variables)
(ii) Passing by reference (or pointer)
(iii) Returning a structure (or container)

Like nuderobmonkey said you can make even_count and odd_count global and then it would work. But it's better to use local variables unless you simply can't use them.

But my suggestion would be to pass two variables by references for odd_count and even_count.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void CountingEvenOdd(int arr[], int arr_size, int& odd_count, int& even_count)
{
	even_count = 0;
	odd_count = 0;
	
	for (int i = 0; i < arr_size; i++)

	{
		if (arr[i] & 1 == 1)
			odd_count++;
		else
			even_count++;
	}
	
}



Full program after edit:
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
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

void print_array(int[], const int&);
void total_array(int[], const int&, int&);



int main()
{
        int even_count, odd_count;
	const int arraysize = 5;
	srand(unsigned(time(0)));   // seed the random number generator
	int num_arr[arraysize] = { 0 }, total = 0;   // array declaration

	for (int index = 0; index < arraysize; index++)
		num_arr[index] = rand() % 100 + 1;   // array update
	
	print_array(num_arr, arraysize);
	total_array(num_arr, arraysize, total);

	
	int n = sizeof(arr) / sizeof(arr[0]);
	CountingEvenOdd(arr, n);
	
	cout << "The total = " << total << endl;
	cout << "Number of even elements = " << even_count;
	cout << "\nNumber of odd elements = " << odd_count << endl;
	
	return 0;
} 

void print_array(int n[], const int &s)
{
	int spaces = 0;
	for (int i = 0; i < s; i++)
	{                                   // formaulates table 66 spaces wide
		cout << setw(4) << n[i] << " ";
		spaces += 6;

		if (spaces % 66 == 0)
			cout << endl;
	}

}

void total_array(int n[], const int &s, int &t)
{
	for (int i = 0; i<s; i++)
		t += n[i];
}


void CountingEvenOdd(int arr[], int arr_size, int& odd_count, int& even_count)
{
	even_count = 0;
	odd_count = 0;
	
	for (int i = 0; i < arr_size; i++)

	{
		if (arr[i] & 1 == 1)
			odd_count++;
		else
			even_count++;
	}
	
}


Read about passing by reference: https://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
Last edited on
I appreciate the help! Your suggestions have helped a lot, the issue I am having still is that arr is not identified on line 27.
arr is not identified on line 27

The error message is correct; arr is not identified on line 27.
The array defined on line 18 is called num_arr. It was probably a copy-paste typo.
Last edited on
So how would I identify arr? Arr is not part of the random array, but is part of the even odd part. Also, how would I identify CountingEvenOdd on line 28? Sorry, its a bit confusing to me.
parameters are a type of renaming.

void foo (int x)
{
cout << x; //x is an alias of sorts for z in this tiny example.
}

int main()
{
int z = 12;
foo(z); //z becomes renamed to x in the function.
}

arr does not exist in main. it is the parameter in your function.
in terms of my example, you are trying to do this:

int main()
{
int z = 12;
foo(z); //z becomes renamed to x in the function.
cout << x; //nope, there is no x in main, nowhere, can't find it, error!
}

you need to call CountingEvenOdd with the variable that you DO have in main, which seems to be num_arr.

c++ is tightly scoped, which means that a variable in one scope (for now, think of scope as {} pairs, you can extend this definition later to be more complete) cannot be seen in another scope. There is a global scope (everything can see it) but that is highly recommended to never use for variables.
Last edited on
I should just count the odd numbers (for example) and make it the return value of the function. The count of even numbers is just the array size n minus the count of odds.

Come to think of it, your totalling function would be more natural returning the sum as a double than as a void function.

For fun, since it's how I think numerical arrays ought to work:
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <valarray>
using namespace std;

ostream & operator << ( ostream &str, const valarray<int> &V )
{
   int num = 0;
   for ( int e : V ) str << setw( 4 ) << e << ( (++num % 10) ? "" : "\n" );
   return str;
}

int main()
{
   const int N = 25;
   valarray<int> V( N );
   srand( time( 0 ));
   for ( int &e : V ) e = 1 + rand() % 100;
	
   int odds = ( V % 2 ).sum();
   cout << V;
   cout << "\nTotal = " << V.sum();
   cout << "\nNumber of even elements = " << N - odds;
   cout << "\nNumber of odd elements = " << odds;
}



  68  17  29  96  21  70  54  21  80  75
  77  84  46  36  15  36  62  87  51  23
  10  50  35   5  14
Total = 1162
Number of even elements = 13
Number of odd elements = 12
Last edited on
Did what you posted compile originally? Shouldn't have. If it did then you're missing something.

Anyways

Why have you written

1
2
int n = sizeof(arr) / sizeof(arr[0]);
	CountingEvenOdd(arr, n);


And not
CountingEvenOdd(num_arr, arraysize);

Like you've called print_array and total_array at line 22 and 23?
1
2
print_array(num_arr, arraysize);
	total_array(num_arr, arraysize, total);


Anyways change it to CountingEvenOdd(num_arr, arraysize);
Thanks for the help everyone. The only issue I am still having is that I can CountingEvenOdd identifier is not found on line 24.

My edited code looks like this.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

void print_array(int[], const int&);
void total_array(int[], const int&, int&);


int main()
{
	int even_count, odd_count;
	const int arraysize = 10;
	srand(unsigned(time(0)));   // seed the random number generator
	int num_arr[arraysize] = { 0 }, total = 0;   // array declaration

	for (int index = 0; index < arraysize; index++)
		num_arr[index] = rand() % 100 + 1;   // array update
	
	print_array(num_arr, arraysize);
	total_array(num_arr, arraysize, total);
        CountingEvenOdd(num_arr, arraysize);
	
	cout << "The total = " << total << endl;
	cout << "Number of even elements = " << even_count;
	cout << "\nNumber of odd elements = " << odd_count << endl;
	
	return 0;
} 

void print_array(int n[], const int &s)
{
	int spaces = 0;
	for (int i = 0; i < s; i++)
	{                                   // formaulates table 66 spaces wide
		cout << setw(4) << n[i] << " ";
		spaces += 6;

		if (spaces % 66 == 0)
			cout << endl;
	}

}

void total_array(int n[], const int &s, int &t)
{
	for (int i = 0; i<s; i++)
		t += n[i];
}


void CountingEvenOdd(int arr[], int arr_size, int& odd_count, int& even_count)
{
	even_count = 0;
	odd_count = 0;

	for (int i = 0; i < arr_size; i++)

	{
		if (arr[i] & 1 == 1)
			odd_count++;
		else
			even_count++;
	}
	
}
Last edited on
You need declaration
void CountingEvenOdd(int arr[], int arr_size, int& odd_count, int& even_count);
at the top (like the other two functions).

Then when you call it you need 4 arguments (including your odd_count and even_count : these are currently missing when you call the function.)

Also, you didn't initialise t in total_array.
Last edited on
By the way total is initialized in the main function so that part is correct. Although it would make sense to also initialize t with 0 in the function or rather bother initializing only in the function.
#include <iostream>
using namespace std;

int main()
{
int n;
cout << "Enter number: ";
cin >> n;

if ( n % 2 == 0)
cout << n << " is even number.";
else
cout << n << " is odd number.";

}
Topic archived. No new replies allowed.