fork download
  1. //Devin Scheu CS1A Chapter 7, P. 444, #1
  2. //
  3. /**************************************************************
  4. *
  5. * FIND LARGEST AND SMALLEST ARRAY VALUES
  6. * ____________________________________________________________
  7. * This program finds the largest and smallest values in an
  8. * array of 10 user-entered numbers.
  9. * ____________________________________________________________
  10. * INPUT
  11. * arrayValues : The 10 values entered by the user
  12. *
  13. * OUTPUT
  14. * largestValue : The largest value in the array
  15. * smallestValue : The smallest value in the array
  16. *
  17. **************************************************************/
  18.  
  19. #include <iostream>
  20. #include <iomanip>
  21.  
  22. using namespace std;
  23.  
  24. int main () {
  25.  
  26. //Variable Declarations
  27. const int ARRAY_SIZE = 10; //OUTPUT - Size of the array
  28. int arrayValues[ARRAY_SIZE]; //INPUT - The 10 values entered by the user
  29. int largestValue; //OUTPUT - The largest value in the array
  30. int smallestValue; //OUTPUT - The smallest value in the array
  31.  
  32. //Prompt for Input
  33. for (int i = 0; i < ARRAY_SIZE; i++) {
  34. cout << "Enter value " << (i + 1) << ": ";
  35. cin >> arrayValues[i];
  36. cout << arrayValues[i] << endl;
  37. }
  38.  
  39. //Find Largest and Smallest
  40. smallestValue = arrayValues[0];
  41. largestValue = arrayValues[0];
  42. for (int i = 1; i < ARRAY_SIZE; i++) {
  43. if (arrayValues[i] < smallestValue) {
  44. smallestValue = arrayValues[i];
  45. }
  46. if (arrayValues[i] > largestValue) {
  47. largestValue = arrayValues[i];
  48. }
  49. }
  50.  
  51. //Separator and Output Section
  52. cout << "-------------------------------------------------------" << endl;
  53. cout << "OUTPUT:" << endl;
  54.  
  55. //Output Result
  56. cout << left << setw(25) << "Smallest Value:" << right << setw(15) << smallestValue << endl;
  57. cout << left << setw(25) << "Largest Value:" << right << setw(15) << largestValue << endl;
  58.  
  59. } //end of main()
Success #stdin #stdout 0s 5328KB
stdin
6
7
11
62
34
91
192
1
0
11111
stdout
Enter value 1: 6
Enter value 2: 7
Enter value 3: 11
Enter value 4: 62
Enter value 5: 34
Enter value 6: 91
Enter value 7: 192
Enter value 8: 1
Enter value 9: 0
Enter value 10: 11111
-------------------------------------------------------
OUTPUT:
Smallest Value:                        0
Largest Value:                     11111