2014-03-19 2 views
0

근본적으로 문제는 findLowest 함수가 내가 계획 한 일을하지 않는다는 것입니다. 나는 그것이 논리 오류라는 것을 알고 있지만 변수가 업데이트되지 않는 이유를 알 수없는 것 같습니다. 항상 기본값으로 인스턴스화됩니다.for 루프 용 논리 흐름

#include <stdafx.h> 
#include <string> 
#include <iostream> 

using namespace std; 

//Function prototypes 
void findLowest(int regAccidents[], int arraySize, string regNames[]); 
int getNumAccidents(string); 
const int NUM_REGIONS = 5; 

int main(){ 
    string regionNames[NUM_REGIONS] = { "North", "South", "East", "West", "Central" }; 
    int regionAccidents[NUM_REGIONS]; 

    for (int i = 0; i < NUM_REGIONS; i++){//Populates the accident array 
     regionAccidents[i] = getNumAccidents(regionNames[i]); 

     if (regionAccidents[i] < 0)//checks to see if there are any accidents counts lower than 0 
      regionAccidents[i] = 0; 
     } 

     findLowest(regionAccidents, NUM_REGIONS, regionNames); 
} 

int getNumAccidents(string region){//returns the accidents for a specific region to be assigned to the regionAccidents[] array in main() 

    int regionAccidents = 0; 
    cout << "How many accidents for " << region << "? "; 
    cin >> regionAccidents; 
    return regionAccidents; 

} 

void findLowest(int regAccidents[], int arraySize, string regNames[]){ //used to determine what the lowest number of accidents is 
    int lowest = regAccidents[0];          //once that is found, update the lowRegion string of the accident location 
    string lowRegion = regNames[0]; 
    for (int i = 0; i < arraySize; i++){ 
     if (regAccidents[i] < regAccidents[i++]){ 
      lowest = regAccidents[i]; 
      lowRegion = regNames[i]; 
     } 
    } 

    cout << "The region with the lowest amount of accidents is: " << lowRegion << endl; 
    cout << "The lowest number of accidents is: " << lowest << endl; 
} 
+0

이이 문제를 보여줍니다 코드의 가능한 가장 작은 조각인가? 그렇지 않다면 무엇입니까? – ObscureRobot

+0

아래쪽에는 findLowest 함수 만 있습니다. 사람들이 필요하다면 원래 프로그램에 대해 아무 것도 변경할 필요가 없기 때문에 내가 제공 한 전체 코드. – koodeta

+0

자세한 정보를 제공해야합니다. 어떤 입력을 사용 했습니까? 이러한 입력을 감안할 때 결과는 무엇입니까? 어떤 예외? 어떤 컴파일러와 플랫폼을 사용하고 있습니까? 많은 정보를 놓치 셨습니다 –

답변

1
if (regAccidents[i] < regAccidents[i++]) { ... 

, 당신은 지금까지 발견 된 가장 낮은 현재의 인덱스를 비교할 죽은 잘못 필요가있다 배열의 동일한 항목을 비교하기 때문에 참이어야합니다. 그것은 , 그러나, 당신이 적어도 그것을 기대할 때 i 증가하십시오. 기본 문제로 인해 완전히 숨겨진 보조 문제이기 때문에 여기에 실제로 중요하지 않습니다 .-)

+0

당신은 완전히 정확합니다. 나는 그것을 놓쳤다는 것을 믿을 수 없다. – koodeta

0

예, 코드에 논리 오류가 있습니다. findLowest은 다음과 같아야를

if (regAccidents[i] < lowest) { ... 

표현 regAccidents[i] < regAccidents[i++] 것이다 결코 :

int lowest = regAccidents[0];  
string lowRegion = regNames[0]; 
for (int i = 1; i < arraySize; i++){ 
     //^^you have initialized lowest as regAccidents[0], so search from 1 
    if (regAccidents[i] < lowest){ 
     //^^if current is smaller than lowest, update lowest and name  
     lowest = regAccidents[i]; 
     lowRegion = regNames[i]; 
    } 
} 
+0

당신은 완전히 정확합니다. 나는 그것을 놓쳤다는 것을 믿을 수 없다. – koodeta

0

당신은 지금 당신의 결과물에 대해 아무 말도하지 않았습니다. 이 줄의 증가분으로 배열을 오버런하고 있습니까?

경우 (regAccidents [I] < regAccidents [I ++])

+0

당신은 지금 당신의 결과물에 대해 아무 말도하지 않았습니다. 이 줄의 증가분으로 배열을 오버런하고 있습니까? if (regAccidents [i] koodeta