1 Replies - 3130 Views - Last Post: 05 August 2008 - 06:17 AM Rate Topic: -----

#1 azuresonata  Icon User is offline

  • New D.I.C Head

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

Finding the median of user-defined elements in an array

Post icon  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=(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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1