2013-11-10 2 views
0
가장 낮은 점수를 찾으려고

과 높은 내가 오류인수는 매개 변수 유형을 두 번 포인터

가 계속와 호환되지
argument of type "double" is incompatible with parameter of type "double*" 

코드 :

cout << "The lowest of the results = " << find_lowest(score[5]); 
cout << "The highest of the results = " << find_highest(score[5]); 

system("Pause"); 


} 

double find_highest(double a[]) 
{ 
double temp = 0; 
for(int i=0;i<5;i++) 
{ 
    if(a[i]>temp) 
     temp=a[i]; 
} 
return temp; 
} 

double find_lowest(double a[]) 
{ 
double temp = 100; 
for(int i=0;i<5;i++) 
{ 
    if(a[i]<temp) 
     temp=a[i]; 
} 
return temp; 
} 
+4

당신의 함수는'double' 배열을 기대합니다, 그러나 당신은 하나의'double' 요소만을 전달합니다. 'score [5]'는 "score'라는 배열의 원소 번호 5를 의미합니다. – jogojapan

+0

"감사의 말"은 질문 주제로서 절대적으로 의미가 없으며 검색 할 때이 사이트의 향후 사용자가 사용할 가치가 없습니다.귀하의 질문 내용과 관련이 있고 유용한 내용으로 [편집]하십시오. –

답변

1

@jogojapan가 지적한 바와 같이, 이러한

cout << "The lowest of the results = " << find_lowest(score[5]); 
    cout << "The highest of the results = " << find_highest(score[5]); 

로 변경해야합니다
cout << "The lowest of the results = " << find_lowest(score); 
    cout << "The highest of the results = " << find_highest(score); 

함수에는 double 요소가 아니라 double 배열이 필요합니다.

0

"double *"은 "double"유형의 변수에 대한 포인터입니다. 당신은

cout << "The lowest of the results = " << find_lowest(score[5]); 

를 작성하고 한 배열 score에서 여섯 번째 요소를 반환 "[5] 점수". 당신은 함수 인수로 사용하는 경우 C 및 C++ 배열의 포인터로 부패하고, 할 수에서
cout << "The lowest of the results = " << find_lowest(score); 

이 필요합니다. [5] 점수 배열의 위치에 5의 두 배 find_lowest 점수 double*에 붕괴 double a[] 기대,하지만 당신은 그것을 double 인 점수의 6 요소가 아닌 double*

0

점수를 통과하려는 (0 , 1,2,3,4,5). 당신이 점수 배열의 첫 번째 6 개 요소를 보내려면 이 뭔가를 시도 할 수 있습니다 :

find_lowest((int[]){score[0],score[1],score[2],score[3],score[4],score[5]}); 

당신의 기능을 좀 더 유연하게하기 위해 이런 일을 할 수있는 더 좋은 생각이 될 수 있지만, 당신은 사용되는 배열의 길이를 유지할 필요가있을 것이다. (하지만 6 원소 이상인지 확인하여 이미 그렇게하고있는 것 같다.)

double find_highest(double a[], size_t len) 
{ 
    double temp = 0; 
    for(size_t i=0;i<len;i++) 
    { 
     if(a[i]>temp) 
     temp=a[i]; 
    } 
    return temp; 
} 
0

문제는 함수의 매개 변수 목록에서 당신은 따라서 오류를 double[] 또는 double*을 지정한 반면 score[5] 유형의 두 배, 표현 find_lowest(score[5])입니다.

그래서 다음과 같은 수정합니다

또한
cout << "The lowest of the results = " << find_lowest(score); 
cout << "The highest of the results = " << find_highest(score); 

, 입력 배열이 같은 모든 음수가 있다면 몇 가지 버그가, 당신의 find_highest() 함수가 A [] = {- 1.0, -2.0, - 3.5, -5.0, -1.3} 다음 기능은 return 0가 잘못되는 올바른 구현 될 것입니다 :

double find_highest(double a[]) 
{ 
double temp = a[0]; 
for(int i=0;i<5;i++) 
{ 
    if(a[i]>temp) 
    temp=a[i]; 
} 
return temp; 
} 

이 유사 find_lowest() 기능이 있어야한다 :

double find_lowest(double a[]) 
{ 
double temp = a[0]; 
for(int i=0;i<5;i++) 
{ 
    if(a[i]<temp) 
    temp=a[i]; 
} 
return temp; 
} 
관련 문제