+ 1
Output and correction
void printArray(int arr[5], int size) { for(int x=0; x<size; x++) { cout <<arr[x]; } } int main() { printArray([1, 2, 3, 4, 5], 5); }
2 Answers
+ 2
Arrays in C++ are enclosed in curly braces {}
printArray({1,2,3,4,5},5)
does not work however.
You need to specify the variable type beforehand like this:
int a[] = {1,2,3,4,5};
printArray(a,5);
Then it works.
You could also cchange write the function in different ways:
void printArray(int arr[], int size)
since you send the size anyway and brackets without the number allow arrays of any size
or also
void printArray(int* arr, int size)
which is basically the same (pointer to first array position)
I hope it helps :)
0
#include <iostream>
using namespace std;
void array(int a[],int size)
{
int i;
for(i=0;i<size;i++)
{
cout<<a[i]<<endl;
}
}
int main()
int a[]={1,2,3,4,5};
void array(a,5);
return 0;
}