Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings Prev Up Next
\(\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 19.14 Practice: Passing Arrays to Functions
Check Your Understanding Check Your Understanding
1.
Suppose an array of integers has been declared using the declaration
int num[50];
Next,
x integers are stored in this array (where x is no larger than 50). The array is to be passed to a function sum which is to add up the x integers stored in the array. What should you be passing to the function sum?
The number x of integers stored in the array and a pointer to the first element of num.
Correct
A pointer to the first element of num.
Not quite. Try again!
num[0]
Not quite. Try again!
The 50 elements of num, stored as individual integers.
Not quite. Try again!
2.
Suppose a function sum has been defined as follows:
int sum(int n, int array[])
{
int i;
int result = 0;
for (i=0; i<n; i++)
result += array[i];
return(result);
}
Suppose furthermore that in your main function an array num has been declared and initialized with the following:
int num[]={4,2,7,5,8,9,4,2,4,6};
What is the correct function call of
sum(), using this array?
Feel free to use the window below to try out some code. Be sure to work the correct answer out "by hand" first before verifying your answer using the code window.
admin ..... open in new window
Correct
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!