2015-09-04 18 views
-2

我有两个关于指针的问题,我试图自己解决它们,但我不能。首先是“使用指针在数组中查找更大更小的数字?”。第二个是“使用带指针的线性搜索算法在整数数组中查找给定数字?” 我不知道我应该把指针我怎样才能找到使用指针和班轮搜索数组中的更大和最小数量?

的第一个这是我的尝试:

#include<stdio.h> 
main() 
{ 
    int arr[]={8,9,5,23,12}; 
    int min; 
    int i; 
    int *arr_pt; 
    int big; 
    min = arr[];  
    for(i=0;i<=5;i++) 
    { 
     if(arr[i]<min) 
      min = arr[i]; 
    } 
} 


#include<stdio.h> 
main() 
{ 
    int i; 
    int y; 
    int arr[]; 
    int found; 
    for(i=0;i<5;i++){ 
     if(arr[]==y) 
      found=1; 
     if(found==1) 
      printf("%d is found",y);   
    } 
}    
+2

你有没有用'}'关闭范围?缩进同样令人伤心。 – LogicStuff

+0

我不想粗鲁,但这看起来像是你在编程课上交给你的任务。也许最好向你的老师解释一下?与等待某人在银盘上呈现正确答案相比,您将获得更多收益。也许读一下指针?您可以使用Google或任何其他搜索引擎来查找信息。例如:http://www.tutorialspoint.com/cplusplus/cpp_array_of_pointers.htm – Wim

+0

谢谢你的链接。是的,这是一项任务。我试图自己解决,然后我问老师,他给了这个代码。他说这会帮助你。 – Moayad

回答

3
#include <algorithm> 

auto minmax = std::minmax_element(std::begin(values), std::end(values)); 

std::cout << "min element " << *(minmax.first) << "\n"; 
std::cout << "max element " << *(minmax.second) << "\n"; 

复杂性:

最多MAX(地板(3/2(N-1)),0)谓词的应用,其中N = std :: distance(first,last)。

C++很漂亮;)

+0

不错。不知道他们将这一点添加到C++ 11中。将其添加到我的技巧列表中。 – NathanOliver

+0

谢谢你的帮助兄弟。 – Moayad

+0

@Moayad不要忘记标记为已解决;)没有问题! – CyberGuy

相关问题