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.

Enter 3 and 9

solution:

3456789

987654

45678

8765

567

76

6

Solutions

Expert Solution

#include <iostream>

using namespace std;

int main() {
    int start, end;
    cin >> start >> end;
    if (start < end) {
        while (start < end) {
            for (int i = start; i < end; ++i) {
                cout << i << " ";
            }
            cout << endl;
            for (int i = end-1; i > start; --i) {
                cout << i << " ";
            }
            cout << endl;
            start++;
            end--;
        }
    } else {
        while (start > end) {
            for (int i = start; i > end; --i) {
                cout << i << " ";
            }
            cout << endl;
            for (int i = end+1; i < start; ++i) {
                cout << i << " ";
            }
            cout << endl;
            start--;
            end++;
        }
    }
    return 0;
}

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.
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...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT