简体   繁体   English

c++ - 如何将函数的返回值分配给变量?

[英]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.我想将函数的返回值分配给 C++ 中的变量,但程序退出时没有任何输出。

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 .我能够在您的程序屏幕截图中看到的返回代码是3221225725 This return code means that your computer ran out of stack memory before the recursion limit was reached.此返回码意味着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.所以我认为在你的函数binarySearch(arr,num,searchNumber) ,你在某处递归调用你的函数。 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二分查找c++函数的正确实现是

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.取num的输入值后,需要用变量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;
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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