I think you're a little confused about how arrays work.
For arrays, you create it with a set number of memory locations BUT that is different than it's actual size.
For example:
int array[25];
This creates an array of integers for at least 25 memory locations.
But I could only read data into only one memory location or up to 25.
Creating an array does not automatically determine the size of the array but simply allocates memory.
The size of the array is determined by input.
Also, another way of creating an array is declaring a variable as an integer constant and using that named variable.
For example:
1 2 3 4 5
|
const int MAX=25;
int main()
{
int array[MAX];
|
Constant declarations are made outside of the main function.
They are global to your whole program.
Now let's look at your new program.
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
|
#include <iostream>
using namespace std;
const int size =5; // Constant declarations are made before main()
void assignValuesToArray(int [], int); // Created a void function that passes an array and the size of the array.
int main()
{
int a[size]; // Create an array of integers called 'a' for 5 memory locations. This is the same as "int a[5];". However 5 is not the size of the array, it is simply the amount of memory locations that it will write to!
assignValuesToArray(a, size); // Function call that sends the array and should send the size. However size is 5. So this doesn't really make sense.
for(int i=0; i<size; i++) //
cout << a[i] << " " << endl;
return 0;
}
void assignValuesToArray(int array[], int SIZE) // "SIZE" and "size" are different name is C++; lower case and upper case matters.
{
cout << "Enter five values to assign to indexed variables:\n";
for(int j=0; j<SIZE ; j++)
cin >> array[j]; // replace "SIZE" with "j" .
}
|
The values read into the assignvaluestoarray are local to that function, but if were to pass by reference then you can use them again in your main function.
For more information, read this:
http://www.docdroid.net/117vu/arrays-ch8.pdf.html