arrfun3.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // arrfun3.cpp -- array functions and const
  2. #include <iostream>
  3. const int Max = 5;
  4. // function prototypes
  5. int fill_array(double ar[], int limit);
  6. void show_array(const double ar[], int n); // don't change data
  7. void revalue(double r, double ar[], int n);
  8. int main()
  9. {
  10. using namespace std;
  11. double properties[Max];
  12. int size = fill_array(properties, Max);
  13. show_array(properties, size);
  14. if (size > 0)
  15. {
  16. cout << "Enter revaluation factor: ";
  17. double factor;
  18. while (!(cin >> factor)) // bad input
  19. {
  20. cin.clear();
  21. while (cin.get() != '\n')
  22. continue;
  23. cout << "Bad input; Please enter a number: ";
  24. }
  25. revalue(factor, properties, size);
  26. show_array(properties, size);
  27. }
  28. cout << "Done.\n";
  29. // cin.get();
  30. // cin.get();
  31. return 0;
  32. }
  33. int fill_array(double ar[], int limit)
  34. {
  35. using namespace std;
  36. double temp;
  37. int i;
  38. for (i = 0; i < limit; i++)
  39. {
  40. cout << "Enter value #" << (i + 1) << ": ";
  41. cin >> temp;
  42. if (!cin) // bad input
  43. {
  44. cin.clear();
  45. while (cin.get() != '\n')
  46. continue;
  47. cout << "Bad input; input process terminated.\n";
  48. break;
  49. }
  50. else if (temp < 0) // signal to terminate
  51. break;
  52. ar[i] = temp;
  53. }
  54. return i;
  55. }
  56. // the following function can use, but not alter,
  57. // the array whose address is ar
  58. void show_array(const double ar[], int n)
  59. {
  60. using namespace std;
  61. for (int i = 0; i < n; i++)
  62. {
  63. cout << "Property #" << (i + 1) << ": $";
  64. cout << ar[i] << endl;
  65. }
  66. }
  67. // multiplies each element of ar[] by r
  68. void revalue(double r, double ar[], int n)
  69. {
  70. for (int i = 0; i < n; i++)
  71. ar[i] *= r;
  72. }