Advertisement
Guest User

Lab9

a guest
Nov 26th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void fillArray(int a[], int size, int& num_used);
  5. //Pre-condition: the declared size of array a is given by parameter size.
  6. // The argument variable passed to the call-by-reference parameter num_used should exist
  7. // and should be an in variabe in the code that calls this function.
  8. //Post-condition: array a will contain a number of letters entered by the user.
  9. // The number is given is stored in num_used parameter.
  10. // num_used shouls be less than or equal to size.
  11.  
  12. void displayArray(int a[], int num_used);
  13. //Pre-condition: array a should exist. num_used should be a valid integer in the
  14. // sense that it represents the actual number of numbers in array a. Note that num_used could
  15. // be equal to the declared size of array a or could be less than the declared size of a.
  16. //Post-condition: Those num_used numbers in array a are displayed on screen in a
  17. // single line in the order that they are stored in array a.
  18. //
  19. void find_max_min(int a[], int num_used, int& max_index, int& min_index);
  20. //Pre-condition: The first parameter a is an array of integers, and the second parameter
  21. // num_used represents the actual number of positions in a that are used to
  22. // store valid integers.
  23. //Post-condition: The third parameter max_index should contain the index position
  24. // where the largest element appears.
  25. // If there is more than one element in a that has the same maximum value,
  26. // then the third parameter max_index should contain the first index position
  27. // where the largest element appears.
  28. // The fourth parameter min_index should contain the index position
  29. // where the smallest element appears.
  30. // If there is more than one element in a that has the same minimum value,
  31. // then the fourth parameter min_index should contain the first index position
  32. // where the smallest element appears.
  33.  
  34. int num_pass_grades(int a[], int num_used);
  35. //Pre-condition: The first parameter a is an array of integers, and the second parameter
  36. // num_used represents the actual number of positions in a that are used to
  37. // store valid integers.
  38. //Post-condition: This function returns the number of integers of integers that are greater than or equal to
  39. // 60 in array a.
  40.  
  41. int main()
  42. {
  43. //Max size of arrays.
  44. const int ARRAY_SIZE = 20;
  45.  
  46. int a[ARRAY_SIZE];
  47. int num = 0;
  48. int x = -1, y = -1;
  49.  
  50. fillArray(a, ARRAY_SIZE, num);
  51. cout << "Now display the array that has just been entered:\n";
  52. displayArray(a, num);
  53.  
  54. find_max_min(a, num, x, y);
  55. cout << "\nNow display the max value in array a:\n";
  56. cout << a[x] << " first appears at index " << x << endl;
  57. cout << "\nNow display the min value in array a:\n";
  58. cout << a[y] << " first appears at index " << y << endl;
  59. }
  60.  
  61. void fillArray(int a[], int size, int& num_used)
  62. {
  63. cout << "\nEnter up to " << size << " integers.\n"
  64. << "Stop entering integers by entering a negative number (which is NOT included in the array).\n";
  65. for (num_used; num_used < size; num_used++)
  66. {
  67. cin >> a[num_used];
  68. if (a[num_used] < 0) break; //Negative number used to end array.
  69. }
  70. }
  71.  
  72. void displayArray(int a[], int num_used)
  73. {
  74. int i = 0;
  75.  
  76. for(i; i < num_used; i++)
  77. {
  78. cout << a[i] << " ";
  79. }
  80.  
  81. cout << endl;
  82. }
  83.  
  84. void find_max_min(int a[], int num_used, int& max_index, int& min_index)
  85. {
  86. int min = a[0],
  87. max = a[0];
  88.  
  89. for (int i = 1; i < num_used; i++)
  90. {
  91. if(a[i] < min)
  92. {
  93. min = a[i];
  94. min_index = i;
  95. }
  96. else
  97. {
  98. min = a[0];
  99. min_index = 0;
  100. }
  101. }
  102. for (int j = 1; j < num_used; j++)
  103. {
  104. if(a[j] > max)
  105. {
  106. max = a[j];
  107. max_index = j;
  108. }
  109. else
  110. {
  111. max = a[0];
  112. max_index = 0;
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement