![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2007
Posts: 1
Rep Power: 0
![]() |
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 Last edited by big_k105; Oct 2nd, 2007 at 1:54 PM. Reason: Added Code Tags |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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)
__________________
JG-Webdesign |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2007
Posts: 14
Rep Power: 0
![]() |
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;
}Last edited by big_k105; Oct 5th, 2007 at 4:54 PM. Reason: Added Code Tags |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
avinashmalipatil: count your printfs - look at the requirements.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Sep 2007
Posts: 14
Rep Power: 0
![]() |
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;
}Last edited by big_k105; Oct 5th, 2007 at 4:54 PM. Reason: Added Code Tags |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
code tags ftw
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Sep 2007
Posts: 14
Rep Power: 0
![]() |
Sorry, I didnt get you. wat do u mean by "code tags ftw"?
__________________
Avinash INDIA |
|
|
|
|
|
#9 |
|
hi: for(;;) goto hi;
|
He means try reading the rules and using code tags so your post isn't painful to look at.
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Sep 2007
Posts: 14
Rep Power: 0
![]() |
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?
__________________
Avinash INDIA |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| <td> height problem | grimpirate | HTML / XHTML / CSS | 19 | May 4th, 2007 6:01 AM |
| Stuck with a C problem | Polaris | C++ | 8 | Aug 19th, 2006 3:30 PM |
| cgi/perl script + IE problem | joyceshee | Perl | 2 | Jan 24th, 2006 11:10 AM |
| Variable array problem | Hintshigen | C | 6 | Apr 10th, 2005 2:35 PM |
| string problem when passing in linked list | quantz | C++ | 0 | Feb 27th, 2005 10:11 AM |