I'm using a function to find all the even numbers from an array, ranging from 0-50. Without using a function, it's very simpple. However, using size = 50; I'm not getting the expected outcome.
#include <iostream>
usingnamespace std;
void arrayFill(int evenArray[], int size);
int main()
{
int size = 50;
int evenArray[size];
arrayFill(evenArray, size);
return 0;
}
void arrayFill(int evenArray[], int size)
{
for (int i = 0; i <= size; i = i + 2)
{
cout << evenArray[i] << endl;
}
}
You don't initialize your array so it just contains random numbers.
You access your array out of bounds for (int i = 0; i <= size; i = i + 2).
The last element is size - 1