Question

Inefficient Median: You need to write a program that reads in two integers from cin and...

Inefficient Median: 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.

So for the case of the first test I have 3 and 10

and the output should look like this

3 4 5 6 7 8 9

9 8 7 6 5 4

4 5 6 7 8

5 6 7

7 6

6

I have other questions of similar style but don't quite work. as it often leaves the 10 on the first line but in the output it needs to be gone. thank your for your help.

Homework Answers

Answer #1

#include<iostream>
using namespace std;
int main()
{
   int a,b,i,j,k=0;
   //input two numbers
   cout<<endl<<"Enter two numbers";
   cin>>a>>b;
   //assign the greatest number to b and smallest number to a,if user enters the
   //first number as big and second as small
   if(a>b)
   {
      i=a;
      a=b;
      b=i;
   }
   //loop to continue the number of times toprint
   do
   {
      if(k==0) //condition to print increasing order
      {
         for(i=a;i<b;i++)
              {
              cout<<" "<<i; //print the value
              }
              k=1;
              b=b-1;//update the value of b
       }
      else
       { //loop to print in decreasing order
           for(i=b;i>a;i--)
           {
           cout<<" "<<i; //print the value
           }
           k=0;
           a=a+1;//update the value of a
       }  
       cout<<endl;  
      
   }while(a!=b); //termination condition
  
}

OUTPUT

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
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...
You need to write a program that reads in two integers from cin and outputs all...
You need to write a 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.
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 in C++) Write a program that reads in two numbers and, if the input is...
(Write in C++) Write a program that reads in two numbers and, if the input is valid, outputs 2 times the product of the integers that lie between the two values (including the values themselves). If either number is not an integer, or if the first number is not less than the second number, just output an error message. The sample runs below should give the idea. User inputs are in bold. Important Notes: Your program should use a loop...
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 the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. You can assume that the list will have at least 2 integers and less than 20 integers. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 To achieve the above, first read...
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T...
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T and (2) a positive integer N. It then reads N integers and counts the numbers that are greater than T and the numbers than are less than T. It then prints out these two counts. Example What is the threshold value? 7 How many values? 5 Enter 5 values: 6 7 9 9 8 3 values are greater than 7 1 values are less...
/*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...
Use C++ Write a program that first reads in how many whole numbers the user wants...
Use C++ Write a program that first reads in how many whole numbers the user wants to sum, then reads in that many whole numbers, and finally outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the numbers just once each and the user can enter them...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT