-4

Problem:1 In this code if I search a number which is not in array it should display Value not found but I don't know it's not displaying that message instead everytime it's showing Found value in element -5I don't have any clue why it's happening.

#include<stdio.h>
#define SIZE 100

size_t linearSearch(const int array[], int key, size_t size);

int main(void)
{

    int a[SIZE];
    size_t x;
    int searchKey;
    size_t element;


    for(x=0; x<SIZE; ++x){
        a[x] = 2*x;
    }

    for(x=0; x<SIZE; ++x){
        if(x%10 == 0){
            puts("");
        }
        printf("%5d", a[x]);
    }

    puts("\n\nEnter integer search key:");
    scanf("%d", &searchKey);

    // attempt to locate searchKey in array a
    element = linearSearch(a, searchKey, SIZE);

    // display results
    if(element != -1){
        printf("Found value in element %d", element);
    }
    else{
        puts("Value not found");
    }
}

size_t linearSearch(const int array[], int key, size_t size)
{
    if(size<0){
        return -1;
    }
    if(key == array[size-1]){
        return size-1;
    }
    return linearSearch(array, key, size-1);

}

Problem:2

I can't understood how

size_t linearSearch(const int array[], int key, size_t size)

function working specially these line

if(key == array[size-1]){
        return size-1;
return linearSearch(array, key, size-1);
Claudia
  • 33
  • 4
  • 4
    `if(size<0)` is stupid for an unsigned value –  Oct 03 '16 at 19:52
  • Looks like a good example to learn a debugger with. Use the debugger. It can show you values of variables as you step through each instruction. – Thomas Matthews Oct 03 '16 at 19:52
  • suggest me, what should I write instead of if(size<0) – Claudia Oct 03 '16 at 19:53
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 03 '16 at 19:56
  • ` if(size<0){ return -1;` made it even worse. Pick your C book and google and look up what `size_t` is and how much use comparing anm unsigned type with a signed value has. Also compiler warnings are not for fun, but sould be respected. Fix all warnings before asking. (And of course enable the recommended! Any modern compiler wil warn about this comparison. – too honest for this site Oct 03 '16 at 20:06
  • 1
    Just change `size<0` to `size==0`. – David G Oct 03 '16 at 20:26
  • @0x499602D2 0 is a valid position. – stark Oct 03 '16 at 20:49
  • 1
    @stark 0 is the size of the array not the position. `size-1` is the position. – David G Oct 03 '16 at 20:50
  • 1
    You also should change `size_t` to `int` if you want to return `-1`. – David G Oct 03 '16 at 20:54
  • http://ideone.com/iiJfjJ – BLUEPIXY Oct 03 '16 at 23:01
  • @0x499602D2 Thanks, but now If I enter a number which is in array list it shows "Found value in element zu" it doesnt print the element! – Claudia Oct 04 '16 at 01:04
  • If you are using the C99 previous compiler, Change `printf("Found value in element %zu", element);` to like `printf("Found value in element %u", (unsigned)element);` ( `%lu` : `(unsigned long)`, `%llu` : `(unsigned long long)`) – BLUEPIXY Oct 04 '16 at 01:33
  • if `sizeof(size_t)` is 4, use `%u` and `(unsigned)`. if `sizeof(size_t)` is 8, use `%llu` and `(unsigned long long)`. – BLUEPIXY Oct 04 '16 at 01:41
  • sizeof(size_t) returned 4 so I used %u and its worked. can anyone please explain me these line please? `if(key == array[size-1]){ return size-1; return linearSearch(array, key, size-1);` – Claudia Oct 04 '16 at 03:07
  • if key match the last element, returns its position. If not, repeat with a shorter one size. key not found if size is zero. – BLUEPIXY Oct 04 '16 at 03:13

3 Answers3

2

1) The main problem is if(size<0){. Conditional expression will always be false. because size_t is unsigned integer. So, it returns a random position with the values found(It's undefined behavior) by chance become a large numbers(e.g. -5 is 4294967291 as unsigned) without end(not found).

if(size<0){ should be if(size==0){

2) If key match the last element, returns its position. If not, repeat with a shorter one size. key not found if size is zero.

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
2

As everyone said that you have a little mistake that is, you should write if(size==0) not if(size<0).

let me explain what's going on recursively in linearSearch() function

size_t linearSearch(const int array[], int key, size_t size)
{
    if(size == 0){
        return -1;
    }
    else
        if(key == array[size-1]){
            return size-1;
    }
    else{
        return linearSearch(array, key, size-1);
    }
}

Suppose you gave an input 198 as searchkey. When you calling linearSearch() function by the statement

element = linearSearch(a, searchKey, SIZE);

you are passing reference to array[], searchKey 198, and Size 100 as argument.

In linearSearch function first if statement if(size==0) checks if the size is equal to zero if not then else if statement runs.

in else if statement If(198 == array[100-1]) condition is checked. and we see 198 is present in array[99] so the else if condition is true thus linearSearch function return the 99 as result.

Now lets see what happend if you input 55 which is not in the array list. if(size==0) is not true so program will skip it and will go to the next statement. if(55 == array[100-1] will be checked as its not true then linearSearch(array, 55, 100-1) will be called. again if(55==array[99-1]) will be checked. at some point size will become 0. and the first if(size==0) statement will execute.

Shateel
  • 146
  • 4
1

Just change the if statement from if(size<0) to if(size==0)your code will work.

Sairat
  • 45
  • 1
  • 6