🔒 Closed C++ po pa help

Status
Not open for further replies.
C++:
#include <iostream>
using namespace std;

int main()
{
    int array[] = {7,4,5,2,9,3,1,1};
    int n = sizeof(array) / sizeof(array[0]);
    int temp;
    float median;

    //sort the array first
    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)
        {
            if (array[i] > array[j])
            {
                //swap values
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

    //median
    //determine if array has odd or even number of items
    if (n % 2 == 0)//even
    {
        median = (array[(n - 1) / 2] + array[n / 2]) / 2;
    }
    else //odd
    {
        median = array[n / 2];
    }

    cout << "Median = " << median;

    getchar();
    getchar();
}
 
Status
Not open for further replies.

Similar threads

About this Thread

  • 1
    Replies
  • 345
    Views
  • 2
    Participants
Last reply from:
Aoshinomori

Trending Topics

Online now

Members online
1,078
Guests online
1,761
Total visitors
2,839

Forum statistics

Threads
2,286,688
Posts
29,039,668
Members
1,217,128
Latest member
distroy123
Back
Top