fork download
  1. // Takes the range of some data and outputs the value of each axis gradation (in terms of big squares on a graph)
  2.  
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <cmath>
  6.  
  7. int toGraphNum(double x)
  8. {
  9. if(x < 1) { return 1; }
  10. if(x < 2) { return 2; }
  11. if(x < 5) { return 5; }
  12. if(x > 5) { return 10; }
  13. }
  14.  
  15. int graphScaler(double largest, double smallest, int squares);
  16. int graphScaler(double difference, int squares);
  17.  
  18. int main()
  19. {
  20. double largest;
  21. double smallest;
  22. int bigSquares;
  23.  
  24. std::cout << "Please enter the largest number:";
  25. std::cin >> largest;
  26. std::cout << "Please enter the smallest number:";
  27. std::cin >> smallest;
  28.  
  29. double difference = abs(largest - smallest);
  30.  
  31. std::cout << "Please enter the number of big squares:";
  32. std::cin >> bigSquares;
  33.  
  34. int bigSquareVal = graphScaler(difference, bigSquares);
  35.  
  36. //IDEOne input newline fix:
  37. std::cout << std::endl;
  38.  
  39. std::cout << "Each big square is worth: " << bigSquareVal << std::endl;
  40. //Utilisation = {plotted data range}/{total axis range = amount per square * count(squares)}
  41. std::cout << "Graph utilisation percentage = " << difference / (bigSquareVal * bigSquares) * 100 << std::endl;
  42.  
  43. std::cout << "Press enter to continue...";
  44. std::cin.get();
  45. return 0;
  46. }
  47.  
  48. int graphScaler(double largest, double smallest, int squares)
  49. {
  50. graphScaler((double)abs(largest - smallest), squares);
  51. }
  52. int graphScaler(double difference, int squares)
  53. {
  54. double nastyGradation = (difference/squares);
  55. int ngOrder = pow(10,floor(log10(nastyGradation)));
  56. return toGraphNum(nastyGradation/ngOrder) * ngOrder;
  57. }
Success #stdin #stdout 0s 2864KB
stdin
78
42
9
stdout
Please enter the largest number:Please enter the smallest number:Please enter the number of big squares:
Each big square is worth: 5
Graph utilisation percentage = 80
Press enter to continue...