Which of the following functions returns the largest value in the array X? int b
ID: 3918949 • Letter: W
Question
Which of the following functions returns the largest value in the array X?
int big(int X[ ], int arraySize) {
int large = X[arraySize];
for ( int i = 0; i< arraySize; i++)
if (X[i] > large)
large = X[i];
return large;
}
int big(int X[ ], int arraySize) {
int large = X[0];
for ( int i = 0; i< arraySize; i++)
if (X[i] > large)
large = X[i];
return large;
}
int big(int X[ ], int arraySize) {
int large = X[0];
for ( int i = 0; i< arraySize; i++)
if (X[i] < large)
large = X[i];
return large;
}
int big(int X[ ], int arraySize) {
int large = X[0];
for ( int i = 0; i<= arraySize; i++)
if (X[i] > large)
large = X[i];
return large;
}
int big(int X[ ], int arraySize) {
int large = X[arraySize];
for ( int i = 0; i< arraySize; i++)
if (X[i] > large)
large = X[i];
return large;
}
Explanation / Answer
Answer : (B) ( Considering A ,B,C,D from top to bottom. )
The function A contains int large = X[arraySize]; , which is wrong as array indies lies between 0 to (arraySize -1 )
The Function C contains wrong logic
if (X[i] < large)
large = X[i];
If X[i] < large than large cannot be equal to X[i]
The function D contains array out of bound error as the loop runs until I = arraySize instead of arraySize-1
Please Up vote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.