Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kimberly Hedger
1,308 PointsURGENT
the cdde is:
#include <stdio.h>
int sumArray(int [], int); // function prototype
int main(void) {
int foo[8] = {44, 9, 17, 1, -4, 22};
int bar[] = {2, 8, 6};
printf("sum is %d\n", sumArray(foo, 8));
printf("sum is %d\n", sumArray(foo, 3));
printf("sum is %d\n", sumArray(bar, 3));
return 0;
}
int sumArray(int arr[], int size) {
int i, total=0;
for (i=0; i<size; i++)
total += arr[i];
return total;
}
and outputs are: sum is 89 sum is 70 sum is 16
I cant understand from where these outputs came -_- can anyone please explain?
1 Answer
Sean T. Unwin
28,690 Pointsfoo is an Array of integers up to a max-length of 8. There are 6.
bar is an Array of integers of any length. There are 3.
The second parameter of sumArray is the total amount of items (integers) to use from the given Array in the first parameter.
printf("sum is %d\n", sumArray(foo, 8)); -- This adds up the numbers in foo using up to 8 numbers in the Array. Again, there is only 6 in the array so it's just adds zero twice to equal 8.
printf("sum is %d\n", sumArray(foo, 3)); -- This adds up the numbers in foo using the first 3 items in the Array.
printf("sum is %d\n", sumArray(bar, 3)); -- This adds up the numbers in bar using up to 3 numbers in the Array.