Arrays vs Arguments

Hi there,

I am currently lost on how to fix my code for this assignment. Any help will greatly be appreciated!

Write a function called fillArray that will fill an array of any size with random numbers in the range of 1 - 100.
Write a separte function called printArray that will print an array of any size.
Write a third function called countEvens which will count and return the number of even numbers in the array.


In main create an array that holds 25 ints. Use your functions to fill, print, and count the number of even numbers. You should

Fill the array
Print the array
Print the number of even numbers in the array
What not to do

Using any of the following will drop your grade for this assignment by 70%

global variables
cin in any funciton other than main
cout in any funciton other than main and printArray
goto statements

It looks like this:
16
90
90
48
80
45
63
28
32
12
77
80
75
83
90
62
56
4
12
76
52
5
There are 18 even numbers in the array
Press any key to continue . . .

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
#include <iostream>
#include <stdio.h>
#include <time.h>

using namespace std;

void fillArray(int num[], int size);
void printArray(int num[], int size);
int countEvens(int num[], int size);

int main()
{
	int num[25], count;

	fillArray(num, 25);
	printArray(num, 25);
	count = countEvens(num, 25);
	printf("There are %d even integers in the array.", count);

	return 0;
}
void fillArray(int num[], int size)
{
	int i;
	for (i = 0; i < size; i++)
	{
		int c = rand() % 100 + 1;
		if (c>0 && c<101)
			num[i] = c;
	}
}
void printArray(int num[], int size)
{
	int i;
	for (i = 0; i < size; i++)
		printf("%d\n", num[i]);
}
int countEvens(int num[], int size)
{
	int count = 0, i;
	for (i = 0; i < size; i++)
	{
		if (num[i] % 2 == 0)
			count++;
	}
	return count;
}
...will drop your grade by 70%

Does that seem a little excessive, or is it just me?

Anyways, remove line 28.
If you only conditionally fill the elements of the array, some elements may be uninitialized, which would cause undefined behavior at the whim of the random number generator.
Last edited on
I am currently lost on how to fix my code for this assignment.

What exactly is wrong with your current code? Please ask specific questions.

Why are you using the C-stdio functions instead of the C++ standard streams (cout)?

Remember this is C++ so you can declare your loop variable in the for() loop initialization section:

1
2
3
for(int i = 0; i < size; ++i)
{
   ...


You should also use a named constant for the array size instead of the "magic number" (25):

1
2
3
const int ARRAY_SIZE = 25;

int num[ARRAY_SIZE];

Last edited on
So, here's what I've changed so far
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
#include <iostream>
#include <ctime>

using namespace std;

void fillArray(int num[], int size);
void printArray(int num[], int size);
int countEvens(int num[], int size);

int main()
{
	const int array_Size = 25; // changed the name as suggested
	int num[array_Size], count;

	fillArray(num, 25);
	printArray(num, 25);
	count = countEvens(num, 25);
	cout << "\nThere are " << count << " even numbers in the array " << endl; // use the c++ streams as suggested

	return 0;
}
void fillArray(int num[], int size)
{
	int i, c;
	for (i = 0; i < size; i++)
	{
		int c = rand() % 100 + 1;
		if (c > 0 && c < 100)    // if I delete this line something wrong goes with the code
			num[i] = c;
	}
}
void printArray(int num[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << "\n" << num[i];
	}
}
int countEvens(int num[], int size)
{
	int count = 0, i;
	for (i = 0; i < size; i++)
	{
		if (num[i] % 2 == 0)
			count++;
	}
	return count;
}


My problem is that it keeps showing in the output "There are 13 even numbers in the array" when the output should show there are 18 numbers instead of 13.
Last edited on
It seems to work fine for me.
1
2
3
 84 87 78 16 94 36 87 93 50 22 63 28 91 60 64 27 41 27 73 37 12 69 68 30 83

There are 13 even numbers in the array 


There are indeed 13 even numbers in that list of numbers.

By the way you should have used your named constant everywhere 25 appeared, not just in the array definition.

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

using namespace std;

void fillArray(int num[], int size);
void printArray(int num[], int size);
int countEvens(int num[], int size);

int main()
{
	const int ARRAY_SIZE = 25; // changed the name as suggested
	int num[ARRAY_SIZE], count;

	fillArray(num, ARRAY_SIZE );
	printArray(num, ARRAY_SIZE);
	count = countEvens(num, ARRAY_SIZE);
	cout << "\nThere are " << count << " even numbers in the array " << endl; // use the c++ streams as suggested

	return 0;
}
void fillArray(int num[], int size)
{
        // NOTICE THE SIMPLIFICATION.
	for (int i = 0; i < size; i++)
	{
	    num[i] = rand() % 100 + 1;  
	}
}
void printArray(int num[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << ' ' << num[i];
	}
	cout << endl;
}
int countEvens(int num[], int size)
{
	int count = 0;
	for (int i = 0; i < size; i++)
	{
		if (num[i] % 2 == 0)
			count++;
	}
	return count;
}


Now if you want to decrease or increase the size of the array you only need to change the number once. For example if I change the const to 5 I get this output:
1
2
3
84 87 78 16 94

There are 4 even numbers in the array 


Thank you so much for the help. The program is working as intended.
Topic archived. No new replies allowed.