음수에 대해 배열을 검사 한 다음 값을 반환하는 함수를 만들었습니다. 첫 번째 매개 변수는 int testArray[]
이고 배열 크기는 int n=14
입니다. 배열을 사용하기 위해 for 루프를 사용했습니다. if
문을 사용하여 testArray[i]<0
을 비교하고 else
문을 사용하여 음수가 발견되지 않았다는 메시지를 출력합니다. 코드는 오류없이 컴파일되지만 출력이 없습니다.함수의 흐름 제어에 대한 경고
In function 'int countNegative(int*, int)': 28:1: warning: control reaches end of non-void function [-Wreturn-type]
나는 그것이 매개 변수가 함수에 전달되는 방식에 문제가 될 수 있다고 생각 : 나는 경고를 얻을.
#include <iostream>
#include <cstdlib>
using namespace std;
int countNegative(int testArray[],int n);
int main(){
int testArray[] = {-2,0,44,12,-45,17,934,-21,67,88,91,1,0,6};
int n = 14;
countNegative(testArray,n);
system("PAUSE");
//EXIT_SUCCESS;
return 0;
}
int countNegative(int testArray[],int n){
for(int i=0; i<n; i++){
if(testArray[i]<0){
int index = testArray[i];
return index;
}
else{
cout << "No Negative Numbers";
}
}
}