c++ - logic help for smallest/largest value -
i went thru many version of algorithm sort smallest , largest brain fried. book point , searching online haven't helped @ all. i'm having difficulties @ saving last. used 3 in, 10 cm , 5 cm test cases. entering 3 in first, becomes largest, entering 5 cm second becomes smallest , 10 cm becomes smallest again. tried different version on 2 hours, re-wrote entire section. in book programming principles , practices using c++, in review section, before cant find me out.
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <stdlib.h> #include <iomanip> using namespace std; int main() { vector<double>all_meters; double smallest= 0,print_smallest, largest = 0,print_largest, num = 0; string unit, s_input, num_s_input, small_unit, large_unit; while(cin.good()){ cout << "\n\t\t\t\tenter '|' exit.\n\n"; cout << "\t\tnumber compare followed white space , unit:"; cin >> num_s_input; if(num_s_input.compare("|") == 0 || (s_input.compare("|") == 0)){ double sum = 0; (double x : all_meters) sum+=x; cout << "sum: " << setprecision(4) << sum << "m\n"; cout << "smallest number: " << print_smallest << small_unit << endl << "largest number: " << print_largest << large_unit << endl << "total number of values: " << all_meters.size() << endl << "all entered numbers converted meters are: \n"; (double = 0; i<all_meters.size(); ++i){ cout << all_meters[i] << setprecision(2) <<"m "; } cout << "\nalright now, goodbye !\n" << endl; break; } else{ cin >> s_input; num = strtod(num_s_input.c_str(), null); unit = s_input; double meter = 0; if(unit=="cm"){ meter = num / 100;} else if(unit=="in"){ meter = num / 39.370;} else if(unit=="ft"){ meter = num / 3.2808;} else if(unit=="m"){ meter = num;} else { cout << "\n\tyou entered wrong unit!\t\n";} if(largest==0){ largest = meter; print_largest = num; large_unit = unit; cout << num << unit << " largest far.\n"; } else if(smallest==0&&meter<largest){ smallest = meter; print_smallest = num; small_unit = unit; cout << num << unit << " smallest far.\n"; } else if(largest<meter){ largest = meter; print_largest = num; large_unit = unit; cout << num << unit << " largest far.\n"; } else if(smallest>meter){ smallest = meter; print_smallest = num; small_unit = unit; cout << num << unit << " smallest far.\n"; } all_meters.push_back(meter); sort(all_meters.begin(),all_meters.end()); } } } managed solve without using limit, added new changes code. guys !
you need choose standard unit of measure. question suggests meters, use (you can use float or double this, depending on precision need).
the problem simple, create variables sum, smallest seen, , largest seen, each new input, convert standard format, , update variables.
the solution (to started, not working code) might this:
// can represent different types of units integers float converttometers(float unconvertedvalue, int unit) { // convert unconvertedvalue based on unit } float smallest = std::numeric_limits<float>::max(); float largest = std::numeric_limits<float>::lowest(); float sum = 0.0f; // update each new input while (new_input) { float convertedvalue = converttometers(new_value, unit); // update total sum += convertedvalue; // update smallest , largest if (convertedvalue > largest) largest = convertedvalue; else if (convertedvalue < smallest) smallest = convertedvalue; } as nathan mentioned, #include <limits> limits.
Comments
Post a Comment