Thread: pyramiding asterisks " * "

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    pyramiding asterisks " * "

    I need some your comments for this lab exercise of mine:

    -Our instructor told as to print an array of "pyramiding asterisks"
    just like the one below:

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

    Using only one(1) asterisk(*) on the source code.
    ---
    He gave us 2 clues to experiment with,...

    FIRST clue:

    #include<stdio.h>
    main()
    {
    int i;
    clrscr();
    for(i=0; i<=10; i++)
    {
    gotoxy(1+i, 17);
    printf(" RAY");
    delay(15000);
    }
    getch();
    }
    ======

    SECOND clue:

    #include<stdio.h>
    main()
    {
    int i;
    clrscr();
    for(i=0; i<=10, i++)
    {
    gotoxy(1+i, 1+i);
    printf("*"
    delay(15000;
    }
    getch();
    }
    -----------------------------------------

    I made my own source code, but it seems so awkward and stupid !

    #include <stdio.h>

    main()
    {
    char A;
    clrscr();
    A = '*';
    gotoxy(40,12); printf("%c", A); gotoxy(39,13); printf("%c", A);
    gotoxy(40,13); printf("%c", A); gotoxy(41,13); printf("%c", A);
    gotoxy(38,14); printf("%c", A); gotoxy(39,14); printf("%c", A);
    gotoxy(40,14); printf("%c", A); gotoxy(41,14); printf("%c", A);
    gotoxy(42,14); printf("%c", A); gotoxy(37,15); printf("%c", A);
    gotoxy(38,15); printf("%c", A); gotoxy(39,15); printf("%c", A);
    gotoxy(40,15); printf("%c", A); gotoxy(41,15); printf("%c", A);
    gotoxy(42,15); printf("%c", A); gotoxy(43,15); printf("%c", A);
    getch();
    }


    In fairness, it works...! Using only one(1) asterisk (*).

    But of course, I need some of your comments and suggestions.
    I kept on using "printf("%c", A);" -which is very impractical !

    My queries on this matter raise some thoughts on:

    1. "How do I tile a series of text(s) on the screen?"
    2. "I know there's a simple source code,... what could be those new concepts?"

    --

    That's all,.. Thanks !!

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    Correction !!!!!

    Sorry,.. Some corrections....

    The tile of asterisks should form a triangle formation..
    like a Christmas Tree... (not a half)


    8888 * 8888
    888 *** 888
    88 ***** 88
    8 ******* 8


    nevermind the eigth's(88) just the asterisks(***)

    I stand corrected...

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    30
    The foll prg will look like this

    *
    * *
    * * *
    * * * *

    u can use it n make the necessary modifications


    Code:
    main()
    {
       int i,j=1;
       
       while(j<=4)
       {
          i=0;
         
          while(i<j)
          {
             printf ("*");
             i++;
          }
          printf ("\n");
          j++;
       }
    
    }

    U can also use a for loop to do it.

    hope i've been of some help...

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    Oi !!

    Thanks for your ideas !!

    Great help for a beginner like me !!

    I'll take full note of everything you comment...


    Thanks !!!

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    - If you want the tree to be like a christamast tree
    - As i underrstand you have to use gotooxy().

    here's mine:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main( void )
    {
    	int i, j, number;
    
    	printf( "How many flowrs? " );
    	scanf( "%d", &number );
    
    	for( i=number; i > 0; i-- ){
    		gotoxy( number-i+1, i+2);
    		for( j=i*2; j>1; j-- )
    			printf( "*" );
    	}
    
    	return 0;
    }
    Loading.....
    ( Trying to be a good C Programmer )

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Chances are your instructer would want you to look for a pattern and code it.

    Notice that the spaces before each * is the number of rows left to go. Also notice that the number of *s is 1,3,5,7...

    Code:
    #include <iostream.h>
    int main()
    {
    	int n; //rows
    	int j; //current row
    	int s; //spaces
    	int c; //counter (for *s)
    	cout<<"Enter rows: ";
    	cin>>n;
    	cout<<endl<<endl;
    	for(j=1;j<=n;j++)
    	{
    		for(s=0;s<n-j;s++)
    			cout<<' ';
    		for(c=0;c<(2*j-1);c++) // ^_^ C++
    			cout<<'*';
    		cout<<endl;
    	}
    	return 0;
    }
    and you don't have to use gotoxy(), especially since its not in Standard C++.

    Side Note: 24 rows will fill a standard console window (provided u use MSVC++ and it asks for a key to continue). Otherwise its 25 rows.
    The standard console size is 80x25. Since 25 is hieght, 25 rows will fill the window. Try it out.

    -LC
    Last edited by Lynux-Penguin; 07-24-2003 at 02:12 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    C language

    Thanks for your comments and suggestions...

    We're not yet on C++, only on pure C language,

    nonetheless, I got some more ideas and contexts from your suggestions..

    Thanks !!!!!

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    oops... hehe

    wrong board.

    Sorry...

    Code:
    #include <stdio.h>
    int main()
    {
    	int n; //rows
    	int j; //current row
    	int s; //spaces
    	int c; //counter (for *s)
    	printf("Enter rows: ");
    	scanf("%d",&n);
    	printf("\n\n");
    	for(j=1;j<=n;j++)
    	{
    		for(s=0;s<n-j ;s++)
    			printf(" ");
    		for(c=0;c<(2*j-1) ;c++) 
    			printf("*");
    		printf("\n");
    	}
    	return 0;
    }
    blue = rows to go
    red = every odd number
    Last edited by Lynux-Penguin; 07-24-2003 at 02:21 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char O00[] = "\n%%%dc";
    char OO0[BUFSIZ]={0};
    int  O0O,OOO='0'-'o'+'O';
    for(O0O=0;O0O<OOO;O0O++)
    {
        sprintf(OO0,O00,OOO-O0O);
        memset(OO0+strlen(OO0),'*',(O0O<<1)+1);
        printf(OO0,' ');
    }
    You could always just use one loop. Nice and simple.

    Edit: Replaced boring '10' with funner assignment.
    Edit2: On an amusing note, were I to wrap the above code entirely in [b][/b] tags, it would make it visually unreadable, as all O o and 0 would look the same, due to an apparent issue with combining code and bold tags.

    Quzah.
    Last edited by quzah; 07-24-2003 at 03:55 PM.
    Hope is the first step on the road to disappointment.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by vVv
    You foolishly assume BUFSIZ >= 24.

    > printf(OO0,' ')

    OO0 is never NUL-terminated after memset().
    Please know what you're talking about. You apparently missed the declaration of the array. The {0} assigns every value to zero. As such, it is null terminated, unless you happen to have overran your buffer. If you're really worried, a simple modification fixes your problem:
    Code:
    const int O00O = 1000;
    char OO0[O00O]={0};
    Furthermore, it's a fairly safe assumption that BUFSIZ is greater than 24. In MSVC++, it defaults to 512.
    Originally posted by vVv
    > Nice and simple.

    Anything but nice.
    Since you obviously have no sense of humor, I'll not bother explaining the joke.

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

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by vVv
    > Nice and simple.

    Anything but nice.
    You foolishly assume BUFSIZ >= 24.

    > printf(OO0,' ')

    OO0 is never NUL-terminated after memset().
    BUFSIZ will be >24, in fact:
    c89/99
    An implementation shall support text files with lines containing at
    least 254 characters, including the terminating new-line character.
    The value of the macro BUFSIZ shall be at least 256.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rectangle of asterisks
    By sugeknight in forum C++ Programming
    Replies: 1
    Last Post: 02-02-2009, 08:46 PM
  2. Write a program that prints a pattern of asterisks?
    By Basia in forum C Programming
    Replies: 2
    Last Post: 06-01-2002, 05:56 PM
  3. Password Asterisks
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2002, 04:58 PM
  4. How to echo text as asterisks
    By n00b in forum C++ Programming
    Replies: 9
    Last Post: 09-02-2001, 09:16 AM
  5. Replies: 1
    Last Post: 09-01-2001, 10:33 AM