I want to make this loop display 1 2 4 8 16 32 64 128 64 32 16 8 4 2 1 but i cannot figure out a way to do so
Please help
this is the code so far
while (!kbhit ())
{
for (int k = 1; k <= 128; k *= 2)
{
for (int l = 128; l >= 1; l /= 2)
{
cout << l << " ";
Sleep (75);
}
cout << k << " ";
Sleep (75);
}
}
while (!kbhit ())
{
for (int k = 1; k <= 128; k *= 2)
{
for (int l = 128; l >= 1; l /= 2)
{
cout << l << " ";
Sleep (75);
}
cout << k << " ";
Sleep (75);
}
}
I'll assume your teacher's kbhit() function just means hitting the Enter/Return key repeatedly or something. Kind of like using cin.ignore() and turning that into a pause function or something.
I also have a hunch your sleep function pauses for 75 milliseconds, is that correct?
But you could always make two loops, such that:
1 2 3 4 5 6 7
int n;
std::cin >> n;
for( int a = 1; a < n; a *= 2 )
std::cout << a << ' ';
for( int b = n; b >= 1; b /= 2 )
std::cout << b << ' ';