# Finding the median of user-defined elements in an array

Page 1 of 1

## 1 Replies - 2968 Views - Last Post: 05 August 2008 - 06:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=59703&amp;s=2913adb7628ac656d52ad29efd1e33b9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 azuresonata

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 22-July 08

# Finding the median of user-defined elements in an array

Posted 05 August 2008 - 05:46 AM

I created a c program (as practice exercise for myself) that displays the median of a given array. Note that this is only a test program. I am trying to make a program that asks the user to fill in the array(what I did is to add a set of pre-defined number of elements in my program). Here's the program:

```#include <iostream.h>

int main(int argc, char* argv[])
{
int arr[]={1,2,3,4,5,6};
int size=sizeof(arr)/sizeof(int);
int index=size/2;
float median=-1;

if((index % 2) == 0)
median=arr[index];
else
median=(float) (arr[index] + arr[index - 1]) / 2;

return 0;
}

```

I was unable to continue with it. How do I change the program so that it will:

>instead of a pre-defined given elements in the array, it will ask the user to enter the number that will fill in the array.
>it will sort the given array from highest to lowest and determine the median.

I'm actually a beginner in C program so I'm having troubles with the sorting algorithm. Thanks in advance.

Is This A Good Question/Topic? 0

## Replies To: Finding the median of user-defined elements in an array

### #2 Hyper_Eye

Reputation: 39
• Posts: 116
• Joined: 13-September 07

## Re: Finding the median of user-defined elements in an array

Posted 05 August 2008 - 06:17 AM

I have a couple questions on this. First, are you intending this to be a pure C program (as you say C programming throughout your post) or are you making a C++ program. C uses the std input header stdio.h. iostream.h is a C++ header.

Second, do you want the user to input the array elements as command-line parameters to the application or do you want them to input after the program has started. You could do it either way.

Third, you said you are having trouble with the sorting algorithm. Could you post the code from the sort you have already written?