a question

This is a discussion on a question within the C++ Programming forums, part of the General Programming Boards category; this is the output * ** *** **** ***** ****** ******* ******** ********* ********** and i want to right a ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    a question

    this is the output
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    and i want to right a programme to print this output by using for loop and one printf statement printf( "*" )and it should print the whole pattern ,please can you help me
    the stars are right justified opposite of the picture given means not left justified
    Last edited by kuwait; 10-29-2002 at 03:30 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    A nested for loop will do. The first one looping as many times as the pyramid is high, and the second (inner) one starts with 1 loop, then increasing by 1 per outer loop.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    but how it wouldn't be right justified how could i make it?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Print the string " " (a space) before the stars (Height - i) times, where Height is the height of the pyramid (nr of rows) and i is the current line number (when looping).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    Make two loops inside the large one, one that counts out totalLines-currentLine spaces, and another that counts out currentLine '*'s.

    ~Inquirer

    [EDIT]
    Beaten again...
    [/EDIT]
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  6. #6
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    im surprised no one said anything about him doing his own homework.
    I came up with a cool phrase to put down here, but i forgot it...

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    No one is giving away the farm here.

    The code is still up to kuwait.

    It's something of a judgement call, although I do see your point. Rather a fine line sometimes.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    i know how to do it my problem is how to make it right justified


    for(i=1;i<=10;i++){
    for(j=1;j<=i;j++)
    printf("*");
    printf("\n");
    }

    i did this but it wouldn't be right justified how can i do it what should i use pleas can u help?
    And thanx for all

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    You'll need to know the screen size before justifying it to the right.

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    sorry
    the picture which i put is wrong it is right justified i only want to put blanks 9 blanks before the first star
    8blanks befor the 2nd two stars and so on
    how to do this

  11. #11
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    Code:
    for(int i=1; i<=10; i++)
     {
      for(int k=10-i; k>0; k--)
      {
       printf(" ");
      }
      for(j=1; j<=i; j++)
      {
       printf("*");
       printf("\n");
      }
     }
    You want 10-i spaces before each line of stars right?
    so just print 10-i spaces. Meaning, print a space 10-i times.
    - Well that's my 4c Australian.

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    it doesn't work sorry
    i tried it

    i also need blanks before the columns not just the rows i want a triangle of stars to appear
    thanx for ur help

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Those triangles are boring, give your teacher something more interesting and you may get some extra credit.
    Code:
    #include <iostream>
    
    const int N = 16;
    
    int main ()
    {
      for ( int n = 0; n < N; ++n ) {
        for ( int k = 0; k <= n; ++k )
          std::cout.put ( ( ~n & k ) ? ' ' : '*' );
    
        std::cout.put ( '\n' );
      }
    
      std::cin.get();
    }
    -Prelude
    My best code is written with the delete key.

  14. #14
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    You can use the setw() fxn in the header file <iomanip> this will let you have a right-justified display.

    or you can add another for loop to your nested loops with a decrementing counter that prints out space ' ' characters.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  15. #15
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    hi there

    i'm not able to answer it this way it requires to solve it by using for loop and using just <stdio.h> libary

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21