Results 1 to 4 of 4

Thread: Program : How to calculate the median in C++?

  1. #1
    Join Date
    Jan 2010
    Posts
    22

    Program : How to calculate the median in C++?

    Hi, I am the MCA student. I don't have the good programming knowledge. I have to complete my C++ assignment. But I can't find the answer of one question in which I have to write a program that calculate the median of given number. The following are the values:
    int numbers[]={53,2,3,5,59,7,11,13,17,41,53,…
    I had tried it from last hour,but I could not make it.
    So anyone has the answer of my query,Please help me!

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    re: Program : How to calculate the median in C++?

    Hello, I suggest you to try the following code of lines that is similar to your question only you can use whatever the values that are given in your example:

    #include <codecogs/stats/moments/median.h>
    #include <iostream>
    int main()
    {
    double
    p[6] = {0.2, 1.24, 0.5, 2.54, 2.3, 0.3},
    q[6] = {1, 2, 3.2, 4, 5},
    mx = Stats::Moments::median<double>(6, p),
    my = Stats::Moments::median<double>(5, q);
    std::cout << "The median of the p array is: " << mx << std::endl;
    std::cout << "The median of the q array is: " << my << std::endl;
    return 0;
    }

    Output:

    The median of the p array is: 0.87
    The median of the q array is: 3.2

  3. #3
    Join Date
    Nov 2005
    Posts
    1,323

    Code of Median:C++

    I guess you have the little bit of knowledge about the C++ language. you can also calculate the median using the Bubble sort. The following syntax helps you to calculate the median:

    void med( int setofscores[], int tst)
    {
    bublSort(stdScores[][EXM], int, int);
    printf("\n\n The sorted array is = ");
    printArray(setofScores, int tst);
    printf("\n\n The median is %d\n\n", setofscores[][ EXM / 2] );
    }
    void bublSort(int stdScores[][EXM], int hld, int tst)
    {
    int pas;
    int p, q;
    int hld;

    for (pas = 1; pas < tst; pas++)
    {
    for (j = 0; j < tsts-1; j++)
    {
    if (stdScores[p][q] > stdScores[p][q + 1]) {
    hld = stdScores[p][q];
    stdScores[p][q] = stdtScores[p][q + 1];
    stdScores[p][q + 1] = hld;
    }
    }
    }

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Cpp

    I guess you know what is the meaning of the median. The Median is the value that is located in middle of the given sequence of number. If the number of data which is given is odd then the middle value is the median. But if the number of data is even then average of the two middle values is the median.
    I think you have to try the algorithm that uses the Smallest function of bubble sort. I hope you got your answer. Have Nice Day!

Similar Threads

  1. Replies: 5
    Last Post: 16-03-2011, 06:22 PM
  2. program to calculate definite integration
    By MaryJ in forum Software Development
    Replies: 5
    Last Post: 24-09-2010, 10:06 PM
  3. What is the shell program to calculate addition of two numbers?
    By Shophia_D in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 02:34 PM
  4. What is the Program to calculate the percentage?
    By Ekpah in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 12:30 PM
  5. Calculate Average in C program
    By BoanHed in forum Software Development
    Replies: 3
    Last Post: 30-10-2009, 11:40 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,943,616.43877 seconds with 17 queries