2014-12-07 1 views
0

현재 코드에서 프롬프트를 맨 위에 첨부했습니다. 나는 높은 값이나 낮은 값으로 힘든 시간을 보내고 있습니다. 나는 함수가 내 int main()보다는 오히려 호출되는 문제라고 믿지만 잘못 될 수있다. 가장 낮은 값은 기록되는 값이 아닙니다. 배열의 중간 또는 끝에 기본값이있는 것 같습니다 (6 월 또는 12 월은 내 Low로 출력되는 공통 월 임)배열 및 함수 호출로 카운트를 잃음

이 문제점을 해결할 수있는 방법에 대한 아이디어가 있습니까?

/* 
Write a program that lets the user enter the total 
rainfall for each of 12 months into an array of 
doubles. The program should calculate and display 
the total rainfall for the year, the average monthly 
rainfall, and the months wit hthe highest and lowest 
amounts. 

Input Validation: Do not accept negative numbers 
for monthly rainfall figures. 

*/ 

include <iostream> 
include <iomanip> 
include <string> 

using namespace std; 

//Function Prototypes 
double RainTotal(double[], int); 
double RainAverage(double[], int); 
double RainHighpoint(double[], int); 
double RainLowpoint(double[], int); 


int main() 

{ 
    int count;        //counting variable for loops 
    const int Size = 12; 

    double RainInput[Size];     //Numerical variables 
    int RAINHIGHEST, RAINLOWEST; 
    double RAINTOTAL, RAINAVERAGE; 
    string Month[] = { "January", "Febuary", "March", "April",   //Must list string names for each month 
     "May", "June", "July", "August", "September", "October", 
     "November", "December" }; 



    cout << "Please enter the average rainfall with the corresponding month\n\n"; 
    for (count = 0; count < Size; count++) 
    { 
     cout << Month[count] << " : ";         //User prompted to enter rainfall values 
     cin >> RainInput[count]; 
     while (RainInput < 0) 
     { 
      cout << "Please enter a positive number for " << Month[count] << endl; 
      cin >> RainInput[count]; 
     } 
    } 

    RAINTOTAL = RainTotal(RainInput, Size);        //Call Total Rainfall 
    RAINAVERAGE = RainAverage(RainInput, Size);       //Call Average Rainfall 

    string LowMonth, HighMonth;           //String value for given High/Low Months 
    double LowPoint, HighPoint;           //Values stored for highest and lowest rainfall 

    RAINLOWEST = RainLowpoint(RainInput, Size);       //Call Lowest Array Subscript Value 
    LowMonth = Month[RAINLOWEST]; 
    LowPoint = RainInput[RAINLOWEST]; 

    RAINHIGHEST = RainHighpoint(RainInput, Size);      //Call Highest Array Subscript Value 
    HighMonth = Month[RAINHIGHEST]; 
    HighPoint = RainInput[RAINHIGHEST]; 

    cout << endl << endl; 

    cout << "The Total Rainfall is: " << RAINTOTAL<<endl; 
    cout << "The Average Rainfall is: " << RAINAVERAGE << endl; 
    cout << LowMonth << " had the least rainfall all year with " << LowPoint << endl; 
    cout << HighMonth << " had the most rainfall all year with " << HighPoint << endl; 
    return 0; 
} 

double RainTotal(double RainInput[], int size) 
{ 
    double Total = 0; 
    for (int count = 0; count < size; count++)      //Find the Total Rainfall 
    { 
     Total += RainInput[count]; 
    } 
    return Total; 
} 

double RainAverage(double RainInput[], int size) 
{ 
    double Total = 0; 
    double Average; 
    for (int count = 0; count < size; count++)      //Find the Rainfall Average 
    { 
     Total += RainInput[count]; 
    } 
    Average = Total/size; 

    return Average; 
} 

double RainLowpoint(double RainInput[], int size) 
{ 
    double LowPoint = RainInput[0]; 
    int LowCount = 0; 

    for (int count = 0; count < size; count++)       //Find Lowest rainfall month through numerical comparison 
    { 
     if (RainInput[count] <= LowPoint) 
     { 
      LowCount = count; 
     } 
    } 
    return LowCount; 
} 

double RainHighpoint(double RainInput[], int size) 
{ 
    double HighPoint = RainInput[0]; 
    int HighCount = 0; 

    for (int count = 0; count < size; count++)       //Find Highest rainfall month through numerical comparison 
    { 
     if (RainInput[count] >= HighPoint) 
     { 
      HighCount = count; 
     } 
    } 
    return HighCount; 
} 
+2

에 오신 것을 환영합니다! 자신의 문제를 여전히 보여주는 [SSCE *] (http://sscce.org/)를 게시하십시오. 또한 코드 내에서 발생하는 상황과 해결 방법에 대해 자세히 설명하십시오. :) – Qix

답변

1

루프의 현재 최고점과 최저점을 업데이트하는 것을 잊었으므로 아래 두 가지 기능을 다시 작성하면 문제가 해결됩니다.

double RainLowpoint(double RainInput[], int size) 
{ 
    double LowPoint = RainInput[0]; 
    int LowCount = 0; 

    for (int count = 0; count < size; count++) //Find Lowest rainfall month through numerical comparison 
    { 
     if (RainInput[count] <= LowPoint) 
     { 
      LowCount = count; 
      LowPoint = RainInput[count]; // was missing 
     } 
    } 
    return LowCount; 
} 

double RainHighpoint(double RainInput[], int size) 
{ 
    double HighPoint = RainInput[0]; 
    int HighCount = 0; 

    for (int count = 0; count < size; count++) //Find Highest rainfall month through numerical comparison 
    { 
     if (RainInput[count] >= HighPoint) 
     { 
      HighCount = count; 
      HighPoint = RainInput[count]; // was missing 
     } 
    } 
    return HighCount; 
} 

당신은 또한 당신의 사용자 지정 코드를 삭제하고 표준 라이브러리 std::min_elementstd::max_element에서 < 알고리즘 >를 사용할 수 있습니다.

+0

루프를 초기화 할 때 이미 루프를 시작 했으므로 루프를 시작할 필요가 없습니다. 즉, "int count = 1"을 수행하여 최적화 할 수 있다고 생각합니다. – Amadeus

0

당신의 LowPoint과 HighPoint 변수를 업데이트하십시오 :

if (RainInput[count] <= LowPoint) 
{ 
     LowCount = count; 
     LowPoint = RainInput[count]; 
} 

...에 StackOverflow에

if (RainInput[count] >= HighPoint) 
{ 
     HighCount = count; 
     HighPoint = RainInput[count]; 
}