fork download
  1. //Cameron Pham CS1A Chapter 7, p. 444, #2
  2. //
  3. /***************************************************************
  4. * DISPLAY LARGEST AND SMALLEST ARRAY VALUES
  5. ****************************************************************
  6. * This program accepts 10 float values from the user and stores them
  7. * in an array. It then determines and displays the largest and
  8. * smallest values stored in the array.
  9. * ______________________________________________________________________________
  10. * INPUT:
  11. * Values // The 10 numbers entered by the user
  12. *
  13. * OUTPUT:
  14. * Largest // The largest value in the array
  15. * Smallest // The smallest value in the array
  16. ****************************************************************/
  17.  
  18. #include <iostream>
  19. using namespace std;
  20.  
  21. int main()
  22. {
  23. // Initialize variables
  24. float values[10]; // Stores the 10 numbers
  25. float largest; // Stores the largest number
  26. float smallest; // Stores the smallest number
  27.  
  28. // Ask the user to enter 10 numbers
  29. for (int i = 0; i < 10; i++)
  30. {
  31. cout << "Enter value #" << (i + 1) << ": ";
  32. cin >> values[i];
  33. }
  34.  
  35. // Initialize the largest and smallest values
  36. // using the first element in the array
  37. largest = values[0];
  38. smallest = values[0];
  39.  
  40. // Search the array for the largest and smallest values
  41. for (int i = 1; i < 10; i++)
  42. {
  43. if (values[i] > largest)
  44. {
  45. largest = values[i];
  46. }
  47.  
  48. if (values[i] < smallest)
  49. {
  50. smallest = values[i];
  51. }
  52. }
  53.  
  54. // Display the results
  55. cout << endl << "Largest value: " << largest << endl;
  56. cout << "Smallest value: " << smallest << endl;
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0s 5320KB
stdin
67.67 900 41 87 -5677886 0.11112 700.90867 4 1000000000.8 1
stdout
Enter value #1: Enter value #2: Enter value #3: Enter value #4: Enter value #5: Enter value #6: Enter value #7: Enter value #8: Enter value #9: Enter value #10: 
Largest value: 1e+09
Smallest value: -5.67789e+06