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--; }
}
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--;
}
}
Get Answers For Free
Most questions answered within 1 hours.