edited by
874 views
3 votes
3 votes

Consider the following piece of code:

int function(int a[], int n, int x)
{
    int i;
    for (i=0; i<n && a[i]!=x;i++);
    if (i==n) return -1;
    else return i;
}

A function call is made with the arguments as follows:

  • $a[]=\{5, 32, 1, 9, 7, 2\}$
  • $n=6$
  • $x=8$

What will be the value returned by the above code?

edited by

1 Answer

Best answer
4 votes
4 votes
Seeing carefully at the end of for loop ; is used. So loop will terminate increasing value of i at 6.

And given condition if(i == n) will become true.

So this will return value -1.
selected by
Answer:

Related questions

0 votes
0 votes
2 answers
1
7 votes
7 votes
2 answers
2
Ruturaj Mohanty asked Dec 27, 2018
1,292 views
The remainder when $'m+n'$ is divided by $12$ is $8$, and the remainder when $'m-n'$ is divided by $12$ is $6$. If $m>n$, then what is the remainder when $'mn'$ is divide...
8 votes
8 votes
2 answers
3
Ruturaj Mohanty asked Dec 27, 2018
1,269 views
For a given $m$-ary tree, the relationship between leaf nodes and internal nodes is represented by the graph given below. What is the value of $'m'$? Take necessary appro...