Advertisement
Porr011

Lesson 11 activity 4

Apr 17th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. //*******************************************************
  5. // function for bubble sort
  6. // Sort numbers of the ARRAY_SIZE using Bubble Sort.
  7. //********************************************************
  8. void BubbleSort (int numbers[], int ARRAY_SIZE)
  9. {
  10.      bool swapped = true; // boolean function
  11.       int j = 0;
  12.       int tmp; // varible for temporary swap
  13.  
  14.     while (swapped) // while loop for the bubble sort
  15.     {
  16.     swapped = false;
  17.     j++;
  18.     for (int i = 0; i < ARRAY_SIZE - j; i++)
  19.     {
  20.     if (numbers[i] > numbers[i + 1])
  21.     {
  22.     tmp = numbers[i];
  23.     numbers[i] = numbers[i + 1];
  24.     numbers[i + 1] = tmp;
  25.     swapped = true;
  26.  
  27.     }
  28.  
  29.     }
  30.  
  31.     }
  32. } // bubble sort function ends here
  33. //*******************************
  34. // function for finding the median
  35. // using numbers and ARRAY_SIZE to find median and print the median
  36. //**********************************
  37. int FindMedian(int numbers[], int ARRAY_SIZE)
  38.  {
  39.  int median;
  40.  int mid;
  41.  //Find the mid element.
  42.  mid = ARRAY_SIZE / 2;
  43. //print out the value of mid
  44.  //Array size is even.
  45.  if (ARRAY_SIZE % 2 == 0)
  46.  {
  47.  //Calculate the median.
  48.  median =(numbers[mid] + numbers[mid + 1]) / 2;
  49.  }
  50.  //Array size is odd.
  51.  else
  52.  {
  53.  //Calculate the median.
  54.  median = (numbers[mid]);
  55.  }
  56.  //cout median statement
  57.  cout << endl << "Median is:" << median << endl;
  58.  }
  59.  
  60. //**************
  61. //* Main function
  62. //* opens our list and then stores this list into the array
  63. //* also has our functions for printing the array that is not sorted
  64. //* function for the bubble sort algorithm and prints the sorted numbers by returning
  65. //************
  66. int main ()
  67. {
  68.     int count = 0; // loop counter varible
  69.     string fileName="file.txt";
  70.     ifstream inputFile; // input file stream object
  71.    
  72.     //opening the file
  73.     inputFile.open(fileName); // Using ARRAY_SIZE and inputfile so that the text file works with any ammount of numbers
  74.      int ARRAY_SIZE;
  75.    inputFile >> ARRAY_SIZE;
  76.      int numbers[ARRAY_SIZE];
  77.    
  78.     //reading the numbers from the file then putting it into the array
  79.     while (count < ARRAY_SIZE && inputFile >> numbers [count])
  80.     count ++;
  81.    
  82.     // closing file
  83.     inputFile.close();
  84.    
  85.     // function for sorted numbers
  86.     BubbleSort(numbers, ARRAY_SIZE);
  87.    
  88.     //cout statement for displaying the sorted numbers
  89.      cout << " The sorted numbers are: " ;
  90.      for (auto num: numbers)
  91.     {
  92.     cout << num << ' ' ;
  93.     }
  94.     //Declaring the variable to store the median.
  95.     int median;
  96.      //Calling the function findMedian() to find the median.
  97.      median = FindMedian(numbers, ARRAY_SIZE);
  98.    
  99.     //cout statment for displaying median
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement