loop program to display this: Enter height: 5 * * * * * * * * *
what do you have so far?
I think I know the answer ;)
i have this, it's different, i can't seem to change the loop statement :( #include <iostream.h> #include <conio.h> int main() { int number=0; int counter1=0; cout<<"Enter the size of the shape: "; cin >> number; counter1=-number; while(counter1 <=number) { for (int counter2 = -number; counter2 <= number; ++counter2) { if (abs(counter1) == abs(counter2)) { cout<<"x"; } else { cout<<" "; } } ++counter1; cout<<endl; } getche (); return 0; }
have you tried drawing it out with a regular pen and paper for these cases? : height = 1, 2,3 ?
yup, for example when i enter 3 x x x x x x x x x x x x x
i got a new set of codes, but still not working, i can't figure out what is wrong :( #include <iostream.h> #include <math.h> #include <conio.h> int main() { int num, length; clrscr(); cout<<"Enter the size: "; cin >> length; num=length for(int i=1; i<length; i++) { for(int j=1; j<length; j++) { if((i==j) && (j==num)) cout<<"*"; else cout<<" "; } num--; cout<<endl; } getche (); return 0; }
^ tl;dr summary : think about it some more... work it out with pen and paper on what the computer is actually doing.
try this : #include <iostream.h> #include <conio.h> int main() { int number=0; int counter1=0; cout<<"Enter the size of the shape: "; cin >> number; counter1=-number\(/2\); while(counter1 <=number) { for (int counter2 = -number\(/2\); counter2 <= number\(/2\); ++counter2) { if (abs(counter1) == abs(counter2)) { cout<<"x"; } else { cout<<" "; } } ++counter1; cout<<endl; } getche (); return 0; }
Join our real-time social learning platform and learn together with your friends!