Thread: Just in case: "Odd" Triangle Challenge (for me)

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Exclamation Just in case: "Odd" Triangle Challenge (for me)

    This new thread is practically a continuation of my last Triangle problem. The reason I posted this separately is just to ensure I can receive feedback right away.


    Thanks! I understand perfectly.

    Now, I have to create a triangle that displays like this, in this case, when the height is 11:

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

    The difference is apparent. The user must input a positive odd integer value. The amount of stars printed on each line increases by 2 as we go from line 1 up to the middle part, where 11 (for instance) or the input value of stars is printed. Going downhill, the number of stars begins to decrease by 2, until only 1 star is printed (like in the beginning). I've tried to make some changes on the original triangle program code, but so far I haven't suceeded.

    My code from last time, which I fixed, looks like:

    Code:
    /*Project 53
    Herbert Ortiz
    Due: October 11, 2004
    This programs prompts the user to input a positive odd integer value:  And
    based on this information, print a triangle as the output*/
    
    #include <stdio.h>
    #include <conio.h>
    #include <assert.h>
    
    void PrintTop(int linenumber);   //this function will print the top triangle portion
    void PrintBottom(int linenumber2); //this one will print the bottom portion
    
    main()
    {
     int linenumber, linenumber2;   //local variables declared for use in the loops
     int height; //input variable is declared here
    
     printf("Enter the height of the triangle: "); //prompt
     scanf("%d", &height); //height is stored in memory for evaluation in the loop
    
     assert(height>0);  //error handler
    
     for(linenumber=0; linenumber<height; linenumber++) //the for loop dealing with top triangle portion
     {
      PrintTop(linenumber);  //function call for top portion of triangle
     }
     for(linenumber2=height; linenumber2>=0; linenumber2--) //for loop dealing with bottom triangle portion
     {
      PrintBottom(linenumber2);  //function call for bottom portion
     }
    
     system("PAUSE");
    }
    
    void PrintTop(int linenumber) //function header and definition
    {
     int i;  //variable i is used for the for loop here
    
     for(i=0; i<linenumber; i=i+2) //for loop that will print first output
     {
      printf("*");
     }
    
     printf("\n");
    }
    
    void PrintBottom(int linenumber2)  //function header and definition
    {
     int j; //j is used for the following for loop
    
     for(j=linenumber2; j>0; j=j-2)  //this loop prints the bottom triangle piece
     {
      printf("*");
     }
    
     printf("\n");
    }
    Please help me. Thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you need help? If you can print the triangle right side up, you can print it upside down. What's the problem? And stop I am sillyI am sillyI am sillyI am sillying spamming the board! Read the Announcements.

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

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    ?

    What are you talking about? I stated clearly what the triangle should look like. If you read it, it says the number of stars increases by 2 as I reach the "height" or tip of the triangle.
    The changes to be made are in my for loops, if my guessing's right.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > void PrintTop(int linenumber); //this function will print the top triangle portion
    > void PrintBottom(int linenumber2); //this one will print the bottom portion
    If you look closely, you'll see that both functions do the same thing - namely print 'n' stars

    > assert(height>0); //error handler
    Use if statements for user input not assert()
    assert() is used for trapping programming errors

    > The amount of stars printed on each line increases by 2 as we go from line 1 up to the middle part
    Try swapping which loops get the +2 and -2
    You have them on the inner loops (which result in flat triangles)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 09:44 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM
  5. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 04:13 PM