PSX5Central

Non Gaming Discussions => Off-Topic => Topic started by: Tyrant on April 04, 2003, 05:07:04 AM

Title: need help with a c-program
Post by: Tyrant on April 04, 2003, 05:07:04 AM
well this is not something that i would normally ask for but i\'m quite desperate. i\'ve been studying for my midterm exams and so i decided to solve some of the problems in the text book i came across this nice little question which asked to print the following patterns using the "for loop" and "printf("*")"
a.
*
**
***
****
*****
******
*******
********
*********
**********
b. is the mirror image of a
_________*
________**
_______***
______****
_____*****
____******
___*******
__********
_*********
********** (without the ________)
now the first was a peice of cake

#include
int main ()
{
int i;
int size=10, count=9;
for(count=9;count>=0;count--)
{
for (i=1;i<=size-count;i++)
{
printf("*");
}
printf("\\n");
}
return 0;
}

now i\'ve been trying to solve the second for over an hour and still havent come up with a viable solution.
so if anyone can help me, please do.
 :)
Title: need help with a c-program
Post by: Avatarr on April 04, 2003, 07:07:13 AM
Ugh that\'s very ugly code. Always remember to indent even if you\'re just posting in a forum. Personally, I\'d just use an inverse of the first algorithm but with single spaces instead of *\'s. And at each step of that loop, I\'d add the *\'s using the original solution.
Title: need help with a c-program
Post by: Tyrant on April 04, 2003, 07:50:03 AM
avvy what do ya mean by indent, i\'m relatively new to c programming so forgive my ignorrance.
Title: need help with a c-program
Post by: Tyrant on April 04, 2003, 09:04:01 AM
w00t0rz i r0x
i finally got it here it is (its not the most perfect code but it\'ll do)

#include
int main ()
{
int i=1,size=10;
for (size=10;size>=1;size--)
{
for (i=1;i<=10;i++)
{
if (iprintf(" ");
else
printf("*");}
printf("\\n");
}
return 0;
}

btw thanks for the help Avvy :).
Title: need help with a c-program
Post by: EmperorRob on April 04, 2003, 09:06:02 AM
nm looks like you got it.
Title: need help with a c-program
Post by: Tyrant on April 04, 2003, 09:22:04 AM
emperor, yer code looked quite interesting, but too bad i\'m not allowed to use any type of code which is not covered by my course.
anyways i apreciate yer help, thanks alot guys. :)
Title: need help with a c-program
Post by: EmperorRob on April 04, 2003, 02:27:02 PM
If you can get your hands on a C reference book check out the options for printf.  What I was saying was

printf("%10s", "word");
   is the same as
printf("%*s", 10, "word");

the * lets you specify bytesize on the fly.  Very useful for formatting.  It doesn\'t help so much in this example though.  C is horrible for strings.  C++ is a little better but perl is #1 choice.
Title: need help with a c-program
Post by: Avatarr on April 04, 2003, 05:33:27 PM
hahaha, nevermind.. emmy put some hack that kills all the spaces.
:p

u know how you start writing things on a different vertical line, usually towards the right? thats indentation. :p
Title: need help with a c-program
Post by: EmperorRob on April 05, 2003, 02:06:04 AM
My goal is to do as much as possible with as few lines as possible.  Laziness is a virtue :)
Title: need help with a c-program
Post by: Avatarr on April 05, 2003, 03:17:41 AM
its actually easier to do more lines than less. ur not being lazy thar, ur being clever. :)