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
//Task 1: AllEven
#include <iostream>
#include "Header.h"
usingnamespace std;
int main()
{
read();
system("PAUSE");
return 0;
}
void read()
{
int size = 0;
int* arrSize = newint[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.
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.
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).