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
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 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...
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
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
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT