我一直在尝试制作线性搜索程序,但未返回所需的值 该功能在
下#include<iostream>
using namespace std;
void input (int arr[], int x , int n);
int search(int arr[], int x , int n );
// main function
int main(){
int arr[]={0};
int x;
int n;
input(arr ,x ,n) ;
search (arr,x ,n) ;
}
// input function
void input (int arr[], int x , int n) {
cout<<"inter the size of array \n";
cin>>n;
for(int i = 0 ; i< n ; i++){
cout<<"Enter your element in index number "<<i<<endl;
cin>>arr[i];
}
cout <<"enter the number you want to find \n";
cin>>x;
}
// search function
int search(int arr[], int x , int n ){
int a=0;
bool b = true;
for(int i = 0 ; i<n ; i++){
if(arr[i] ==x ) {
cout<<"element "<<x<<" was found at "<<i<<endl;
b = false;
}
}
if(b){
cout<<"Number not found \n";
}
}
这是完整程序的链接:https://codeshare.io/axEODM