in Algorithms edited by
530 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
530 views

6 Comments

what function is actually performed by above code??? is it returns 5??
0
0
Normal linear search operation is being performed. if x is not present in the array then it returns -1. So simple!!!
0
0

 

Please fix, the code segment is not properly showing up in the test.

0
0
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