banner



How To Find The Median Of An Array In C++

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer'southward Best Practices
  • Questions and Answers
  • Constructive Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

C program to find the median of a given list.


If the elements of the list are bundled in order, and so, the centre value which divides the items into two parts with equal number of items on either side is chosen the median.

Odd numbers of items have just ane middle value whereas; fifty-fifty numbers of items have two middle values.

The median for fifty-fifty number of items is therefore, designated as the average of the two middle values.

Algorithm

Refer an algorithm given below to calculate the median.

Stride 1 − Read the items into an array while keeping a count of the items.

Stride two − Sort the items in increasing social club.

Step iii − Compute median.

The logic used to sort the numbers before finding a median is as follows −

for (i = 1 ; i <= due north-i ; i++){    for (j = 1 ; j <= due north-i ; j++){       if (a[j] <= a[j+i]){          t = a[j];          a[j] = a[j+one];          a[j+1] = t;       } else       go on ;    } }

The logic used to find a median of list is as follows −

if ( due north % ii == 0)    median = (a[n/2] + a[n/2+i])/2.0 ; else    median = a[n/2 + 1];

Example

Post-obit is the C program to calculate the median of given numbers −

 Live Demo

#include<stdio.h> #define N 10 main( ){    int i,j,n;    bladder median,a[N],t;    printf("Enter the number of items\n");    scanf("%d", &n);    /* Reading items into array a */    printf("Input %d values \due north",n);    for (i = 1; i <= n ; i++)    scanf("%f", &a[i]);    /* Sorting begins */    for (i = 1 ; i <= n-i ; i++){ /* Trip-i begins */       for (j = ane ; j <= n-i ; j++) {          if (a[j] <= a[j+1]) { /* Interchanging values */             t = a[j];             a[j] = a[j+i];             a[j+one] = t;          }          else          go along ;       }    } /* sorting ends */    /* calculation of median */    if ( due north % 2 == 0)       median = (a[n/ii] + a[n/2+1])/2.0 ;    else    median = a[due north/2 + i];    /* Printing */    for (i = ane ; i <= n ; i++)    printf("%f ", a[i]);    printf("\n\nMedian is %f\n", median); }

Output

When the higher up program is executed, it produces the post-obit output −

Enter the number of items 5 Input 5 values 2.3 1.2 3.8 4.six 8.9 eight.900000 4.600000 3.800000 2.300000 1.200000  Median is 3.800000

raja

Published on 25-Mar-2021 11:38:47

  • Related Questions & Answers
  • Program to notice median of ii sorted lists in C++
  • C++ Program to Find the Number of Permutations of a Given String
  • Program to Find Out Median of an Integer Array in C++
  • C# Plan to find the cube of elements in a list
  • C++ programme to notice the type of the given iterator
  • C program to find the length of linked listing
  • Programme to find folded list from a given linked listing in Python
  • C/C++ Programme to find the sum of elements in a given array
  • C++ Program to notice the median of two sorted arrays using binary search approach
  • Find Itinerary from a given list of tickets in C++
  • How to find the index of given chemical element of a Coffee List?
  • C++ Program to Find the Longest Increasing Subsequence of a Given Sequence
  • C++ Program to Find the Transitive Closure of a Given Graph 1000
  • C++ Program to Find the Longest Prefix Matching of a Given Sequence
  • C# program to find the index of an element in a List

Source: https://www.tutorialspoint.com/c-program-to-find-the-median-of-a-given-list

Posted by: williamsuniagard.blogspot.com

0 Response to "How To Find The Median Of An Array In C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel