Thread: Help me making a weird triangle ( this question never been on this forum)

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    8

    Thumbs up Help me making a weird triangle ( this question never been on this forum)

    The users enters a number
    which will spicify the number of rows and the value of the middle number

    like
    lets say the user entered the number '7'
    the shape will be like this


    Code:
                            7                                 12 spaces from the left
                        6_7_6                             10 spaces from the left
                    5_6_7_6_5                         8 spaces from the left
                4_5_6_7_6_5_4                     6 spaces from the left
            3_4_5_6_7_6_5_4_3                 4 spaces from the left
        2_3_4_5_6_7_6_5_4_3_2             2 spaces from the left
    1_2_3_4_5_6_7_6_5_4_3_2_1         0 spaces from the left
    Note: the symbol '_' means space

    we must use 'for' functions three or two ones
    Last edited by haniunited; 11-09-2005 at 08:28 AM.

  2. #2
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    So what - can't you make a loop which counts from 7 down to 1 ?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    is it shame that i dont know?

    i just learned the 'for' tag myslef
    i bought a book

    i dont get it how to solve these questions they give

    this was part of the question

    if the number is even. we scan a number and print print this triangle

    i solved the question i still need this part?
    anyone can help ?

  4. #4
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    Well if
    i = i + 1
    count's forwards, do you imagine that
    i = i - 1
    might count backwards?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    look, my book is in spanish
    am not good in english
    need some help there

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    One possible way to start off (assume user entered their input in a var called 'choice'):

    Code:
    int i;
    for( i = 0; i < choice; ++i )
    {
        /* You will need another loop (or two) in here to do the rest. 
           Start perhaps by printing out (6-i)*2 leading spaces using a
           loop... first time through, 'i' is 0 so we print (6-0)*2 = 12
           spaces; second time through, 'i' is 1 so we print (6-1)*2 = 10
           spaces.  Or, you can do this other ways.  You might then use
           another loop to print the numbers... there are different ways
           to go about doing this. */
    }
    Just an example of how you might start.
    Last edited by hk_mp5kpdw; 11-09-2005 at 09:03 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    thanks very much
    even i did not understand almost anything

    but i will try

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    any1 knows ?
    the answer?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. Lots of us know the answer. Here, let me let you in on a few "secrets":
    Quote Originally Posted by kermi3
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.

    It appears that you are posting a homework assignment or other project.

    Please don't ask people to do all your work for you, See the announcement on Homework at the top of all forums to see what is acceptable or PM me. Basically people are happy to help, but they're not going to do it all for you.

    Show us what you've got, show your code (using code tags), or where you're confused and someone will be happy to help you I'm sure. If it's something that you absolutely don't understand how it works, like you have no clue how qsort works, then ask a general question about the function and I'm sure someone will explain it. Though they may not give you all of the code for it, but someone will explain the concept.


    On obivous homework questions especially, I like to remind people of the board's ninth guildeline, while this board is very helpful to people, make sure you have your instructor's permission before seeking help on assignments. While people on these boards are more than happy to help, we discourage people from asking for help on graded work without the instructor's permission, and we claim no repsonsibilty for any cheating or honor violations.

    Feel free to PM me with any questions.

    Good Luck,

    Kermi3

    Read and follow the above instructions, and try your post (in the same thread) again.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    man u all missed understanding me
    i am thinking
    i just not reaching the correct thing

    here is what i arrived until now
    plaese any1 post the right code if its not

    Code:
    #include<stdio.h>
    void main ()
    {
    int i,j,t,n;
    
    printf("Please enter a number\n");
    scanf("%d",&n);
    x=(n-i)*2
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n;j++)
    printf(" ");
    
    for(t=1;t<n;t++)
    printf("%d,t);
    for(t=n;t>=1;t--)
    {
    if(t!=1)
    printf("SPACE HERE THEN %d",n);
    else
    printf(%d"t)
    {
    printf("\t");
    }
    }
    Last edited by haniunited; 11-09-2005 at 10:43 AM.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    This much was already given to you in pseudo code...
    There is still some work left to do
    Code:
    #include<stdio.h>
    
    int main()
    {
        int i,j,number;
    
        printf("Please enter a number: ");
        scanf("%d",&number);
    
        for(i=number; i > 0; i--) {
            /* print leading spaces */
            for(j=0; j < (i-1) * 2; j++) {
                putchar(' ');
            }
    
            /* need a loop or 2 loops or something here */
            printf("%d \n", i);
        }
    
        return 0;
    }
    Last edited by spydoor; 11-09-2005 at 10:55 AM.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    am sorry i dont get it

    this is only my 2nd day programming

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    did you even, compile, run and try to understand the code?

    considering the time between posts I doubt it.

    giving you the complete code will not help you at all.

    I'll try to explain what's going on:
    the outer loop simply counts down from the entered number to 1

    if you remove the inner loop you'll see it prints a count down

    the next step is to print the leading spaces for each row. so we add a loop that prints from 0 to (i-1) * 2 spaces. where i is the current row.

    all that's left is to print the rest of the triangle.

    how about instead of just a printf you add a loop that prints from i (the current row) to number. something like
    Code:
    for(k=i; k <= number; k++) {
       printf("%d ", k);
    }
    printf("\n");

    now all that's left is to print the right half of the triangle. (another loop before the newline)
    Last edited by spydoor; 11-09-2005 at 11:21 AM.

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    any1?

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    And your latest attempt?

    I'll try to help with another start:
    Code:
    #include<stdio.h>
    
    void foo(int target)
    {
       int i, j;
       for ( i = 0; i < target; ++i )
       {
          /*
           * Leading spaces:
           */
          for ( j = 1; j < target - i; ++j )
          {
             printf("  "); /* two spaces */
          }
          /*
           * Initial sequence:
           */
          for ( /* leave j where it was, at (target - i) */; j <= target; ++j )
          {
             printf("%d ", j);
          }
          /* more code */
          putchar('\n');
       }
    }
    
    int main()
    {
       foo(7);
       return 0;
    }
    
    /* my output
                7 
              6 7 
            5 6 7 
          4 5 6 7 
        3 4 5 6 7 
      2 3 4 5 6 7 
    1 2 3 4 5 6 7 
    */
    Surely you can make an attempt at completing this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird question
    By SavesTheDay in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 09:08 AM
  2. Newbie Question: making a lib
    By Xterria in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2002, 12:58 PM
  3. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM
  4. Weird Question maybe
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-27-2001, 07:30 PM
  5. Replies: 1
    Last Post: 12-26-2001, 03:00 AM