loops
hello, I'm trying to figure out loops and so far, I think I'm being very inefficient.
I'm trying to output this.
5 7 9 11 10 8 6 4
3 6 9 8 5 2
2 6 5 1
|
This is the code I have, but looks terrible to me. Any help or guidance would be appreciated, thank you for your time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
using namespace std;
int main()
{
int i =5;
while (i!=13)
{
cout<<i<<" ";
i+=2;
}
i=10;
while(i!=2)
{
cout <<i<<" ";
i-=2;
}
cout<<endl;
i=3;
while(i!=12)
{
cout<<i<<" ";
i+=3;
}
i=8;
while(i!=-1)
{
cout<<i<<" ";
i-=3;
}
cout<<endl;
i=2;
while(i!=10)
{
cout<<i<<" ";
i+=4;
}
i=5;
while(i!=-3)
{
cout<<i<<" ";
i-=4;
}
cout<<endl;
system("pause");
return 0;
}
|
Last edited on
I did this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
using namespace std;
int main()
{
int i= 5;
signed int c= 2;
int max = 11;
cout << "start" << endl;
while(i!=1)
{
for(int j=0;j<2;j++)
{
for(int k=0;( (k<4) && (i<=max) && (i>=1) ) ;k++)
{
cout << i << "\t";
i+=c;
}
i-=c;
i -= (i>2) ? 1 : 0;
c*=-1;
}
max--;
c++;
cout << endl;
}
getchar();
return 0;
}
|
Topic archived. No new replies allowed.