Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void fillArray(int a[], int size, int& num_used);
- //Pre-condition: the declared size of array a is given by parameter size.
- // The argument variable passed to the call-by-reference parameter num_used should exist
- // and should be an in variabe in the code that calls this function.
- //Post-condition: array a will contain a number of letters entered by the user.
- // The number is given is stored in num_used parameter.
- // num_used shouls be less than or equal to size.
- void displayArray(int a[], int num_used);
- //Pre-condition: array a should exist. num_used should be a valid integer in the
- // sense that it represents the actual number of numbers in array a. Note that num_used could
- // be equal to the declared size of array a or could be less than the declared size of a.
- //Post-condition: Those num_used numbers in array a are displayed on screen in a
- // single line in the order that they are stored in array a.
- //
- void find_max_min(int a[], int num_used, int& max_index, int& min_index);
- //Pre-condition: The first parameter a is an array of integers, and the second parameter
- // num_used represents the actual number of positions in a that are used to
- // store valid integers.
- //Post-condition: The third parameter max_index should contain the index position
- // where the largest element appears.
- // If there is more than one element in a that has the same maximum value,
- // then the third parameter max_index should contain the first index position
- // where the largest element appears.
- // The fourth parameter min_index should contain the index position
- // where the smallest element appears.
- // If there is more than one element in a that has the same minimum value,
- // then the fourth parameter min_index should contain the first index position
- // where the smallest element appears.
- int num_pass_grades(int a[], int num_used);
- //Pre-condition: The first parameter a is an array of integers, and the second parameter
- // num_used represents the actual number of positions in a that are used to
- // store valid integers.
- //Post-condition: This function returns the number of integers of integers that are greater than or equal to
- // 60 in array a.
- int main()
- {
- //Max size of arrays.
- const int ARRAY_SIZE = 20;
- int a[ARRAY_SIZE];
- int num = 0;
- int x = -1, y = -1;
- fillArray(a, ARRAY_SIZE, num);
- cout << "Now display the array that has just been entered:\n";
- displayArray(a, num);
- find_max_min(a, num, x, y);
- cout << "\nNow display the max value in array a:\n";
- cout << a[x] << " first appears at index " << x << endl;
- cout << "\nNow display the min value in array a:\n";
- cout << a[y] << " first appears at index " << y << endl;
- }
- void fillArray(int a[], int size, int& num_used)
- {
- cout << "\nEnter up to " << size << " integers.\n"
- << "Stop entering integers by entering a negative number (which is NOT included in the array).\n";
- for (num_used; num_used < size; num_used++)
- {
- cin >> a[num_used];
- if (a[num_used] < 0) break; //Negative number used to end array.
- }
- }
- void displayArray(int a[], int num_used)
- {
- int i = 0;
- for(i; i < num_used; i++)
- {
- cout << a[i] << " ";
- }
- cout << endl;
- }
- void find_max_min(int a[], int num_used, int& max_index, int& min_index)
- {
- int min = a[0],
- max = a[0];
- for (int i = 1; i < num_used; i++)
- {
- if(a[i] < min)
- {
- min = a[i];
- min_index = i;
- }
- else
- {
- min = a[0];
- min_index = 0;
- }
- }
- for (int j = 1; j < num_used; j++)
- {
- if(a[j] > max)
- {
- max = a[j];
- max_index = j;
- }
- else
- {
- max = a[0];
- max_index = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement