🔒 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.

About this Thread

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

Trending Topics

Online now

Members online
1,022
Guests online
1,139
Total visitors
2,161

Forum statistics

Threads
2,272,806
Posts
28,945,415
Members
1,236,418
Latest member
yesitsme
Back
Top