Thread: pyramid char..

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68

    pyramid char..

    hey,, I have new problem.. hehehe..
    how can we make a pyramid by using char?

    eq
    w
    qwe
    aqwer

    I only can make a triangle..
    Code:
    #include<stdio.h>
    #include<string.h>
    
    void stringkiri(char st[], int n, char strhasil[]);
    
    int main()
    {
    int i, panjangst;
    char st[]="testing";
    char sthasil[10];
    panjangst= strlen(st);
    
    for(i=0; i<panjangst;i++)
    {
    stringkiri(st,panjangst-i,sthasil);
    puts(sthasil);
    }
    getchar();
    }
    
    void stringkiri(char st[],int n,char strhasil[])
    {
    int i;
    for(i=0;i<n;i++)
    	{
    		
    		strhasil[i]=st[i];
    			
    	}
    	strhasil[i]='\0';
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Each row of the square has the same length. You will print spaces as needed, before you print the other letters.

    Each row, you print one fewer space char's ( ' ' ), before you start printing letters for that row. Which means in your loop, you're going to decrement the number of spaces, as you decrement the loop counter. Your counter should start with the one less than the center column of the first row.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    yeah,, I know the theory,, but I can make it into code in C.. T_T
    I can make it, if the variable is int.. but I am not sure too...

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Before you can do this pyramid, you have to know how many rows the pyramid will require. For each row, you need one more space. (I used *'s here for easy counting).

    You were on the right track with using strlen().

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      int i, j, k, row, maxCol, maxRow;
      char *letters = {"abcdefghijklmnopqrstuvwxy"};
      maxCol = strlen(letters);
    
      /*this was the toughest part. You can't print up the pyramid right,
      unless you know how many rows it will contain, before you start
      */
      for(i = 1, j = 1; j < maxCol; i++)
        j = (2*j + 2) ;
    
      //if you use postfix increment (i++), you need to subtract 1
      //to get the goal index you want, frequently.
      maxRow = i-1;
    
      j = k = 0;
      printf("\n\n\n");
      for(row = 0; k < maxCol; row++) {  //handles the rows
        for(i = j; i < maxRow; i++) {           
          printf("%c", '*');                         //the star's
        }
        
        for(i = 0; i <= row+j; k++, i++) { //the letters
          printf("%c", *letters+k);
        }
        putchar('\n');
        j++;
        
      }
    
      printf("\n\n\t\t\t     press enter when ready");
      i = getchar();
      return 0;
    }
    /*
    ****a
    ***bcd
    **efghi
    *jklmnop
    qrstuvwxy
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM