EECS168 09:Lab10
| Navigation | 
|---|
| Home | 
| Information | 
| Classwork | 
Objectives
-  Elaboration on Arrays
-  Finding the median of an array
- Sorting Arrays
 
 
 -  Finding the median of an array
 
Median of an Array
The median of a finite list of numbers can be found by arranging all the elements from lowest to highest value (or vice versa) and picking the middle one. If there is an even number of elements, the median is not unique, so one often takes the mean of the two middle values. In this lab, you will write a program that reads a list of numbers from a file into an array. The program then sorts the array and returns its median.
Program Specification
- The program will read a list of integer numbers from a file called numbers.dat
 - Consider a maximum size of 100 for the array
 - Write a function called exchange_sort() to sort the array
 - Write a function called median() to find the median of the array
 
Sorting Arrays
There are various methods for sorting arrays. Different approaches have various efficiency. In this lab we will start looking at one of the basic sort methods called Exchange Sort. The exchange sort compares elements of the array and swaps those that are not in their proper positions.
Create a program called exchangeSort.cpp and follow the instructions to complete the code,
// Consider a maximum array size of 100
const int MAX_ARRAY_SIZE = 100;
//=======================
// FUNCTION DECLARATIONS
void exchange_sort(int array[], int array_size);
float median(int array[], int array_size);
//========================
int main()
{
	// Declare an integer array
	
        // Open the input file 
	// read in the array elements from the input file
	
	// output the original array
	
        // call the exchange sort function
	// output the sorted array
        // call the median function
	
}
//=========================
// FUNCTION DEFINITIONS
//Function definition of exchange_sort
void exchange_sort(int array[], int array_size)
{
  int i, j;
  int temp;   // temporary swap variable
  // Write a code which goes through each element of the array, compares it with all the elements after it, and swaps them if necessary, use the temporary variable to swap any two array elements
  // TO DO: you will need two nested for loops
  // The outer loop will go through all elements except the last one
  // The inner loop will go through all the elements after the current element of the outer loop
//Function definition of median
Example Program Execution
- The original array:
 - 12 32 14 56 43 22 87 65 45 55
 
- The sorted array:
 - 87 65 56 55 45 43 32 22 14 12
 
- The median is: 44
 
Lab Submission
- exchangeSort.cpp
 
- Your tarball and the directory inside it should be named like Smith-1234567-Lab-08
 - The email subject should be "[EECS 168] Lab 10" (without the quotes).
 
Remember to include your full name, student number, and disk usage in the email.