Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   diamond asterics problem (http://www.programmingforums.org/showthread.php?t=14092)

blouro Oct 2nd, 2007 2:32 PM

diamond asterics problem
 
i was given 10 asterics problems to do. i've completed all of them except for this one. i've given up. i can only get half of the diagram to print

its supposed to print like this

************* row 1 0 spaces ,13*
******-****** row 2 1 spaces, 12*
*****-- -***** row 3 3 spaces, 10*
****----- **** row4 5 spaces, 8*
***------- *** row5 7 spaces ,6*
**--------- **row6 9 spaces, 4*
* ----------- *row7 11 spaces, 2*
**--------- **row8 9 spaces ,4*
***------- ***row9 7 spaces ,6*
****----- ****row10 5 spaces ,8*
*****--- *****row11 3 spaces ,10*
****** -******row12 1 spaces ,12*
*************row13 0 spaces ,13*



so far i have
:

for(int rows = -6; rows <=6; rows++)
{
        for(int st = 1+ abs(rows)+1; st>0; st--)
        {
                printf("*");

        }
        printf("\n");
}


but this only pritns half the *'s

if anyone can help me do the other half it will be greatly appreciated.

i can only use printf(" ") printf("*") printf("\n") once in the whole block of code.

thank you

Wizard1988 Oct 2nd, 2007 9:09 PM

Try to come up with a mathematical expression for when a star shoudl be printed or not using x and y variables (Row vs Column)

avinashmalipatil Oct 3rd, 2007 5:39 AM

Here is the code...
But remember never giveup, keep trying until u find the solution..
:

#include<stdio.h>

int main()
{
        int row, space=0,ast=13,count;

        for(row=0;row<13;row++)
        {
                for(count=1;count<=ast/2;count++)
                {
                        printf("*");
                }

                if(ast==13)
                {
                        printf("*");
                }
                               
                for(count=1;count<=space;count++)
                {
                        printf(" ");
                }
               
                for(count=1;count<=ast/2;count++)
                {
                        printf("*");
                }
               
                if(row<6)
                {
                        if(space==0)
                        {
                                space++;
                                ast--;
                        }
                        else
                        {
                                space+=2;
                                ast-=2;
                        }
                }
                else
                {
                        if(space==1)
                        {
                                space--;
                                ast++;
                        }
                        else
                        {
                                space-=2;
                                ast+=2;
                        }
                }
                        printf("\n");
                }
return 0;
}


The Dark Oct 3rd, 2007 10:01 AM

avinashmalipatil: count your printfs - look at the requirements.

avinashmalipatil Oct 4th, 2007 1:40 AM

Here i have modified the code. If there is any mistake, let me know.
:

#include<stdio.h>

int main()
{
        int row, space=-1,ast=7,col;

        for(row=1;row<=13;row++)
        {
                for(col=1;col<=13;col++)
                {
                        if(col>ast && col<=(ast+2*space+1) && ast!=7)
                        {
                                printf(" ");
                        }
                        else
                        {
                                printf("*");
                        }
                }
               
                        if(row<7)
                        {
                                ast--;
                                space++;
                        }
                        else
                        {
                                ast++;
                                space--;
                        }
               
                printf("\n");
        }
return 0;
}


Seif Oct 4th, 2007 7:52 PM

code tags ftw

DaWei Oct 4th, 2007 8:32 PM

This is not an entirely straightforward problem because of the similarities between the first and second rows and the penultimate and ultimate rows, and the requirement for a minimum of printf statements.

It will require a tad of thinking beforehand to resolve those impediments. I would recommend dividing the problem horizontally (left, center, and right portions), and then incorporate that into the vertical dimension. Use value transitions as a key to behavior modification.

avinashmalipatil Oct 5th, 2007 1:00 AM

Quote:

Originally Posted by Seif (Post 134748)
code tags ftw

Sorry, I didnt get you. wat do u mean by "code tags ftw"?

peaceofpi Oct 5th, 2007 5:17 PM

Quote:

Originally Posted by avinashmalipatil (Post 134765)
Sorry, I didnt get you. wat do u mean by "code tags ftw"?

He means try reading the rules and using code tags so your post isn't painful to look at.

avinashmalipatil Oct 8th, 2007 3:17 AM

The code is so small and i dont think code tags r required. U can easily understand the code, its so simple. And there is no reply from Blouro. Is ur ploblem solved?


All times are GMT -5. The time now is 3:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC