c++ - How do I add in decimals to my current working code without butchering it? -
c++ - How do I add in decimals to my current working code without butchering it? -
i have been cracking @ code day , when think making ground seem take 1 step forwards , 2 steps back.
i using visual studios 2013 update 4 , wondering if help me incorporate beingness able utilize decimals output calculations in working code have completed far.
i have tried using double variable, throw 1120 errors or nil @ all.
any help appreciated.
// chapter 7 problem #3 // rainfall statistics // 4-7-15 #include <iostream> #include <iomanip> #include <string> using namespace std; // functions prototypes void getrain(string[], int[], int); int gettotal(int[], int); int getave(int[], int); int largestelement(int[], int); int smallestelement(int[], int); int main() { // costant number of month entries const int rain_per_month = 12; //array of months string names[rain_per_month] = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "decemeber" }; // array of inches of each month int inches[rain_per_month]; //total inches of rainfall int totalrainfall; // ave rain fall int averainfall; // subscript of month had rain int highrainfall; // subscript of month had to the lowest degree rain int lowrainfall; // amount of rainfall in inches of each month. getrain(names, inches, rain_per_month); // total rainfall , ave rainfall , high rainfall , low rainfall in inches. totalrainfall = gettotal(inches, rain_per_month); averainfall = getave(inches, rain_per_month); highrainfall = largestelement(inches, rain_per_month); lowrainfall = smallestelement(inches, rain_per_month); // display rainfall study header. cout << endl << endl; cout << " 12 month rain study \n\n"; cout << "______________________________________________________________\n"; // display , rain in inches of each month. (int rain_per_month = 0.00; rain_per_month < rain_per_month; rain_per_month++) { cout << setw(6) << names[rain_per_month] << setw(21) << inches[rain_per_month] << endl; } // display total rainfall, highest rainfall, , lowest rainfall. cout << "\nthe total rainfall year was:" << setw(15) << totalrainfall << " inches" << endl; cout << "the average rainfall year " << averainfall << endl; cout << "the largest amount of rainfall " << inches[highrainfall] << " inches in " << names[highrainfall] << endl; cout << "the smallest amount of rainfall " << inches[lowrainfall] << " inches in " << names[lowrainfall] << endl; cout << "\n" << endl; homecoming 0; } /********************************************************* * getrain * * gets amount of rain in inches of each month * * user. names array parameter holds names of * * months, , month array parameter hold * * amount of rainfall in iches of each corresponding * * month. * **********************************************************/ void getrain(string names[], int inches[], int size) { (int month = 0.00; month < size; month++) { //get amount of rainfall each month. cout << "how many inches of rain fall month of " << names[month] << ": "; cin >> inches[month]; // validate input. while (inches[month] < 0.00) { cout << "inches of rain must 0 or more. please re-enter: "; cin >> inches[month]; } } } /********************************************************** * gettotal * * calculates , returns total of values stored in* * array passed function. * ***********************************************************/ int gettotal(int array[], int size) { int total = 0.00; (int pos = 0.00; pos < size; pos++) total += array[pos]; homecoming total; } /*********************************************************** * getave * * calculates total of values stored in * * array passed function , averages them. * * ************************************************************/ int getave(int array[], int size) { int total = 0.00; double ave; (int pos = 0.00; pos < size; pos++) total += array[pos]; ave = total / size; homecoming ave; } /********************************************************** * largestelement * * finds , returns subscript of array position * * holding largest value in array passed * * function. * ***********************************************************/ int largestelement(int array[], int size) { int indexoflargest = 0.00; (int pos = 1; pos < size; pos++) { if (array[pos] > array[indexoflargest]) indexoflargest = pos; } homecoming indexoflargest; } /********************************************************** * smallestelement * * finds , returns subscript of array position * * holding smallest value in array passed * * function. * ***********************************************************/ int smallestelement(int array[], int size) { int indexofsmallest = 0.00; (int pos = 1; pos < size; pos++) { if (array[pos] < array[indexofsmallest]) indexofsmallest = pos; } homecoming indexofsmallest; }
you appear confusing concept of 'type' concept of 'variable'. each variable contain specific type of data, , have declare figure out it.
for example: there 12 months, should declare dealing month number integer.
another example: amount of rainfall can fractions of inch, dealing rainfall amounts, totals, averages, maximums, or minimums should declared floating-point type, such float or double.
finally, general hint, go reread , prepare variable names. have this:
// costant number of month entries const int rain_per_month = 12;
but mean months_per_year. you'll find if prepare names accurately describe mean, you'll understand code better.
c++ floating-point
Comments
Post a Comment