+ 1

Why x<size is used & explain how the program works

#include <iostream> using namespace std; void printArray(int arr[], int size) { for(int x=0; x<size; x++) { cout <<arr[x]<< endl; } } int main() { int myArr[3]= {42, 33, 88}; printArray(myArr, 3); }

19th Jul 2017, 12:57 PM
Rahul.R
Rahul.R - avatar
3 odpowiedzi
+ 2
in c / c++ array length is not specific, if we go beyond its range it does not give any error. thus the given function printArray() needs the size of array.
19th Jul 2017, 1:14 PM
Ashwaghosh Sadanshiv
Ashwaghosh Sadanshiv - avatar
+ 2
In short there is problem on get length of that array So we need to use "size"
19th Jul 2017, 1:20 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
0
This isn't the best code. Int size seems to be the number of values in the array you are testing; however, it would be better if the function dynamically detected how many values there were, perhaps using ARRAY_SIZE(). x<size is used here to tell the loop to stop at the last array key--so the loop prints each array value on a new line.
19th Jul 2017, 1:13 PM
James
James - avatar