Advertisements
Advertisements
प्रश्न
The following code may contain some errors. Identify the errors, modify the program by underlining the changes made, and write the aim of the program.
int RA[ ] = {42, 51, 75, 95, 86, 84, 62};
boolean fnLinearSearch(int val)
{
for(int p = RA.length;p >=0; p--)
{
if(RA[p] = val)
{
break;
}
}
if(p >= 0)
return true;
return false;
}कोड लेखन
Advertisements
उत्तर
Syntax Error: p = RA.length will cause an error: array index out of bounds.
Syntax Error: p has been used outside the loop so it cannot be the loop counter
int RA[ ] = {42, 51, 75, 95, 86, 84, 62};
boolean fnLinearSearch(int val)
{
int p = 0;
for(p = RA.length - 1; p>=0; p--)
{
if(RA[p]= val)
{
break;
}
}
if(p >=0)
return true;
return false;
}
Aim: The aim of the programme is to search for an element in an array using linear search.
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 8: Arrays - Sort & Search - Exercises [पृष्ठ १९९]
