Hello

Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: need help with a c-program  (Read 574 times)

Offline Tyrant
  • Hero Member
  • *****
  • Posts: 1877
  • Karma: +10/-0
    • http://www.bahrainicars.com
need help with a c-program
« 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.
 :)
« Last Edit: April 04, 2003, 05:22:18 AM by Tyrant »
[size=1.5]It is a mistake to try to look too far ahead. The chain of destiny can only be grasped one link at a time.~Sir Winston Churchill[/size]
Bahrains ultimate vehicle showroom,  CV8=ownage, Bahrain F1, Bahraini cars, GulfGt.

Offline Avatarr
  • Wise Member

  • Hero Member
  • *****
  • Posts: 1647
  • Karma: +10/-0
    • http://www.sheepsheet.com
need help with a c-program
« Reply #1 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.

Offline Tyrant
  • Hero Member
  • *****
  • Posts: 1877
  • Karma: +10/-0
    • http://www.bahrainicars.com
need help with a c-program
« Reply #2 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.
[size=1.5]It is a mistake to try to look too far ahead. The chain of destiny can only be grasped one link at a time.~Sir Winston Churchill[/size]
Bahrains ultimate vehicle showroom,  CV8=ownage, Bahrain F1, Bahraini cars, GulfGt.

Offline Tyrant
  • Hero Member
  • *****
  • Posts: 1877
  • Karma: +10/-0
    • http://www.bahrainicars.com
need help with a c-program
« Reply #3 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 :).
« Last Edit: April 04, 2003, 09:06:12 AM by Tyrant »
[size=1.5]It is a mistake to try to look too far ahead. The chain of destiny can only be grasped one link at a time.~Sir Winston Churchill[/size]
Bahrains ultimate vehicle showroom,  CV8=ownage, Bahrain F1, Bahraini cars, GulfGt.

Offline EmperorRob
  • Mr Sexual Harassment
  • Legendary Member
  • ******
  • Posts: 3932
  • Karma: +10/-0
need help with a c-program
« Reply #4 on: April 04, 2003, 09:06:02 AM »
nm looks like you got it.
« Last Edit: April 04, 2003, 09:10:20 AM by EmperorRob »
This is America and I can still pay for sex with pennies

Offline Tyrant
  • Hero Member
  • *****
  • Posts: 1877
  • Karma: +10/-0
    • http://www.bahrainicars.com
need help with a c-program
« Reply #5 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. :)
[size=1.5]It is a mistake to try to look too far ahead. The chain of destiny can only be grasped one link at a time.~Sir Winston Churchill[/size]
Bahrains ultimate vehicle showroom,  CV8=ownage, Bahrain F1, Bahraini cars, GulfGt.

Offline EmperorRob
  • Mr Sexual Harassment
  • Legendary Member
  • ******
  • Posts: 3932
  • Karma: +10/-0
need help with a c-program
« Reply #6 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.
This is America and I can still pay for sex with pennies

Offline Avatarr
  • Wise Member

  • Hero Member
  • *****
  • Posts: 1647
  • Karma: +10/-0
    • http://www.sheepsheet.com
need help with a c-program
« Reply #7 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
« Last Edit: April 04, 2003, 05:35:51 PM by Avatarr »

Offline EmperorRob
  • Mr Sexual Harassment
  • Legendary Member
  • ******
  • Posts: 3932
  • Karma: +10/-0
need help with a c-program
« Reply #8 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 :)
This is America and I can still pay for sex with pennies

Offline Avatarr
  • Wise Member

  • Hero Member
  • *****
  • Posts: 1647
  • Karma: +10/-0
    • http://www.sheepsheet.com
need help with a c-program
« Reply #9 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. :)

 

SMF spam blocked by CleanTalk