Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Project 5: Median. 2 The median of a collection of numbers is the member for which there are an equal number less than or equal and greater than or.

Similar presentations


Presentation on theme: "1 Project 5: Median. 2 The median of a collection of numbers is the member for which there are an equal number less than or equal and greater than or."— Presentation transcript:

1 1 Project 5: Median

2 2 The median of a collection of numbers is the member for which there are an equal number less than or equal and greater than or equal. If the numbers are arranged in order, and there is an odd number of values, the median is the middle value. Example: 1, 2, 3, 4, 5, 6, 7 If there is an even number of values, there are two “middle” values. Use the first of these. Example: 1, 2, 3, 4, 5, 6

3 3 Project 5: Median Write a program that accepts up to 10 integers from the user and finds the median value. Values can be either positive or negative. Values must be greater than INT_MIN. User can enter a letter to indicate no more values. OK to terminate input when any non-digit is entered. Output an error message if no numbers are entered. Think about what can go wrong.

4 4 Implementation Specifications Provide an array in main to hold the numbers. Write a function to get keyboard input and put the values into the array. Call from main. Write a function that takes the array, and number of values, as parameters and returns the median value. This function will sort the numbers, determine the median, and return the median to the caller. Call from main. int median (int numbers[], int length)

5 5 Implementation Specifications Write a function to sort the array. void sort(int numbers[], int length) Call from median Use any sorting algorithm you like Example on next slide. Let the sort function call a function to swap values. The swap function will take two pointers to integer as arguments and exchange the values to which they point. void swap (int* n1, int* n2) Call from sort

6 6 Sorting Algorithm You may use this inefficient, but very simple, sorting algorithm if you wish: Simple Exchange Sort Given an array of numbers, Compare each entry after the first to the preceding entry. If the preceding entry is larger, exchange them. Repeat the above until no exchange is done.

7 7 Sample Run 212 Sun Server:~/test: gcc -Wall median.c 213 Sun Server:~/test: a.out This program determines the median of a list of numbers All numbers must be greater than -2147483648 Please enter up to 10 integers Enter any letter to terminate input before 10 entries 1: 2345 2: -67676 3: -21 4: 89045 5: 199 6: x The median value is 199 214 Sun Server:~/test:

8 8 Sample Run 214 Sun Server:~/test: a.out This program determines the median of a list of numbers All numbers must be greater than -2147483648 Please enter up to 10 integers Enter any letter to terminate input before 10 entries 1: 10 2: 9 3: 8 4: 7 5: 6 6: 5 7: 4 8: 3 9: 2 10: 1 The median value is 5 215 Sun Server:~/test:

9 9 Sample Run 215 Sun Server:~/test: 215 Sun Server:~/test: a.out This program determines the median of a list of numbers All numbers must be greater than -2147483648 Please enter up to 10 integers Enter any letter to terminate input before 10 entries 1: abc No numbers were entered 216 Sun Server:~/test:

10 10 Sample Run 216 Sun Server:~/test: 216 Sun Server:~/test: a.out This program determines the median of a list of numbers All numbers must be greater than -2147483648 Please enter up to 10 integers Enter any letter to terminate input before 10 entries 1: 12.4 2: The median value is 12 217 Sun Server:~/test:

11 11 Implementation Specifications This is an exercise in using functions and passing arrays and pointers as arguments to functions. To get full credit, your program must Include a function to fill the array with keyboard input. Include a function to find the median of an array of intergers. Include a function to sort an array of integers. Include a function to exchange two integers.

12 12 Submission Submit just your source file. No test plan. No executable file. Project is due by midnight, Sunday night, March 5 No late submissions will be accepted. Turn in your your source code through Blackboard Assignments for this class. Not Digital Dropbox Not Email

13 13 The Usual Ground Rules Apply This is an individual project. It is OK to discuss the project. BUT: Don’t let anyone see or copy your work. Don’t look at or copy anyone else’s work. Be sure your source file is read & write protected. Files will be checked for plagarism. End of Presentation


Download ppt "1 Project 5: Median. 2 The median of a collection of numbers is the member for which there are an equal number less than or equal and greater than or."

Similar presentations


Ads by Google