1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ankoles [38]
3 years ago
5

C++You are given an array x of int elements along with an int variable n that contains the number of elements in the array . The

re are no duplicates in this array . You are also given an int variable m that has been declared . Assign to m the median value of the array .NOTE: The median of a set of numbers is the number in the set such that there are as many numbers above that value as there are below. If the set has an even number of members, the median is the average of the pair of numbers such that there are as many numbers above the pair as there are below.EXAMPLE 1: Given 5 8 1 9 7 the median is 7 because there are two numbers below (1 5) and two numbers above (8 9).EXAMPLE 2: Given 3 7 1 4 6 9 the median is the average of 4 and 6 because there are two numbers above 4 and 6 (7 9) and two numbers below (3 1).
Computers and Technology
1 answer:
Firdavs [7]3 years ago
3 0

Answer:

C++ Code

#include <iostream>

using namespace std;

int median(int x[], int n)

{

   for (int i = 0; i < n-1; i++)

   {

   for (int j = 0; j < n-i-1; j++)  

       if (x[j] > x[j+1])

       {

           int temp = x[j];

           x[j] = x[j+1];

           x[j+1] = temp;

       }

   }

   if( n%2 == 0 )

   {

       return (x[ (n/2) -1] + x[n/2])/2;

   }

   else

   return x[n/2];

}

int main()

{

   //example 1

   int x[] = {5,8,1,7,9};

   int n = 5;

   

   int m = 0;

   m = median(x,n);

   cout << m << endl;

   

   //example 2

   int x2[] = {3,7,1,4,6,9};

   int n2 = 6;

   m = median(x2,n2);

   cout << m << endl;

}

Explanation:

The main program contains the examples given in the question.

Function Explanation: The function int median(int x[], int n) takes two arguments, one is the array x and the other is the size of the array n.

First, the function sorts the elements in ascending order.

After that, it is calculated whether n is even or odd. If n is an even number,  The middle numbers can be obtained as follows:

Divide n by 2 to get the first middle element.

The second middle element is the element before first middle element, therefore

x[ (n/2 )-1] + x[n/2] gives us the sum of middle elements, where as dividing it by 2 gives us the avg of the two numbers.

If n is an odd number, than the middle element can be simply returned by dividing n by 2.

The working of the program is as follows

Array 1: 5 8 1 9 7

After sorting, it becomes: 1,5,7,8,9.

Array indices are : 0,1,2,3,4. Middle element is at index 2, n = 5, therefore n/2 = 2.5 (This is rounded down to 2 because result is stored in integer)

Array 2: 3 7 1 4 6 9

After sorting, it becomes: 1 3 4 6 7 9

Array indices are : 0,1,2,3,4,5. Middle element is at index 2 and 3,n = 6, therefore it can be obtained by n/2-1 and n/2 (n/2 = 3 and n/2 - 1 = 2)

You might be interested in
Whats is a better game?
Jobisdone [24]

Answer:

The first two are the best ones I see here, because I play them. Not sure how else to say that. Please make sure to also use brainly for school, though. Thanks!

3 0
2 years ago
Why is the human immunodeficiency virus so deadly?
damaskus [11]

<em> </em><em>I</em><em> </em><em>think</em><em> </em><em>the</em><em> </em><em>answer</em><em> </em><em>is</em><em> </em><em>D</em><em>.</em>

<em>It</em><em> </em><em>kills</em><em> </em><em>the</em><em> </em><em>cells</em><em> </em><em>that</em><em> </em><em>make</em><em> </em><em>antibodies</em><em>.</em>

<em>Hope</em><em> </em><em>this</em><em> </em><em>will</em><em> </em><em>help</em><em> </em><em>u</em><em>.</em><em>.</em>

8 0
3 years ago
What are the classifications of computer
anygoal [31]

Answer:

Computers differ based on their data processing abilities. They are classified according to purpose, data handling and functionality.Computers can be classified, or typed, in many ways. Some common classifications of digital computers are summarized below.

Microcomputers (personal computers).

Minicomputers (midrange computers).

Mainframe computers.

Supercomputers

Microcomputers became the most common type of computer in the late 20th century

8 0
3 years ago
Knowledge of how Microsoft Word toolbars are designed and organized allows the user to learn the program more easily and use it
Anvisha [2.4K]
A. true 

have a great day
4 0
4 years ago
Read 2 more answers
How does Botnet operate and communicate.
zhuklara [117]

Answer:

 Botnet is typically operate by the internet relay networking and domain. The botnet are generally received specific type of the instruction from the control server.

The main purpose and structure of botnet is basically depend upon the various type of instruction and structure.

The botnet is communicated with the help of IRC ( Internet relay chat) and it is basically communicated with the chat server for transferring the data to the client. The IRC network basically use simple network with low bandwidth for communication with the host of the botnet.

4 0
3 years ago
Other questions:
  • A knowledge and skills test is a tool support managers use when selecting new employees; it measures an applicant's understandin
    13·1 answer
  • Which LOOKUP function will you use if you want to search for a value in the columns?
    5·1 answer
  • 2 inventions revolutionized photography, what are they?
    10·1 answer
  • What year did Elvis die? Right answer 1977.
    14·1 answer
  • What are some effects of the holocaust? She walked along the river until a policeman stopped her. It was one o'clock, he said. N
    8·1 answer
  • Finish the program to compute how many gallons of paint are needed to cover the given square feet of walls. Assume 1 gallon can
    9·1 answer
  • I cant tell if tubbo is a bee or a goat <br>or could he be human in the smp
    12·1 answer
  • Can someone suggest how to manage a group when you are the group leader?​
    10·2 answers
  • A(n) ____ tells the compiler or interpreter that the character that follows it has a special purpose.
    14·1 answer
  • what statement about constructors is false? group of answer choices all constructors are passed a pointer argument constructors
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!