Question: Question 1 The following function is supposed to return true if any element of the array a has the value 0 and false if all

Question 1 The following function is supposed to return true if any element of the array a has the value 0 and false if all elements are non-zeros. Sadly, it contains an error. Find the errors and show how to fix it. bool has_zero (int a[], int n) { int i; for (i = 0; i < n; i++) if(a[i] == 0) return true; else return false; Question 2 The following function finds the median of three numbers. Rewrite the function so that it has just one return statement. double median (double x, double y, double z) { if (x <= y) if (y <= z) return y; else if (x <= z) return z; else return x; if (z <= y) return y; if (x <= z) return x; return z; }

Question 3 Show the output produced by each of the following programs. (a) #include int foo(int n) { if (n == 2 || n == 3) return n; else return (foo(n+1) + foo(n+2)); } int main() { int n = 3, i = 0; for(i; i<=n; i++) printf("%d ", foo(i)); return 0; } b) #define SIZE 10 int whatIsThis( int b[], int p ) { if ( 1 == p ) return b[ 0 ]; else return b[ p - 1 ] + whatIsThis( b, p - 1 ); } int main( void ) { int x; int a[ SIZE ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; x = whatIsThis( a, SIZE ); printf( "Result is %d ", x ); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!