Gr4vitas Posted October 22, 2009 Posted October 22, 2009 (edited) So essentially what I'm trying to do is output this pattern on the screen. + ++ +++ ++++ +++++ ++++++ +++++++ ++++++++ +++++++++ ++++++++++ ++++++++++ +++++++++ ++++++++ +++++++ ++++++ +++++ ++++ +++ ++ + I'm having the hardest time understanding the logic behind getting this to output, all I've come up with is this. #include <iostream> #include <conio.h> using namespace std; const char space = '_'; const char draw = 'X'; int main() { int col, row, print; print = 0; for(row = 0; row<10; row++) { for(col=0; col<10; col++) { if(print == col) { cout << draw; } else { cout << space; } } print++; cout << endl; } getchar(); } Obviously this doesn't accomplish what I need to, I just cant figure it out and this is all I really come up with. anyway if anyone has a method for this that'd be really cool. by the way the above code outputs: X_________ _X________ __X_______ ___X______ ____X_____ _____X____ ______X___ _______X__ ________X_ _________X Edited October 22, 2009 by Gr4vitas Quote Share this post Link to post Share on other sites More sharing options...
CheeseMan42 Posted October 22, 2009 Posted October 22, 2009 Try this out. const char space = ' '; int main() { int col, row, print; char str[10]; print = 0; for(row = 0; row<10; row++) { strcpy( str, space ); for(col=0; col<10; col++) { if(print <= col) { strcat( str, draw ); } else { strcat( str, space ); } } print++; cout << str << endl; } getchar(); } This should get you through the first half of the pattern, and I'll let you figure out the second half. Quote Share this post Link to post Share on other sites More sharing options...
Gr4vitas Posted October 22, 2009 Posted October 22, 2009 (edited) Try this out. const char space = ' '; int main() { int col, row, print; char str[10]; print = 0; for(row = 0; row<10; row++) { strcpy( str, space ); for(col=0; col<10; col++) { if(print <= col) { strcat( str, draw ); } else { strcat( str, space ); } } print++; cout << str << endl; } getchar(); } This should get you through the first half of the pattern, and I'll let you figure out the second half. Thanks man! Really helped a lot! Just read your sig thanks for the quote haha! EDIT: It would appear that it won't build. strcat cannot convert parameter 2 from 'char' to 'const char *' Edited October 22, 2009 by Gr4vitas Quote Share this post Link to post Share on other sites More sharing options...
The Smith Posted October 22, 2009 Posted October 22, 2009 (edited) Why not just do something like: for(i=1; i<11; i++){ for(j=1; j<=i; j++) cout << "+"; cout << endl; } Edited October 22, 2009 by The Smith Quote Share this post Link to post Share on other sites More sharing options...
Gr4vitas Posted October 22, 2009 Posted October 22, 2009 Why not just do something like: for(i=1; i<11; i++){ for(j=1; j<=i; j++) cout << "+"; cout << endl; } Now how would I go about displaying that in reverse order? Quote Share this post Link to post Share on other sites More sharing options...
spectrascope Posted October 22, 2009 Posted October 22, 2009 think --; Quote Share this post Link to post Share on other sites More sharing options...
Gr4vitas Posted October 22, 2009 Posted October 22, 2009 think --; I tried lmao Quote Share this post Link to post Share on other sites More sharing options...
RimX Posted October 22, 2009 Posted October 22, 2009 Now how would I go about displaying that in reverse order? cout << endl; for(i=11; i>0; i--){ for(j=11; j>=i; j--) cout << "+"; cout << endl; } that way that should work I think. Quote Share this post Link to post Share on other sites More sharing options...
Gr4vitas Posted October 22, 2009 Posted October 22, 2009 cout << endl; for(i=11; i>0; i--){ for(j=11; j>=i; j--) cout << "+"; cout << endl; } that way that should work I think. doesnt work Quote Share this post Link to post Share on other sites More sharing options...
CheeseMan42 Posted October 22, 2009 Posted October 22, 2009 for(i=11; i>1; i--){ for(j=1; j<=i; j++) cout << "+"; cout << endl; } Might have to play with the bounds though. Quote Share this post Link to post Share on other sites More sharing options...
RimX Posted October 22, 2009 Posted October 22, 2009 (edited) public class test { public static void main(String[]args) { for(int i=1; i<11; i++) { for(int j=1; j<=i; j++) { System.out.print("+"); } System.out.println(); } System.out.println(); for(int i=11; i>0; i--) { for(int j=1; j<i; j++) { System.out.print("+"); } System.out.println(); } } } in java EDIT: the codebox cut off the last nested for statement Edited October 22, 2009 by RimX Quote Share this post Link to post Share on other sites More sharing options...
spectrascope Posted October 22, 2009 Posted October 22, 2009 here its not pretty but it works #include <iostream> #include <conio.h> int main() { int print, temp; for(print = 0 ; print<10; print++) { if(print <= 10) { temp = print; while(temp > -1) { printf("+"); temp=temp-1; } printf("\n"); } } printf("\n"); print = 9; while(print > -1) { if(print >= -1) { temp = print; while(temp > -1) { printf("+"); temp=temp-1; } printf("\n"); print--; } } system("PAUSE"); } Quote Share this post Link to post Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.