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. const int SIZE = 10;
  24. // Initialize variables
  25. float values[SIZE]; // Stores the 10 numbers
  26. float largest; // Stores the largest number
  27. float smallest; // Stores the smallest number
  28.  
  29. // Ask the user to enter 10 numbers
  30. for (int i = 0; i < SIZE; i++)
  31. {
  32. cout << "Enter value #" << (i + 1) << ": ";
  33. cin >> values[i];
  34. }
  35.  
  36. // Initialize the largest and smallest values
  37. // using the first element in the array
  38. largest = values[0];
  39. smallest = values[0];
  40.  
  41. // Search the array for the largest and smallest values
  42. for (int i = 1; i < SIZE; i++)
  43. {
  44. if (values[i] > largest)
  45. {
  46. largest = values[i];
  47. }
  48.  
  49. if (values[i] < smallest)
  50. {
  51. smallest = values[i];
  52. }
  53. }
  54.  
  55. // Display the results
  56. cout << endl << "Largest value: " << largest << endl;
  57. cout << "Smallest value: " << smallest << endl;
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5308KB
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