0
Passing an array of varying to a function and iterating through it?
Can anyone tell me why this code is throwing an unknown compile error? I want to learn how to iterate through arrays with a function, albeit somewhat technical for C++ and the resources on the internet seem to be highly varied. #include <iostream> using namespace std; // create a funciton which calculates the sum of the values within a given array int multiplyArr(int arr1[], int size){ int sum = 0; for (int i = 0; i < size; i++){ sum += arr1[i]; } return sum; } // create the array and output the return value of the function int main() { int vals_counter = 4; int vals[] = {5,6,8,10}; cout >> multiplyArr(vals, vals_counter); return 0; }
3 odpowiedzi
+ 2
You have a syntax error in main, cout >> should be cout <<
0
Thanks Jojo, I'm guessing 'size_t' is in the standard library?
- 2
Just a little thing, try to use type 'size_t' for 'vals_counters'.
'size_t' is a type for variables that contain the size of an array ('size_t' is very similar to 'int' but if you have good habits, you'll code good code).