🔒 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
  • 355
    Views
  • 2
    Participants
Last reply from:
Aoshinomori

Trending Topics

Online now

Members online
1,260
Guests online
4,266
Total visitors
5,526

Forum statistics

Threads
2,308,767
Posts
29,186,977
Members
1,191,200
Latest member
Rolo
Back
Top