Advertisement
craigdc1982

C++ Code

Apr 25th, 2020
3,996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1. // ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3. //==========================================================================================================
  4. //Name:         MathCon
  5. //Author:       Craig D Cartwright
  6. //Version:      1.0
  7. //Copyright:    CDC 2020
  8. //Description:  A small console application performing mathematical operations
  9. //
  10. //===========================================================================================================
  11.  
  12. #include <iostream>
  13. #include <algorithm>
  14. #include <stdio.h>
  15. #include <vector>
  16. using namespace std;
  17. void quick_sort(int[], int, int);
  18. int partition(int[], int, int);
  19.  
  20.  
  21. //Variables
  22. //const int SIZE = 50;
  23. string name; //A string called name which we can store a name
  24. int age; //A integer called age allowing to store a persons age
  25. int math; //A integer for the menu options
  26. int values;
  27. int CountNumbers;
  28. int largest;
  29. int smallest;
  30. int result;
  31. int i, max, min, size;
  32.  
  33.  
  34. int main()
  35. {
  36.     //Code for entering and storing name
  37.     cout << "Welcome, what is your name?\n"; //This asks the user their name and then indicates to start a new line (\n)
  38.     cin >> name; //This takes the input from the previous line and stores in the name string.
  39.     cout << "Thank you " << name << "! Welcome to the MathCon App" << endl; //
  40.    
  41.     //Code for entering and storing age
  42.     cout << "How old are you " << name << "??\n"; //Asks user for their age
  43.     cin >> age; //This takes the users age input and stores in the age integer
  44.     cout << "Ah, so that means that you are " << age << " then " << name << "!\n"; //makes a comment about the age.
  45.  
  46.     //Code for entering integers into the console app
  47.     int a[50], n, i; //Initalise an array for up to 50 integers
  48.     cout << "How many Integers would you like to enter?\n";
  49.     cin >> n;//Stores the user value into n
  50.     cout << "Please enter your integers...\n";//asks the user to enter their chosen numbers...
  51.  
  52.     for (i = 0; i < n; i++)
  53.         cin >> a[i];
  54.  
  55.     quick_sort(a, 0, n - 1);
  56.     cout << "\nYour integers after sorting in ascending order are: " << endl;
  57.  
  58.        for (i = 0; i < n; i++)
  59.         cout << a[i] << " " << endl;
  60.  
  61.     //Mathematical Operations
  62.  
  63.        cout << name << " please choose from the options below \n" << //This asks the user their name and then indicates to start a new line (\n)
  64.            "1. Display the Count of the Integers input \n" <<
  65.            "2. Display the Lowest and Highest Integers input \n" <<
  66.            "3. Calculate the Range of the Integers input \n" <<
  67.            "4. Calculate the Mean of the Integers input \n" <<
  68.            "5. Calculate the Median of the Integers input \n" <<
  69.            "6. Exit the program \n" <<
  70.            "7. Enter new Integers \n";
  71.        cin >> math;
  72.  
  73.     //Switch Decisions for Menu Options
  74.  
  75.        switch (math)
  76.        {
  77.        case 1: //Display the Count of the Integers Input
  78.        {
  79.            cout << "The Total Number of Integers entered is " << i << "\n";
  80.            break;
  81.        }
  82.        case 2: //Display the Lowest and Highest Integers input
  83.  
  84.            vector<int> getLargestAndSmallestNumFromVector(vector<int> input) {
  85.  
  86.                int min = input[0];
  87.                int max = input[0];
  88.  
  89.                for (int i = 0; i < input.size(); i += 1) {
  90.  
  91.                    int checkSum = input[i];
  92.  
  93.                    if (checkSum < min) {
  94.                        min = checkSum;
  95.                    }
  96.  
  97.                    if (checkSum > max) {
  98.                        max = checkSum;
  99.                    }
  100.                }
  101.  
  102.                vector<int> result;
  103.                result.push_back(min);
  104.                result.push_back(max);
  105.                return result;
  106.            }
  107.            cout << "\nThe largest value entered is " << max << endl;
  108.            cout << "\nThe smallest value entered is " << min << endl;
  109.        }
  110.        }
  111.        
  112.     }
  113.  
  114.  
  115.  
  116. void quick_sort(int a[], int l, int u)
  117. {
  118.     int j;
  119.     if (l < u)
  120.     {
  121.         j = partition(a, l, u);
  122.         quick_sort(a, l, j - 1);
  123.         quick_sort(a, j + 1, u);
  124.     }
  125. }
  126.  
  127. int partition(int a[], int l, int u)
  128. {
  129.     int v, i, j, temp;
  130.     v = a[l];
  131.     i = l;
  132.     j = u + 1;
  133.  
  134.     do
  135.     {
  136.         do
  137.             i++;
  138.  
  139.         while (a[i] < v && i <= u);
  140.  
  141.         do
  142.             j--;
  143.         while (v < a[j]);
  144.  
  145.         if (i < j)
  146.         {
  147.             temp = a[i];
  148.             a[i] = a[j];
  149.             a[j] = temp;
  150.         }
  151.     } while (i < j);
  152.  
  153.     a[l] = a[j];
  154.     a[j] = v;
  155.  
  156.     return(j);
  157.  
  158.    
  159. }
  160.  
  161.  
  162.  
  163.    
  164.  
  165.  
  166. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  167. // Debug program: F5 or Debug > Start Debugging menu
  168.  
  169. // Tips for Getting Started:
  170. //   1. Use the Solution Explorer window to add/manage files
  171. //   2. Use the Team Explorer window to connect to source control
  172. //   3. Use the Output window to see build output and other messages
  173. //   4. Use the Error List window to view errors
  174. //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  175. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement