c++ - Why this program sometimes doesn't display the output, and sometimes yes? (Rainfall program) -
c++ - Why this program sometimes doesn't display the output, and sometimes yes? (Rainfall program) -
i finished working on programme calculates total, average, highest , lowest rainfall during year. programme requires user type rainfall each month, , create calculations. when lowest , highest numbers, programme should display month corresponding numbers, not number itself.
my programme runs fine , don't error @ all. however, notice sometimes, depending on output, 1 of these 3 cases:
the programme displays month highest number, doesn't display month lowest number. the programme displays month lowest number, doesn't display month highest number. the programme displays without problemsthe code following:
#include<iostream> #include<string> using namespace std; int main() { //array containing months string months[12] = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }; //variables double data[12]; double sum = 0; double avg, max1, min1; string max2, min2; (int = 0; < 12; i++)//asks user input each month { cout << "enter rainfall " << months[i] << ": "; cin >> data[i]; while (data[i] < 0) //input validation { cout << "invalid info (negative rainfall) -- retry"; cin >> data[i]; } } (int j = 0; j < 12; j++) //the sum of of numbers { sum += data[j]; } max1 = data[0]; (int k = 0; k < 12; k++) //calculates biggest number , month { if (data[k] > max1) { max1 = data[k]; max2 = months[k]; } } min1 = data[0]; (int l = 0; l < 12; l++) //calculates to the lowest degree number , month { if (data[l] < min1) { min1 = data[l]; min2 = months[l]; } } avg = sum / 12; //output cout << "total rainfall: " << sum << endl; cout << "average rainfall: " << avg << endl; cout << "least rainfall in " << min2 << endl; cout << "most rainfall in " << max2 << endl; homecoming 0; }
example of output:
i don't know why happening. if help me reply question , how can prepare error.
if need original problem text, leave here well. give thanks you!
rainfall statistics
write programme lets user come in total rainfall each of 12 months (starting january) array of doubles . programme should calculate , display (in order):
the total rainfall year, average monthly rainfall, , months highest , lowest amounts.
months should expressed english language names months in gregorian calendar, i.e.: january, february, march, april, may, june, july, august, september, october, november, december.
input validation: not take negative numbers monthly rainfall figures. when negative value entered, programme outputs "invalid info (negative rainfall) -- retry" , attempts reread value .
prompts , output labels: decimal values should displayed using default precision, i.e. not specify precision. each item read should prompted string of form "enter rainfall month:" month "january" or "february" or ... or "december". output should of form:
total rainfall: 12.36 total rainfall: 1.03 to the lowest degree rainfall in august rainfall in apr
where specific amount of rainfall or specific months identified depend on actual input.
the problem never set strings maximum or minimum months if next conditions never met:
if (data[k] > max1) //... if (data[l] < min1)
you can either set max2
, min2
first item before come in loop, or find index of largest , smallest values, , utilize index tell month take in month array.
int maxindex = 0; max1 = data[0]; (int k = 0; k < 12; k++) { if (data[k] > max1) maxindex = k; } int minindex = 0; min1 = data[0]; (int l = 0; l < 12; l++) { if (data[l] < min1) minindex = l; } max2 = months[maxindex]; min2 = months[minindex];
c++ visual-studio-2013
Comments
Post a Comment