Results 1 to 10 of 10

Thread: how can you print this using C programing?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    208

    how can you print this using C programing?

    how can you code to print the following output using C programing:

    1
    1 2 1
    1 2 3 2 1
    1 2 3 4 3 2 1
    1 2 3 4 5 4 3 2 1
    this is like a pyramid, it is narrow at the top and it becomes wide as it goes to the bottom.

    and what about to print the reverse of the above:



    1 2 3 4 5 4 3 2 1
    1 2 3 4 3 2 1
    1 2 3 2 1
    1 2 1
    1
    thanks
    Last edited by nebrom; May 27th, 2007 at 12:23 AM.

  2. #2
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    Re: how can you print this using C programing?

    you mean printing to the console?
    sure you can, you just have to figure out how many spaces to print to get the desired look
    read up on printf()
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  3. #3
    Member lex_ph's Avatar
    Join Date
    Dec 2006
    Location
    Philippines
    Posts
    37

    Re: how can you print this using C programing?

    is this your homework for programming class? because this was one of the problems for us to solve during our programming class way back when i was a freshman.
    There are on 10 kinds of people: 1 who knows how to count, and the others who doesn't.

    :: VB6 Apprentice :: Adobe Photoshop Novice ::

  4. #4
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: how can you print this using C programing?

    Code:
    # include <iostream.h>
    # include <iomanip.h>   // library that has the setw output manipulator
     
    int main ()
    {
     
     char letter;      // letter is the symbol or letter made into a giant triangle
     int width;        // width is how far to go into the center of screen
     int base;         // base is how many symbols are on bottom line
     int a;            // a is how many lines down the triangle is
     int b = 1;        // b is how many symbols are displayed on each line
     int counter = 0;  // counter is how many times the loop executed
     
     cout<<"This program will make a triangle of the symbol entered."<<endl;
     cout<<"It must be an odd number for the triangle to form properly."<<endl; 
     cout<<"If an even number is entered, it will be lowered to the previous odd."<<endl;
     
     
     while(cin)        // This allows the program to loop until the user closes the window
     {
      
      cout<<"Enter a symbol or character."<<endl;
      cin>>letter;
     
      cout<<"Enter the number of characters the base should have."<<endl;
      
      cin>>base;
     
      width = (base / 2) + 5 - counter;    // This is how far into the center it should space until it starts outputting the symbol
                                           // It must first be given a value before it enters the loop
      a = 1;                               // a is how many lines down the triangle is, and natuarally it starts on the first line
     
      while(width > 5)    // It will loop and continue to output the symbol until it reaches 5 spaces from the left margin...
                          // so that everything isn't jammed against the side
      {      
       width = (base / 2) + 5 - counter;   // This is how far into the center it should space until it starts outputting the symbol
       
       cout<<setw(width);  // setw is an output manipulator in the <iomanip.h> library.  this tell the compiler how many lines
                           // to move until it first sends a character to the screen.  It is currently set to move the value of 
                           // "width" spaces to the right before it outputs.
       
       while(b > 0)  // This while loop will continue to output the desired symbol to the current line until it is equal to 1
       {
        cout<<letter;    // outputs the letter or symbol entered
        b--;             // b is decremented so only so many letters are outputted per line
       }
       cout<<endl;       // an endl is used to jump to the next line to output the next part of the triangle
       b = (a * 2) - 1;  // the number of symbols per line is found using this equation
     
       width--;          // the width is decremented so that everything is spaced properly
       b = b + 2;        // b is given 2 more symbols because it is on the next line, and every line has 2 more than the previous
       a++;              // this is how many lines into the triangle it is
       counter++;        // the counter is used to ensure proper spacing done by the width variable
      }
      cout<<endl<<endl;  // endl is used to add some space between each time the program is executed
      b = 1;             // b is returned to 1 because the program started over
      counter = 0;       // counter is returned to 0 because the program started over
     }
    return 0;
    }
    Here is some code not by me but it does kind of what you want

  5. #5
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: how can you print this using C programing?

    printf is superceded by cout
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  6. #6
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: how can you print this using C programing?

    Quote Originally Posted by Lord Orwell
    printf is superceded by cout
    Well, that really depends on whether you are using C or C++.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: how can you print this using C programing?

    yeah i know. I didn't remember that until after i had posted and then i couldn't find the thread...
    Last edited by Lord Orwell; May 28th, 2007 at 09:37 PM. Reason: lines of text were missing
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: how can you print this using C programing?

    PHP Code:
    #include <stdio.h>

    int main()
    {
        const 
    int HIGHEST_NUMBER 5;

        
    int i 0,
            
    0,
            
    1,
            
    HIGHEST_NUMBER 1,
            
    inc 1
            
    ;

        while(
    1)
        {
            for(
    != += inc)
            {
                for(
    HIGHEST_NUMBER j--)
                    
    printf("  ");

                for(
    j++)
                    
    printf("%d "j);

                for(
    j--)
                    
    printf("%d "j);

                
    printf("\n");
            }

            
    printf("\n");

            if(
    inc == -1)
                break;

            
    HIGHEST_NUMBER;
            
    inc = -1;
        }

        return 
    0;

    I don't live here any more.

  9. #9
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    Re: how can you print this using C programing?

    Dude, you just did his homework for him!
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  10. #10
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: how can you print this using C programing?

    I was bored.

Posting Permissions

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



Click Here to Expand Forum to Full Width