0

This is a linear search algorithm and I am a newbie programmer....Why is the "return i" statement not returning i (printing it on console)?? Is it because the computer considers this as "end the program successfully" because the value of i > 0 (I mean is it acting like "return 0" statement??) how to solve this issue??

#include <iostream>

int linearSearch(int n, int arr[], int num);

int main() {
    int n, num, arr[n];
    std::cout << "Enter Array Size: ";
    std::cin >> n;
    std::cout << "Enter Array: ";
    for(int i = 0; i < n; i++) {
        std::cin >> arr[i];
    }
    std::cout << "Enter Num to Search in Array: ";
    std::cin >> num;
    linearSearch(n, arr, num);
}


int linearSearch(int n, int arr[], int num) {
    for(int i = 0; i < n; i++) {
        if(arr[i] == num) {
            std::cout << "Found at Index: ";  
            return i; //this should return index i but program ends before printing i to console
        }
    }
    std::cout << "...Not Found...";
    return -1;
}
Raghav
  • 3
  • 2
  • 2
    `int n, num, arr[n];` What is the value of `n` when the array `arr` is created? – Some programmer dude Aug 24 '22 at 12:16
  • 1
    Besides, [variable-length arrays aren't part of C++](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Use a `std::vector` instead. – Some programmer dude Aug 24 '22 at 12:17
  • where do you print the index ? The function returns it only but does not print it and `main` calls the function but ignores the returned value – 463035818_is_not_an_ai Aug 24 '22 at 12:17
  • Trust me, `return` *works*. That is not your problem. – Jesper Juhl Aug 24 '22 at 12:42
  • 1
    It looks like you're confusing a function's "output" – i.e. returning a value to the caller of the function – with a program's "output" – i.e. printing a value to the console. They are completely unrelated. – molbdnilo Aug 24 '22 at 13:15

3 Answers3

2

You have to write std::cout << linearSearch(n, arr, num); as last line in your main function.

Heiko Vogel
  • 172
  • 6
  • That makes no sense at all. Why should this be done? – Some programmer dude Aug 24 '22 at 12:18
  • @MarkoBorković By using `std::cin`? And the programs ends before the function returns anyway which is because of a different error. – Some programmer dude Aug 24 '22 at 12:19
  • Yeah but judging from the question text he is expecting return to print it for him: `Why is the "return i" statement not returning i (printing it on console)`. So that should be clarified too. Also you are right, I see now he originally used `cin` I just kind autocorrected in my head. – Marko Borković Aug 24 '22 at 12:21
2

Why is the "return i" statement not returning i (printing it on console)??

Thats a misunderstanding common among beginners. Returning something from a function and printing something to the console are two different things.

Not every value you return from a function will be displayed on the console. If you use other languages with an interpreter you may be used that the result of all statements appears on the console. But you aren't running an interpreter.

In your function

return i;

is completely fine to return the value. It does work!

In your main

linearSearch(n, arr, num);

You call the function and ignore the returned value. If you want the value returned from the call to appear on the console you need to write code for that. For example:

int x = linearSearch(n,arr,num);
if (x != -1) std::cout << "index = " << x;
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

When you found the index, you forgot to print the output to the console:

if(arr[i] == num) {
    std::cout << "Found at Index: ";  
    return i; //this should return index i but program ends before printing i to console
}

And since you didn't instruct it to print, it doesn't print ;-)

This should do the job:

if(arr[i] == num) {
    std::cout << "Found at Index: " << i << std::endl;  
    return i; //this will return index i after printing it to the console
}

Btw, you can't allocate the neccessary memory for arr when you don't know the size.

int n, num, arr[n]; // you don't know the size
std::cout << "Enter Array Size: ";
std::cin >> n;

you need to re-arrange it and allocate dynamically once you know the size (and delete once it is no longer used):

int n, num;
std::cout << "Enter Array Size: ";
std::cin >> n;    
*arr = new int[n];

int main() {
    int n, num;
    std::cout << "Enter Array Size: ";
    std::cin >> n;
    *arr = new int[n];
...
    linearSearch(n, arr, num);
    delete arr;
}
chirp147
  • 11
  • 1