Question

In: Computer Science

You need to write a program that reads in two integers from cin and outputs an...

You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value.

First count from the first number to the second, but stop one short.

Then, count from that number back towards the first, again stopping one short.

Continue until you reach a single number.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Remove all cout statements inside while loop, if you do not want to display the calculation process. Those are used to demonstrate the working of the program using given algorithm.

#include<iostream>

using namespace std;

int main(){

                int lower, upper;

                //asking and reading values for lower and upper bounds

                cout<<"Enter lower and upper numbers to calculate the median: ";

                cin>>lower>>upper;

                //looping as long as lower is less than upper, assuming user enter valid values

                //note: remove all cout statements inside while loop if you do not want to see the

                //calculation process

                while(lower<upper){

                                //looping from lower to upper -1

                                for(int i=lower;i<=upper-1;i++){

                                               //displaying i

                                               cout<<i;

                                }

                                cout<<endl;

                                //advancing lower by one position forward

                                lower++;

                                //now looping in reverse, from upper-1 to lower

                                for(int i=upper-1;i>=lower;i--){

                                               cout<<i;

                                }

                                cout<<endl;

                                //advancing upper by one position backward

                                upper--;

                }

                //finally displaying the current lower bound as the median value.

                //note: if there are even number of values between the user entered

                //range, then median will contain decimal values, but here we deal

                //with integers only, so the median will be an integer rounded to

                //next integer, for example, if the median is 8.5, the program will

                //display 9

                cout<<"Median is "<<lower<<endl;

                return 0;

}

/*OUTPUT*/

Enter lower and upper numbers to calculate the median: 1 9

12345678

8765432

234567

76543

3456

654

45

5

Median is 5


Related Solutions

You need to write a program that reads in two integers from cin and outputs an...
You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value. First count from the first number to the second, but stop one short. Then, count from that number back towards the first, again stopping one short. Continue until you reach a single number. Enter 3 and 9 solution: 3456789 987654 45678 8765 567 76 6
a)     Write a program that reads a list of integers, and outputs all even integers in the...
a)     Write a program that reads a list of integers, and outputs all even integers in the list. For example, if the input list is [1, 2, 13, 14, 25], the output list should be [2, 14]. b)    Based on your code for part (a), write a program that reads a list of integers, and reports True if all numbers in the list are even, and False otherwise. Example: For input [1, 2, 13, 14, 25], the output should be False. Your...
Write a program in Java that reads in a set of positive integers and outputs how...
Write a program in Java that reads in a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data 15 40 28 62 95 15 28 13 62 65 48 95 65 62 65 95 95 -999 The output is...
Write two versions of a program that reads from the user a sequence of positive integers...
Write two versions of a program that reads from the user a sequence of positive integers ending with -1, and another positive integer num that the user wishes to search for. The program should then print all the line numbers in sequence entered by the user, that contain num, or a message saying that num does not show at all in the sequence. Your program should interact with the user exactly as it shows in the following example: Please enter...
Write a Java program that reads two integers on the keyboard and displays them on the...
Write a Java program that reads two integers on the keyboard and displays them on the screen.
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Code is in C Write a program that reads integers from stdin. Once it reaches the...
Code is in C Write a program that reads integers from stdin. Once it reaches the * end of the input, it prints the smallest absolute value among those * of the numbers it read. * * For example, if * 4, 6 -3, 3, -2, 13, -4 * are read from stdin, the program should print 2. * * If the end of file is reached before any integer is seen, the * number printed should be INT_MAX (defined...
Write a recursive ARM Assembly program that takes two integers as input and outputs the greatest...
Write a recursive ARM Assembly program that takes two integers as input and outputs the greatest common divisor. *I am using Eclipse DS-5 Community Workspace with A64 Instruction Set) Use the following algorithm: // Given two integers m and n: if (m < n) gcd(n, m) if n is a divisor of m gcd(m, n) = n else gcd (m, n) = gcd (n, m % n) Your program must be recursive. You must create a function that calls itself,...
In python write a program that gets a list of integers from input, and outputs non-negative...
In python write a program that gets a list of integers from input, and outputs non-negative integers in ascending order (lowest to highest). Ex: If the input is: 10 -7 4 39 -6 12 2 the output is: 2 4 10 12 39 For coding simplicity, follow every output value by a space. Do not end with newline
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT