fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. float half(float num)
  6. {
  7. return num / 2;
  8. }
  9.  
  10. int main()
  11. {
  12. // Declares variables
  13. float nums[3]; // or std::array<float, 3> nums; or std::vector<float> nums(3);
  14. float halfvalues[3];
  15.  
  16. //asks for values
  17. std::cout << "Enter 3 real numbers and I will display their halves: " << std::endl << std::endl;
  18. //stores values
  19. for (auto& v : nums) {
  20. std::cin >> v;
  21. }
  22. //return half and assign result to halfvalue
  23. std::transform(std::begin(nums), std::end(nums), std::begin(halfvalues), &half);
  24.  
  25. //set precision
  26. std::cout << std::fixed << std::showpoint << std::setprecision (3);
  27. //Prints message with results
  28. for (const auto& v : halfvalues) {
  29. std::cout << " " << v;
  30. }
  31. std::cout << " are the halves of ";
  32. for (const auto& v : nums) {
  33. std::cout << " " << v;
  34. }
  35. std::cout << std::endl;
  36. }
  37.  
  38.  
Success #stdin #stdout 0s 3460KB
stdin
9 4 2
stdout
Enter 3 real numbers and I will display their halves: 

 4.500 2.000 1.000 are the halves of  9.000 4.000 2.000