Question

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

You need to write a program that reads in two integers from cin and outputs a horribly inefficient 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 9

Out:

3 4 5 6 7 8 9

9 8 7 6 5 4

4 5 6 7 8

8 7 6 5 5 6 7

7 6

6

I get close but I have extra numbers at beginning or end of lines.

int main() {
int n1;
int n2;
cin >> n1;
cin >> n2;
int c = n2 - n1;
int line_num = 0;
int temp;

while(c != -1) {
int count = n1 , i;
if (line_num % 2 == 0) {
for (i = 0; i <= c; ++i) // odd lines
cout << count++ << " ";
temp = n1;
n1 = n2;
n2 = temp +1; }
else { for (i = 0 ; i <= c; ++i) // even
cout << count-- << " ";
temp = n1;
n1 = n2;
n2 = temp-1; }
line_num++;
cout << "\n";
c--; }
}

Homework Answers

Answer #1

If you have any doubts, please give me comment...

#include <iostream>

using namespace std;

int main()

{

    int n1;

    int n2;

    cout << "Enter: ";

    cin >> n1 >> n2;

    cout << "Out: " << endl;

    int c = n2 - n1 - 1;

    int line_num = 0;

    int temp;

    while (c != -1)

    {

        int count = n1, i;

        if (line_num % 2 == 0)

        {

            for (i = 0; i <= c; ++i) // odd lines

                cout << count++ << " ";

            temp = n1;

            n1 = n2-1;

            n2 = temp + 1;

        }

        else

        {

            for (i = 0; i <= c; ++i) // even

                cout << count-- << " ";

            temp = n1;

            n1 = n2;

            n2 = temp - 1;

        }

        line_num++;

        cout << "\n";

        c--;

    }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that reads a string and outputs the number of lowercase vowels in the...
Write a program that reads a string and outputs the number of lowercase vowels in the string. Your program must contain a function with a parameter of a char variable that returns an int. The function will return a 1 if the char being passed in is a lowercase vowel, and a 0 for any other character. The output for your main program should be: There are XXXX lowercase vowels in string yyyyyyyyyyyyyyyyyyyyyy Where XXXX is the count of lowercase...
A.6 ... static int x = 1; int y = x * 2; void t1() {...
A.6 ... static int x = 1; int y = x * 2; void t1() {                 y++;                 cout << "x: " << x << " | y: " << y << endl;                 y += 1;                 x -= -1; } void t2() {                 int* x = &y;                 cout << "x: " << x << " | y: " << y << endl; } void t3() {                 int y = x;                 static int x...
write a in c++ program that reads in two integers from cin and outputs all of...
write a in c++ program that reads in two integers from cin and outputs all of the integers between those values, except powers of three. You can assume that the first value is strictly less than the second value If you were to enter 5 and 30 you would get 6,7,8,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,28 so I’m not sure how to get rid of the powers of 3, so 3^1, 3^2 ... etc
In Coral,write a program that reads a list of 10 integers and outputs those Integers in...
In Coral,write a program that reads a list of 10 integers and outputs those Integers in Reverse. For coding Simplicity, follow each output integer by a space, including the last one. Then, output a new line. Example if the input is 2 4 6 8 10 12 14 16 18 20, the output is: 20 18 16 14 12 10 8 6 4 2 to achieve above first read the integers into an array. Then output the array in reverse
Write a C++ program that would report all of the Min and Max. example: 1. 95...
Write a C++ program that would report all of the Min and Max. example: 1. 95 2. 95 3. 80 4. 80 lowest 80 on test(s): 3, 4 highest 95 on test(s): 1, 2 I have started it, however, this only outputs one of the Min and Max and not all of the other instances. #include<iostream> using namespace std; int main() { int grades[10] , min , max , minIndex , maxIndex ; int n , choice , rt ,...
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...
Write a program that accepts as input the mass, in grams, and density, in grams per...
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places. ** Add Comments ** Print Name and Assignment on screen ** Date ** Submit .cpp file. Demo // This program uses a type cast to avoid an integer division. #include <iostream> // input - output stream #include <fstream> //working file...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT