This sounds like a homework/lab program in a first programming course.
I don't know if you've learned about pointers, so I won't go into that. I didn't learn about pointers until a long time after I learned about arrays.
An array is a bunch of variables (of the same type) stored in (essentially) one variable, but with indexes. It's a little more complicated than that, but that's fine for now.
The function you're passing it to NEEDS to know that what you're sending it is an array. So, we can't just pass it like a normal variable - we have to include the "[]" when we pass the array to the function.
#include <iostream>
usingnamespace std;
// Function definition.
void printArray(int someArray[], int size){
for(int i = 0; i < size; i++){
// Print out the value held by the element corresponding to i.
cout << someArray[i] << endl;
}
}
int main()
{
int myArray[10]; // Remember to assign values to each element.
printArray(myArray, 10); // Pass the array to the function and the size of the array.
return 0;
}
I didn't assign values to it. So whatever it prints out is undefined.
Try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
// Function definition.
void printArray(int someArray[], int size){
for(int i = 0; i < size; i++){
// Print out the value held by the element corresponding to i.
cout << someArray[i] << endl;
}
}
int main()
{
int myArray[10] = {0, 1, 2, 4, 5, 6 , 7, 8, 9}; // Remember to assign values to each element.
printArray(myArray, 10); // Pass the array to the function and the size of the array.
return 0;
}