Hi ! I need help , I should create an array and the user will enter the values , and display the array and it's values in another function , without using pointers .
so I need help , i don't know what should I write in "displayArray" function
Pretty sure you need to pass size as reference to readArray. Very sure that it should be constint size for displayArray.
@line 50, you can have the array size 6 (< not <=).
You print it in basically the same way that you read it:
1 2 3 4 5 6 7 8 9
if (size > 6)
{
cout << "Hey, you entered a large number and I didn't fix it yet!\n";
return;
}
for (int i = 0; i < size; i++)
cout << a[i] << " ";
cout << '\n';
Why did you declare the second parameter of function displayArray as a reference?!
I showed you already how the function is defined. Are you unable to copy and paste the code I showed?
One more for very advanced programmers:
1 2 3 4
void displayArray( constint a[], int size )
{
for ( int i = 0; i < size; i++ ) std::cout << a[i] << ' ';
}
Further if you defined the array as
int a[6];
then you shall use its size.
So instead of the definition
int a[6], size = 0;
shall be
1 2
constint SIZE = 6;
int a[SIZE];
And function readArray shall not ask about the array size. It is fixed and equal to 6. So the function shall ask to enter exactly 6 that is SIZE elements of the array.
Otherwise if the size of the array is not known then you shall allocate it in the heap and use the pointer to its first element.
I'm sorry but I used your way and it didn't work , i can't have an output if i choose '2'
and about the size it could be 6 or less , it's not fixed at 6
It is not the problem of the function I showed. It is your problem that you even can not read what other wrote.
Please reread one more my previous message. Function readArray shall be rewritten as I pointed out.
The problem is that you enter new size of the array but in main this size is unknown. It is a bad design of the program. I already showed you how function readArray can look.
And why did you declare int i outside the loop?
1 2 3 4 5 6 7 8
void displayArray( constint a[], int size )
{
int i;
for ( i = 0; i < size; i++)
cout << a[i] << ' ';
}
Are you unable simply copy and paste the code I showed?!