1 project 5: median. 2 the median of a collection of numbers is the member for which there are an...

13
1 Project 5: Median

Upload: zoe-park

Post on 26-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

1

Project 5: Median

Page 2: 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

2

Project 5: Median

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

Page 3: 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

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.

Page 4: 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

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)

Page 5: 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

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

Page 6: 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

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.

Page 7: 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

7

Sample Run

212 Sun Server:~/test: gcc -Wall median.c213 Sun Server:~/test: a.outThis program determines the median of a list of numbersAll numbers must be greater than -2147483648 Please enter up to 10 integersEnter any letter to terminate input before 10 entries1: 23452: -676763: -214: 890455: 1996: xThe median value is 199214 Sun Server:~/test:

Page 8: 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

8

Sample Run

214 Sun Server:~/test: a.outThis program determines the median of a list of numbersAll numbers must be greater than -2147483648 Please enter up to 10 integersEnter any letter to terminate input before 10 entries1: 102: 93: 84: 75: 66: 57: 48: 39: 210: 1The median value is 5215 Sun Server:~/test:

Page 9: 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

9

Sample Run

215 Sun Server:~/test: 215 Sun Server:~/test: a.outThis program determines the median of a list of numbersAll numbers must be greater than -2147483648 Please enter up to 10 integersEnter any letter to terminate input before 10 entries1: abcNo numbers were entered216 Sun Server:~/test:

Page 10: 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

10

Sample Run

216 Sun Server:~/test: 216 Sun Server:~/test: a.outThis program determines the median of a list of numbersAll numbers must be greater than -2147483648 Please enter up to 10 integersEnter any letter to terminate input before 10 entries1: 12.42: The median value is 12217 Sun Server:~/test:

Page 11: 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

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.

Page 12: 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

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

Page 13: 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

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