[try Beta version]
Not logged in

 
Loop

Dec 1, 2015 at 11:36pm
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);
}
}
Dec 1, 2015 at 11:48pm
First of all, get brackets around your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}
}



Also, what is kbhit? What exactly does it return?
Last edited on Dec 1, 2015 at 11:49pm
Dec 1, 2015 at 11:52pm
kbhit is just hitting the keyboard. My teacher has been saying to use it so i did
Dec 2, 2015 at 12:32am
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 << ' ';


Dec 2, 2015 at 12:37am
As a tangent, what exactly does sleep do? Does it just stop the program for runnign for 75 milliseconds?
Dec 2, 2015 at 12:45am
Thanks that works great
Dec 2, 2015 at 12:46am
Sleep slows the program down for the set amount of milliseconds
Topic archived. No new replies allowed.