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
What are wizard ranks in brainly​
Hunter-Best [27]

Answer:

Brainly gives special ranks to users who give outstanding performance in a specific subject. These ranks are known as Wizard rank. Specifically for only one person in each subjects: Maths, Chemistry, Physics, Biology, and English.

Explanation:

4 0
3 years ago
Read 2 more answers
First person to answer gets free brainlist
andrew11 [14]

Answer:

TYSM!!!

Explanation:

4 0
3 years ago
Read 2 more answers
A switch that is configured to use the Spanning Tree Protocol will place a port in ____________ mode if sending traffic to it ma
Rashid [163]

Answer:

Blocking Mode    

Explanation:

Spanning Tree Protocol is used to allow path redundancy in the network without creating cycles/circles also called loops.

When two parts of the switched network are connected via two or more Layer 2 switches this result in a loop.

This affects the performance of the network as the result of broadcast packets flooding.

STP puts one port of the switch to forwarding mode and the rest of the ports within the same part of the network to the blocking mode to avoid broadcast packet flooding. STP puts all the ports that are allowing redundant paths to blocking mode and the one port that is left after this is placed in forward mode.

Spanning Tree Algorithm is used by STP to determine the optimal path  of switch to the network.

Bridge Protocol Data Units are used to share the information about the optimal path determined by the spanning tree algorithm with other switches.

This information helps STP to eliminate the redundant paths.

So this is how STP allows only one active path to the destination while blocking all other paths to avoid switching loop.

6 0
3 years ago
1 Let Σ= {Ac,BA,bcb,cd,ab,d,i}. Find the length for the given below strings
Zepler [3.9K]

Answer:

What in the world

Explanation:

4 0
3 years ago
Thinking actively promotes critical thinking. Explain how thinking actively promotes critical thinking
pav-90 [236]

A critical thinker is someone who is actively engaged in a thought process. Active thinking is that process of making judgments about what has happened while critical thinking involves a wide range of thinking skills that eventually leads to a desired outcome. Active thinking prompts learning during complex problems situations and provides learners with an opportunity to think about how a problem will be solved. As a result, it will help them evaluate, analyze, and intercept information.

8 0
3 years ago
Other questions:
  • What filter gives a “squeezed” effect to an image?
    14·2 answers
  • Give one example of a civil engineering structure.
    5·1 answer
  • Harmful programs used to disrupt computer operation, gather sensitive information, or gain unauthorized access to computer syste
    7·1 answer
  • A golf ball is at rest on the grass. What must happen to cause the ball to move?
    5·1 answer
  • PLEASE HELP! One of the nice byproducts of joining a club, organization, community service project, or service learning project
    6·1 answer
  • A(n) ____ local area network is a network in which devices use high-frequency radio waves to communicate with a base station, wh
    11·1 answer
  • Write a programmer defined function that compares the ASCII sum of two strings. To compute the ASCII sum, you need to compute th
    6·1 answer
  • When would the Jerusalem virus attack?<br> 1. Friday the 13th<br> 2. Wednesday the 13th
    13·2 answers
  • In the year, , the American Department of Defense put a military research network, called ARPANET online.
    5·1 answer
  • What is boot sector virus​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!