Dynamic Arrays and functions

Hi guys,

This is the question I am attempting.

Write a program to prompt the user to enter the size of an array followed by that number of integers which are to be stored in the array. The program should then create a new array containing only the even numbers from the original input, and display its contents. The program should be implemented using 3 functions: read, getEven, print

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
//Task 1: AllEven
#include <iostream>
#include "Header.h"
using namespace std;
int main()
{
	read();

	system("PAUSE");
	return 0;
}

void read()
{
	int size = 0;
	int* arrSize = new int[size];
	cout << "\nEnter the size of the array: ";
	cin >> size;
	cin.ignore();
	
	getEven(arrSize, size);
}

void getEven(int r[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cin >> r[i];
	}

	cin.ignore();

	print(r, size);
	
}

void print(int z[], int size)
{
	cout << endl;

	for (int i = 0; i < size; i++)
	{
		cout << z[0] << endl;
	}
}


I have debugged the program and found that the problem is on line 33 as the print function is just returning the first element of the array and not the full array. How would I correct this?

I haven't implemented the even code yet, as I was wanting to get the base functions completed first with no errors.

Cheers
Last edited on
Updated 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
//Task 1: AllEven
#include <iostream>
#include "Header.h"
using namespace std;
int main()
{
	read();

	system("PAUSE");
	return 0;
}

void read()
{
	int size = 0;
	
	cout << "\nEnter the size of the array: ";
	cin >> size;

	int* arrSize = new int[size];
	
	cin.ignore();

	for (int i = 0; i < size; i++)
	{
		cin >> arrSize[i];
	}
	
	cin.ignore();

	getEven(arrSize, size);
}

void getEven(int r[], int s)
{
	int* even = new int [s];

	for (int i = 0; i < s; i++)
	{
		if (r[i] % 2)
		{
			
		}

		else
		{
			even[i] = r[i];
		}
	}

	print(even, s);
 
}

void print(int z[], int si)
{

	for (int i = 0; i < si; i++)
	{
		cout << z[0] << endl;
	}
	 
}


In the getEven function, it seems that the "even" and "r" array has only one element no matter the size. I don't understand why.

Also am I using cin.ignore() correctly and where would I delete[] the pointers?
Change
cout << z[0] << endl;
to
cout << z[i] << endl;
on line 60
As a side node, I see some memory leaks. You aren't deleting your arrays after allocating.
Make sure you call delete on those arrays after you're done with them to free those resources.
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
//Task 1: AllEven
#include <iostream>
#include "Header.h"
using namespace std;
int main()
{
	read();

	system("PAUSE");
	return 0;
}

void read()
{
	int size = 0, element = 0;
	
	cout << "\nEnter the size of the array: ";
	cin >> size;

	int* arrSize = new int[size];
	
	cin.ignore();

	for (int i = 0; i < size; i++)
	{
		element++;
		cout << "\nEnter element " << element << ": ";
		cin >> arrSize[i];
	}
	
	cin.ignore();

	getEven(arrSize, size);
	 
}

void getEven(int r[], int s)
{
	int* even = new int [s];

	for (int i = 0; i < s; i++)
	{
		if (r[i] % 2 == 0)
		{
			even[i] == r[i];
		}

		else
		{
			even[i] = 0;
		}
	}

	print(even, s);
	delete[] r;
	delete[] even;
}

void print(int z[], int si)
{
	cout << endl;

	for (int i = 0; i < si; i++)
	{
		cout << z[i] << endl;
	}
	
}


I am still having problems with my code if I enter an array of size 3 followed by 4, 5, 6....I get -842150451, 0, -842150451 which is obviously wrong
Bump
There's a compiler warning message for line 45
 
    even[i] == r[i];

[Warning] statement has no effect [-Wunused-value]

The operator == should be the assignment operator =.
Oh thanks, my compiler is visual studio 2015 and didn't display any error messages. Which compiler do you use?

Also did I use cin.ignore() correctly here on line 22 and line 31?
As it happens, I used MinGW (GCC) Compiler. But whichever compiler you use, it is a good idea to configure it to display as many warnings and errors as possible - I'm not sure how to do that with visual studio.

The use of cin.ignore() seems neither good nor bad here. It can certainly be useful when the program is using getline() as well,

The use of delete[] looks a bit unusual. Though it seems ok, it is probably better to match it more closely with the corresponding new []. Instead of delete[] r; at line 55, it might make more sense (it makes the code easier to follow) to instead put delete [] arrSize; at the end of function read() (about line 34).
Thanks alot!
Topic archived. No new replies allowed.