简体   繁体   中英

How to assign return value of a function into a variable in c++?

i want to assign a returned value of a function into a Variable in C++, but the program is exit without any output.

int numberPosition = binarySearch(arr, num, searchNumber);

program output screenshot

Full code---

#include <iostream>
using namespace std;


int binarySearch(int arr[], int num, int searchNumber){
    int start=0, end=num;

    while(start<=end){
        int mid = (start+end) / 2;
        if(arr[mid] == searchNumber){
            return mid;
        }else if(arr[mid] <= searchNumber){
            start = mid+1;
        }else{
            end = mid-1;
        }
    }
    
    return -1;
}

int main(){
    
    int num, arr[num], searchNumber;
    cout<<"How many Elements u want to insert? \n";
    
    cin>>num;
    
    cout<<"\n Enter ut Numbers:- \n";
    for(int i=0; i<num; i++){
        cin>>arr[i];
    }
    
    cout<<"\n Enter the Number u want to search:- ";
    cin>>searchNumber;
    
    int numberPosition = binarySearch(arr, num, searchNumber);
    
    if(numberPosition){
        cout<<searchNumber<<" is founded at position: "<<numberPosition;
    }else{
        cout<<searchNumber<<" is Not Founded";
    }
    
    return 0;
}   

The return code I am able to see in your program screenshot is 3221225725 . This return code means that your computer ran out of stack memory before the recursion limit was reached. .

So, you still have infinite recursion somewhere in your code.

So I think in your function binarySearch(arr,num,searchNumber) , You are recursively calling your function somewhere. As you didn't post the code of your function I can't help you on that where you made the mistake.

So make sure your Implementation is correct. The correct implementation of the binary search c++ function is

int binarySearch(int arr[], int l, int r, int x) 
{ 
    if (r >= l) { 
        int mid = l + (r - l) / 2; 

        if (arr[mid] == x) 
            return mid; 

        if (arr[mid] > x) 
            return binarySearch(arr, l, mid - 1, x); 

        return binarySearch(arr, mid + 1, r, x); 
    } 

    return -1; 
} 

Here This code works. You need to declare the length of array with variable num after you have taken the input value of num.

#include <iostream>
using namespace std;


int binarySearch(int arr[], int num, int searchNumber){
int start=0, end=num;

while(start<=end){
    int mid = (start+end) / 2;
    if(arr[mid] == searchNumber){
        return mid;
    }else if(arr[mid] <= searchNumber){
        start = mid+1;
    }else{
        end = mid-1;
    }
}

return -1;
}

int main(){

int num, searchNumber;
cout<<"How many Elements u want to insert? \n";

cin>>num;
int arr[num];
cout<<"\n Enter ut Numbers:- \n";
for(int i=0; i<num; i++){
    cin>>arr[i];
}

cout<<"\n Enter the Number u want to search:- ";
cin>>searchNumber;

int numberPosition = binarySearch(arr, num, searchNumber);

if(numberPosition!=-1){
    cout<<searchNumber<<" is founded at position: "<<numberPosition;
}else{
    cout<<searchNumber<<" is Not Founded";
}

return 0;
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM