Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- //*******************************************************
- // function for bubble sort
- // Sort numbers of the ARRAY_SIZE using Bubble Sort.
- //********************************************************
- void BubbleSort (int numbers[], int ARRAY_SIZE)
- {
- bool swapped = true; // boolean function
- int j = 0;
- int tmp; // varible for temporary swap
- while (swapped) // while loop for the bubble sort
- {
- swapped = false;
- j++;
- for (int i = 0; i < ARRAY_SIZE - j; i++)
- {
- if (numbers[i] > numbers[i + 1])
- {
- tmp = numbers[i];
- numbers[i] = numbers[i + 1];
- numbers[i + 1] = tmp;
- swapped = true;
- }
- }
- }
- } // bubble sort function ends here
- //*******************************
- // function for finding the median
- // using numbers and ARRAY_SIZE to find median and print the median
- //**********************************
- int FindMedian(int numbers[], int ARRAY_SIZE)
- {
- int median;
- int mid;
- //Find the mid element.
- mid = ARRAY_SIZE / 2;
- //print out the value of mid
- //Array size is even.
- if (ARRAY_SIZE % 2 == 0)
- {
- //Calculate the median.
- median =(numbers[mid] + numbers[mid + 1]) / 2;
- }
- //Array size is odd.
- else
- {
- //Calculate the median.
- median = (numbers[mid]);
- }
- //cout median statement
- cout << endl << "Median is:" << median << endl;
- }
- //**************
- //* Main function
- //* opens our list and then stores this list into the array
- //* also has our functions for printing the array that is not sorted
- //* function for the bubble sort algorithm and prints the sorted numbers by returning
- //************
- int main ()
- {
- int count = 0; // loop counter varible
- string fileName="file.txt";
- ifstream inputFile; // input file stream object
- //opening the file
- inputFile.open(fileName); // Using ARRAY_SIZE and inputfile so that the text file works with any ammount of numbers
- int ARRAY_SIZE;
- inputFile >> ARRAY_SIZE;
- int numbers[ARRAY_SIZE];
- //reading the numbers from the file then putting it into the array
- while (count < ARRAY_SIZE && inputFile >> numbers [count])
- count ++;
- // closing file
- inputFile.close();
- // function for sorted numbers
- BubbleSort(numbers, ARRAY_SIZE);
- //cout statement for displaying the sorted numbers
- cout << " The sorted numbers are: " ;
- for (auto num: numbers)
- {
- cout << num << ' ' ;
- }
- //Declaring the variable to store the median.
- int median;
- //Calling the function findMedian() to find the median.
- median = FindMedian(numbers, ARRAY_SIZE);
- //cout statment for displaying median
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement