Thread: Need help please with a homework problem

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    9

    Unhappy Need help please with a homework problem

    the problem is explained in the comments part in the top of the code between /* and */ , I will really apreciate any kind of help about this please.

    Code:
    /*************************************************************
    
    Write a program that uses for structures to print 
    the following patters separately, one bellow the other. 
    Use for loops to generate the patters separately.
    All asterisks (*) should be printed by a single statement
    of the form cout << '*'; ( this causes the asterisks to print 
    side by side). [Hint: The last two patterns require that
    each line begin with an apropiate number of blanks.
    
    
    
    
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    
    
    ************************************************************/
    
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
    
    
    
    	// wait until user is ready before terminating program
    	// to allow the user to see the program results
    	system("PAUSE");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    well you have to use a for loop like it says

    for(...;...;...)
    {
    cout<<...;
    ....
    ....
    }

    that is one possibility of what it could look like

    and your not suposed to ask for help with homework when you havent tried the question yourself first

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    9
    yeah sorry i just read that in another post, but i have tryed all morning lol, and still i cant make it work, i tryed with one loop but wont work it looks simple but is not i am ready to kill myself lol

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Post your code that isn't working...

    Or better yet, post, in english, what you think your code should be doing. Step one to solving the problem is to figure out the solution in general terms, and then you write the code to implement that solution. So give us your idea of how to solve the problem.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    9
    I had

    Code:
    	for ( int counter = 1; counter <= 10; counter++ ){
    		cout << "*";
    	}

    but this of course would give me something like

    **********

    and if i use after the * a \n it would give me a
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *

    so maybe i have to use 2 loops to get the

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********

    and I have tryed many things but still nothing

    and I dont know if I can use nested loops? but anyways i dont know how to use nested loops

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Nested loops is just putting one loop inside another.
    Code:
    for ( ... ) {
        for ( ... ) {
            // do something
        }
    }
    You would have two counters, maybe one called starCounter, and one called lineCounter, for example. You have to figure out how to get the loops to interact with each other appropriately. Try to nest the loops and see what you come up with.

    HINT: Notice that the number of stars is always the same as the line number.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    9

    Thumbs up I got it to work thank you very much

    Code:
    /**********************************************************************
    ** CSC 213															 **
    ** Lab - 3															 **
    ** Author: Felipe Fuentealba										 **
    **																	 **
    ** Source File Lab3.cpp from page 165, exercise 2.47				 **
    **																	 **
    ** Description - Write a program that uses for structures to		 **
    **			  print the following patters separately, one			 **
    **			  bellow the other. Use for loops to generate			 **
    **			  the patters separately. All asterisks (*) should		 **
    **			  be printed by a single statement of the form			 **
    **			  cout << '*'; ( this causes the asterisks to print		 **
    **			  side by side). [Hint: The last two patterns			 **
    **			  require that each line begin with an apropiate number  **
    **			  of blanks.											 **
    **																	 **
    **		*															 **
    **		**															 **
    **		***															 **
    **		****														 **
    **		*****														 **
    **		******														 **
    **		*******														 **
    **		********													 **
    **		*********													 **
    **		**********													 **
    **																	 **
    ** The Second Loop - creates a variable for the loop,				 **	
    **				  intalize it to 1, and it is a int type			 **
    **				  variable, nStarCounter < (nLineCounter+1)			 **
    **				  means loop until the nStartCounter is				 **
    **				  less then linecounter + 1 so on the first line	 **
    **				  it will loop 1 time cause line counter + 1 == 2	 **
    **				  and if it loops around star counter will be 2		 **
    **				  and its not less then 2.							 **
    **				  The fineal part of the for nStarCounter++			 **
    **				  means when the loop has finished, increment		 **
    **				  nStarcounter by 1									 **
    **																	 **
    **********************************************************************/
    
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    using namespace std;
    
    int main(int nNumbofArgs, char* pszArgs[])
    {
    
    for ( int nLineCounter = 1; nLineCounter <= 10; nLineCounter++ ){
    		for ( int nStarCounter = 1; nStarCounter < (nLineCounter+1); nStarCounter++ ){
    			cout << '*';
    		}
    		// one is a line feed the other is a carrage return
    		cout << '\r'; // \r means move to start of the line
    		cout << '\n'; // \n means go down one spot
    	}
    
    	// wait until user is ready before terminating program
    	// to allow the user to see the program results
    	system("PAUSE");
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One More Homework Problem
    By joxerjen in forum C++ Programming
    Replies: 5
    Last Post: 10-12-2005, 04:39 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 10:24 AM