in Algorithms edited by
890 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?

in Algorithms edited by
890 views

4 Comments

code wasnt coming during the time of exam , plz solve the issue :)
1
1
Fixed now πŸ‘ Please click on β€œflag” button to report any such issues as comments can get lost here.
1
1
Oh ok wasn't aware thanks sir
0
0

1 Answer

4 votes
4 votes
Best answer
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