Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,045 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,521 people online right now. Registration is fast and FREE... Join Now!




Finding the median of user-defined elements in an array

 
Reply to this topicStart new topic

Finding the median of user-defined elements in an array

azuresonata
5 Aug, 2008 - 04:46 AM
Post #1

New D.I.C Head
*

Joined: 22 Jul, 2008
Posts: 9

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:

CODE

#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.
User is offlineProfile CardPM
+Quote Post

Hyper_Eye
RE: Finding The Median Of User-defined Elements In An Array
5 Aug, 2008 - 05:17 AM
Post #2

D.I.C Head
**

Joined: 13 Sep, 2007
Posts: 72



Thanked: 4 times
My Contributions
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?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 04:56PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month