Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: Julians Krumins
- // Date: February 26, 2020
- // Project: ECE_Exercise_21
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- void new_random_array(double a[], int s)
- {
- for (int n=0; n < s; n++)
- {
- a[n] = double(rand() % 100);
- }
- }
- void display_array(double a[], int s)
- {
- for (int n = 0; n < s; n++)
- {
- cout << a[n] << endl;
- }
- }
- double array_max(double a[], int s)
- {
- double max = a[0];
- for (int n = 1; n < s; n++)
- {
- if (a[n] > max)
- {
- max = a[n];
- }
- }
- return max;
- }
- double array_min(double a[], int s)
- {
- double min = a[0];
- for (int n = 1; n < s; n++)
- {
- if (a[n] < min)
- {
- min = a[n];
- }
- }
- return min;
- }
- void array_sort(double a[], int s)
- {
- double swap = 0;
- for (int i = s - 1; i > 0; i--)
- {
- for (int j = 0; j < i; j++)
- {
- if (a[j] < a[j + 1])
- {
- swap = a[j];
- a[j] = a[j + 1];
- a[j + 1] = swap;
- }
- }
- }
- return;
- }
- void array_disp(double a[], int s)
- {
- array_sort(a, s);
- for (int i = 0; i < s; i++)
- {
- cout << a[i] << " " << endl;
- }
- cout << endl;
- }
- double array_mean(double a[], int s)
- {
- double sum = 0;
- for (int n = 0; n < s; n++)
- {
- sum = sum + a[n];
- }
- return sum / s;
- }
- double array_median(double a[], int s)
- {
- double med = 0;
- array_sort(a, s);
- if (s % 2 == 1)
- {
- med = a[s / 2];
- }
- else
- {
- med = (a[s / 2] + a[s / 2 - 1]) / 2.0;
- }
- return med;
- }
- double array_variance(double a[], int s)
- {
- double mean = array_mean(a, s);
- double var = 0;
- for (int n = 0; n < s; n++)
- {
- var = var + (a[n] - mean) * (a[n] - mean);
- }
- var = var / (double(s) - 1);
- return var;
- }
- int main()
- {
- const int size = 10;
- double array[size];
- int selection = 0;
- do
- {
- system("cls");
- cout << "Main Menu" << endl;
- cout << "1. Create a new array" << endl;
- cout << "2. Display array" << endl;
- cout << "3. Find Maximum value" << endl;
- cout << "4. Find Minimum value" << endl;
- cout << "5. Sort the array in descending order and display" << endl;
- cout << "6. Find mean value" << endl;
- cout << "7. Find median value" << endl;
- cout << "8. Find variance" << endl;
- cout << "9. End." << endl << endl;
- cout << "Make a selection: ";
- cin >> selection;
- switch (selection)
- {
- case 1:
- cout << "Created array. " << endl;
- new_random_array(array, size);
- cout << endl;
- system("pause");
- break;
- case 2:
- cout << "Display array: " << endl;
- display_array(array, size);
- cout << endl;
- system("pause");
- break;
- case 3:
- cout << "Maximum value: ";
- cout << array_max(array, size);
- cout << endl;
- system("pause");
- break;
- case 4:
- cout << "Minimum value: ";
- cout << array_min(array, size);
- cout << endl;
- system("pause");
- break;
- case 5:
- cout << "5. Sorted in descending order: " << endl;
- array_disp(array, size);
- cout << endl;
- system("pause");
- break;
- case 6:
- cout << "mean value: ";
- cout << array_mean(array, size);
- cout << endl;
- system("pause");
- break;
- case 7:
- cout << "median value: ";
- cout << array_mean(array, size);
- cout << endl;
- system("pause");
- break;
- case 8:
- cout << "variance: ";
- cout << array_variance(array, size);
- cout << endl;
- system("pause");
- break;
- case 9:
- break;
- system("cls");
- }
- } while (selection != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement