Output and correction | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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); }

3rd Sep 2018, 11:26 AM
Ayush Verma
Ayush Verma - avatar
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 :)
3rd Sep 2018, 11:49 AM
Matthias
Matthias - avatar
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; }
4th Sep 2018, 1:05 PM
Ayush Verma
Ayush Verma - avatar