Tuesday, July 14, 2009

Write a C program for the implementation of Linear Search using pointers.?

Linear search is basically looking through an array of values sequentially, stopping when you find what you're looking for, or the end of the array.





Some pseudocode that should do what you're asking:





- create or get an array of values (what type?)


- declare a pointer variable of the same type as array, call it 'curpos' (for 'current position')


- initialize 'curpos' to point to the first value in the array


- while 'curpos' doesn't point to just past the end of the array


---- compare the value pointed to by 'curpos' with the value you're looking for


---- if they're equal, you're done


---- otherwise, move curpos to the next position in the array





- if you get to just past the end of the array and haven't found what you're looking for, signal an error


No comments:

Post a Comment