+ 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); }
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.
+ 2
In short
there is problem on get length of that array
So we need to use "size"
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.