printing multiple figures side by side
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: printing multiple figures side by side

  1. #1
    Join Date
    May 2017
    Posts
    99

    printing multiple figures side by side

    hello there . So this is my code where I had to display right triangle shapes in different position . Everything is working fine . But I have a problem using a for loop statement to display them side by side . how can I use a for loop to display them on CMD side by side . thx . this is my code below :

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      
    	for ( int i = 1 ; i <= 10 ; ++i ) {                       
    	  
    	  for ( int j = 1 ; j <= i ; j++ ) {
    
    		  if ( j <= i ) 
    
    		    cout << "*";
    		   
    	  }
    	   
    	  cout << endl;
      }
    
    	cout <<"\n";
      
       for ( int i = 10 ; i >= 1 ; --i ) {
    	  
    	  for ( int j = 1 ; j <= i ; j++ ) {
    
    		    cout << "*";
    		   
    	 
    	  
    	  }
    	   
    	  cout << endl;
      }
    
       cout <<"\n";
       
       
       int i , j;
       
       
       for (  i = 1 ; i <= 10 ; i++ ) {
    
    	   for ( j = 1 ; j < i ; j++ ) {
    
    			   cout << " ";
    	 
    	   }
    
    	      for ( j = i ; j <= 10 ; ++j ) {
       
    		        cout << "*";
    		}
    
    		 cout << endl;
    
       }
    
    
       cout <<"\n";
       
       int k , l ;
    
       
       for (  k = 1 ; k <= 10 ; k++ ) {
    
    	   for ( l = k ; l < 10 ; l++ ) {
    
    			   cout << " ";
    	 
    	   }
    
    	       for ( l = 1 ; l <= k ; l++ ) {
    
    			   cout << "*";
    
    		   }
    	   
    	   cout << endl;
    
       }
       
       
       system ("pause");
       return 0 ;
    
    
    }

  2. #2
    Join Date
    May 2017
    Posts
    99

    Re: printing multiple figures side by side

    So what I tried to do is adding another counter and try to add it in for loop with cout << " " << endl ; statement but didnt work properly any idea how to display them side by side with a for loop . thx for ur help.
    Last edited by david16; June 19th, 2017 at 12:56 PM.

  3. #3
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    689

    Re: printing multiple figures side by side

    It would be more clear if you show on the picture how exactly you need the flipped triangles needed to be shown.
    Actually, the code you've posted produces triangles printed side by side, what else it is required to do?
    Popular opinion is the greatest lie in the world.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,455

    Re: printing multiple figures side by side

    To display them side by side you need to output for each output line that part of the triangle that is required for that line would be output

    * ********** ********** *
    and the second line would be
    ** ********* ********* **

    and so on for 10 lines. You need to work out the start position for each of the 4 sets of stars and how many stars there are in each set for each line. You only need one loop to iterate for the lines. Within the loop displaying the correct number of stars and spaces can then be done in the cout statement using setw(n) (sets the minimum field width to n chars), left (left justify), right (right justify) and setfill('c') where c is the required fill character.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  5. #5
    Join Date
    May 2017
    Posts
    99

    Re: printing multiple figures side by side

    I will show you what I mean I want to display them like this

    Code:
          
    *                              **********                                     **********
    **                             ********                                         *********
    ***                           ********                                           ********
    ****                          *******                                              ******* 
    *****                        ******                                                  ****** 
    ******                       *****                                                     *****   
    *******                     ****                                                         ****
    ********                   ***                                                            ***
    *********                  * *                                                              **
    **********                 *                                                                  *
    Last edited by david16; June 19th, 2017 at 01:44 PM.

  6. #6
    Join Date
    May 2017
    Posts
    99

    Re: printing multiple figures side by side

    sorry I did my best to draw them correctly that the best I could do . So it should look something like that but I have problem adding counter to the loop so it will display them side by side . 2kaud Can u plz explain what u mean in a concrete example because I didn't get the point . thx

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,455

    Re: printing multiple figures side by side

    To print 10 stars use
    Code:
    cout << setw(10) << setfill('*') << "*";
    No loop required!

    The whole program can be done with one for statement and one cout statement.

    For 15 lines
    Code:
    *                 ***************     ***************                 *
    **                **************       **************                **
    ***               *************         *************               ***
    ****              ************           ************              ****
    *****             ***********             ***********             *****
    ******            **********               **********            ******
    *******           *********                 *********           *******
    ********          ********                   ********          ********
    *********         *******                     *******         *********
    **********        ******                       ******        **********
    ***********       *****                         *****       ***********
    ************      ****                           ****      ************
    *************     ***                             ***     *************
    **************    **                               **    **************
    ***************   *                                 *   ***************
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  8. #8
    Join Date
    May 2017
    Posts
    99

    Re: printing multiple figures side by side

    Yes this is exactly the display I was looking for but the program was asking to use a for loop to do it which is much harder than just display u see ? I'm trying to work on that but I'm not sure that it can work with a for loop because some of them it starts with 1 star the following shape is reversed from 10 to 1 and makes things harder
    Last edited by david16; June 19th, 2017 at 02:24 PM.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,455

    Re: printing multiple figures side by side

    To print one triangle
    Code:
    for (int i = 1; i <= 10; ++i)
        cout << setw(i) << setfill('*') << "*" << endl;
    Using a for loop!

    So to print 4 triangles next to each other, have a cout statement for each of the triangles and the number of * for each line is a formula based on i and then you need to pad with spaces between the triangles. So if 1 * is displayed then display say 13 spaces, if 3 * is displayed then 11 spaces and so on. The parameter for setw() for the * and space is a formula based upon i. After the spaces displayed, display the line for the next triangle etc.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  10. #10
    Join Date
    May 2017
    Posts
    99

    Re: printing multiple figures side by side

    I found this code actually which suppose to be a hint to use to make them side by side the main problem is that I have no idea how to do it . I trying it over and over for several time now. Do u get the point of that hint ? will it make them side by side or not ? thx

    Code:
    // side by side printing
      // needs additional counter for B and C who rely on decrement counting
        for(int i=1, j=10; i<=10; i++, j--){
            // A
            for(int k=1; k<=10; k++){
                std::cout << ((k <= i) ? '*' : ' ');
            }
            std::cout << "     ";
            // B
            for(int k=1; k<=10; k++){
                std::cout << ((k <= j) ? '*' : ' ');
            }
            std::cout << "     ";
            // C
            for(int k=10; k>=1; k--){
                std::cout << ((k <= j) ? '*' : ' ');
            }
            std::cout << "     ";
            // D
            for(int k=10; k>=1; k--){
                std::cout << ((k <= i) ? '*' : ' ');
            }
            std::cout << std::endl;
        }
    }

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,455

    Re: printing multiple figures side by side

    Why are you complicating what is really a simple matter. I know this is a homework exercise but...
    consider
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main() 
    {
    	const int lines = 15;
    	const int spaces = 8;
    
    	for (int i = 1; i <= lines; ++i)
    	{
    		cout << left << setw(i) << setfill('*') << "*" << setw(lines - i + spaces) << setfill(' ') << " ";
    		cout << left << setw(lines - i + 1) << setfill('*') << "*" << setw(i + spaces) << setfill(' ') << " ";
    		cout << setw(i) << setfill(' ') << " " << setw(lines - i + 1) << setfill('*') << right << "*" <<  setw(spaces) << setfill(' ') << "  ";
    		cout << setw(lines - i + 1) << setfill(' ') << " " << right << setw(i) << setfill('*') << "*" << endl;
    	}
    
    	return 0;
    }
    Last edited by 2kaud; June 19th, 2017 at 04:16 PM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  12. #12
    Join Date
    May 2017
    Posts
    99

    Re: printing multiple figures side by side

    Actually I'm not familiar with this topic . I didnt learn that yet . I will have difficulties to understand that . The exercise asked to do it with for loop I was trying to do so , then I found that hint that hint that should probably solve the mystery but I have some difficulties to adding it . I'm sorry but ur code is not familiar I can't understand it .

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,455

    Re: printing multiple figures side by side

    From post #4

    setw(n) - sets the minimum field width to n chars
    left - left justify
    right - right justify
    setfill('c') - where c is the required fill character where an output field has to be padded to the minimum field width

    So what can't you understand?

    Look at the sample in post #9, then take the cout statements in post #11 one at a time from left to right. It's quite easy!
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  14. #14
    Join Date
    May 2017
    Posts
    99

    Re: printing multiple figures side by side

    Yes its quite easy when u already learned it . My experience didnt go further than loops and the program with the hint was suppose to make things easier now I'm stuck . I guess I should keep struggling and try to see how those extra loops will help get the shapes in order although its going to be harder than ur method but I have to use it because the problem asked so :sick . But thx anyway that was useful advance course .
    Last edited by david16; June 19th, 2017 at 04:01 PM.

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,455

    Re: printing multiple figures side by side

    Quote Originally Posted by david16 View Post
    I found this code actually which suppose to be a hint to use to make them side by side the main problem is that I have no idea how to do it . I trying it over and over for several time now. Do u get the point of that hint ? will it make them side by side or not ? thx
    Have you actually tried this code? You might get a surprise! But IMO understanding it is worse than the code of post #11! I know what's its doing but its not half making a meal of it! Obviously whoever wrote it didn't know much about stream insertion! IMO I wouldn't suggest it as an example of good practice.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)