Need help to use loop

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 3
Reputation: asharrajpoot is an unknown quantity at this point 
Solved Threads: 0
asharrajpoot asharrajpoot is offline Offline
Newbie Poster

Need help to use loop

 
0
  #1
Jan 7th, 2009
i want to show output like this
  1. A B C D E F G F E D C B A
  2. A B C D E F F E D C B A
  3. A B C D E E D C B A
  4. A B C D D C B A
  5. A B C C B A
  6. A B B A
  7. A A

i write a program to out put this but it contaions some error
please help me to solve this problem
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main(void)
  4. {
  5. int row=0,col=0,sp=0,a=65,b,mid=1,st=71,end=64;
  6. clrscr();
  7. for (row=1;row<=7;row++)
  8. {
  9. for(col=65;col<=st;col++)
  10. {
  11. printf("%c ",col);
  12. }
  13. for(b=1;b<mid;b++)
  14. {
  15. printf(" ");
  16. }
  17. for(sp=71;sp>end;sp--)
  18. {
  19. printf("%c ",sp);
  20. }
  21.  
  22. st--;
  23. mid+=2;
  24. a++;
  25. end++;
  26. printf("\n\n");
  27. }
  28. getche();
  29. }

it output is
  1. A B C D E F G G F E D C B A
  2. A B C D E F G F E D C B
  3. A B C D E G F E D C
  4. A B C D G F E D
  5. A B C G F E
  6. A B G F
  7. A G
Last edited by Ancient Dragon; Jan 7th, 2009 at 1:58 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: asharrajpoot is an unknown quantity at this point 
Solved Threads: 0
asharrajpoot asharrajpoot is offline Offline
Newbie Poster

Re: Need help to use loop

 
0
  #2
Jan 7th, 2009
  1. A B C D E F G G F E D C B A
  2. A B C D E F G F E D C B
  3. A B C D E G F E D C
  4. A B C D G F E D
  5. A B C G F E
  6. A B G F
  7. A G
why it donot show spaces between this output
Last edited by Ancient Dragon; Jan 7th, 2009 at 1:59 pm. Reason: add code tags to preserve spacing
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: asharrajpoot is an unknown quantity at this point 
Solved Threads: 0
asharrajpoot asharrajpoot is offline Offline
Newbie Poster

Re: Need help to use loop

 
0
  #3
Jan 7th, 2009
A B C D E F G F E D C B A
A B C D E F __F E D C B A
A B C D E _____E D C B A
A B C D________ D C B A
A B C____________C B A
A B_______________ B A
A __________________A
where (_) is space
i want to print this please help me i am new one
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need help to use loop

 
0
  #4
Jan 7th, 2009
Originally Posted by asharrajpoot View Post
  1. A B C D E F G G F E D C B A
  2. A B C D E F G F E D C B
  3. A B C D E G F E D C
  4. A B C D G F E D
  5. A B C G F E
  6. A B G F
  7. A G
why it donot show spaces between this output
The problem is in vBulletin -- added code tags to correct the problem.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 639
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 103
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Need help to use loop

 
0
  #5
Jan 7th, 2009
From my first glance at your problem, it would appear that the first line is a 'special case' as it does not have spaces at all (which would work as the zero spaces case) but the 'left side' and the 'right side' are not mirror images of each other. Someone has to get the G printed.

What about breaking it down into left / G / right for the first line and then left / spaces / right for the remaining lines.

The reason all of your right sections start with G is because you told them to here: for(sp=71;sp>end;sp--) maybe the 71 should be 71 - row or something?

The reason the right sections don't all go down to A (like you say they should) is because you're incrementing end inside the loop on line 25. If the right sections should always go to A don't change end.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 114
Reputation: zhelih has a little shameless behaviour in the past 
Solved Threads: 11
zhelih's Avatar
zhelih zhelih is offline Offline
Junior Poster

Re: Need help to use loop

 
0
  #6
Jan 7th, 2009
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. int main(void)
  5. {
  6. int row=0,col=0,sp=0,b,mid=1,st=71,end=64;
  7. //clrscr();
  8. int column_num=0;
  9. for (row=1;row<=7;row++)
  10. {
  11. for(col=65;col<=st;col++)
  12. {
  13. printf("%c ",col);
  14. }
  15. for(b=1;b<mid;b++)
  16. {
  17. printf(" ");
  18. }
  19. for(sp=70;sp>end;sp--)
  20. {
  21. printf("%c ",sp-column_num);//start with less symbol
  22.  
  23. }
  24.  
  25. st--;
  26. mid+=2;
  27.  
  28. column_num++;
  29. end++;
  30. printf("\n\n");
  31. }
  32. getche();
  33. return 1;
  34. }
Your code is absolutely unreadable but I managed to noticed smth. First you don't start to wrtite another row with another symbol. My variable column_num does this. Previous message said my second point. Ask question if you want.
Last edited by zhelih; Jan 7th, 2009 at 4:59 pm. Reason: sorry, small syntax mistake.
An Apple a Day keeps a Doctor away!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 639
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 103
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Need help to use loop

 
0
  #7
Jan 7th, 2009
@zhelih

Your english is barely readable, and the above post was your first post in this thread, what was your second point?

@asharrajpoot

I had an idea for another way to code the output. What if you built a string that contained the proper first line. (I'll build it manually, but you should probably use a loop.)

  1. char masterline[80] = "A B C D E F G F E D C B A";

Then to print the table as you wanted:

Then you could walk through the row, outputting characters. If the character in the string is less than a limit (we will cover the limit ina second) you output the character, otherwise you output a space.

For the 1st row, print all characters <= 'G' (which would be all of them)
For the 2nd row, print all characters <='F' (all but the G)
For the 3rd row, print all characters <= 'E'
...
For the last row, print all characters <= 'A'

Would that be any easier?

The proposed algorithm would probably consume more CPU than what you're doing now, but I also suspect that both will run so fast you won't notice.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 25
Reputation: nitu_thakkar has a little shameless behaviour in the past 
Solved Threads: 1
nitu_thakkar's Avatar
nitu_thakkar nitu_thakkar is offline Offline
Light Poster

Re: Need help to use loop

 
0
  #8
Jan 8th, 2009
i have do some modifiaction so try this....
  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4. void main(void)
  5. {
  6. int row=0,col=0,sp=0,a=65,b,mid=1,st=71,end=64;
  7. clrscr();
  8.  
  9. for (row=1;row<=7;row++)
  10. {
  11. for(col=65;col<=st;col++)
  12. {
  13. printf("%c ",col);
  14.  
  15. }
  16.  
  17. for(b=1;b<mid;b++)
  18. {
  19. printf(" ");
  20.  
  21. }
  22.  
  23. for(sp=col-1;sp>end;sp--)
  24.  
  25. {
  26.  
  27. printf("%c ",sp);
  28.  
  29. }
  30.  
  31. st--;
  32. mid+=2;
  33. a++;
  34. /*end++;*/
  35.  
  36. printf("\n\n");
  37. }
  38.  
  39. getche();
  40.  
  41. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Need help to use loop

 
0
  #9
Jan 8th, 2009
  1. void Pyr(int N)
  2. {
  3. if (N >= 0 && N < 26) {
  4. const char* z2a = "ZYXWVUTSRQPONMLKJIHGFEDCBA" + (26 - N);
  5. int i, j, d, dist, mid = N+N - 2, n = 4*N - 1;
  6. for (i = 0; i < N; ++i) {
  7. dist = i + i - 1;
  8. for (j = 0; j < n; ++j) {
  9. d = abs(j-mid);
  10. putchar((j&1)||d < dist
  11. ?' ':z2a[d>>1]);
  12. }
  13. putchar('\n');
  14. }
  15. }
  16. }
  17. int main()
  18. {
  19. Pyr(7);
  20. return 0;
  21. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 44
Reputation: me_ansh is an unknown quantity at this point 
Solved Threads: 5
me_ansh me_ansh is offline Offline
Light Poster

Re: Need help to use loop

 
0
  #10
Jan 21st, 2009
I have written some rough code, please modify according to your requirement:

1) For pattern
A B C D E F G F E D C B A
A B C D E F __F E D C B A
A B C D E _____E D C B A
A B C D________ D C B A
A B C____________C B A
A B_______________ B A
A __________________A

  1. int main(void)
  2. {
  3. char ch;
  4. int i=0,j=0,k=0,l=0,h=0,m=0;
  5.  
  6. printf("\n Enter a CAPS charachter: ");
  7. scanf("%c",&ch);
  8. for(k=0;k<=ch-65;k++)
  9. {
  10. for(i=65;i<=ch-k;i++)
  11. printf("%c ",(char)i);
  12. if(k>0)
  13. {
  14. h = 4*k;
  15. for(l=0;l<h-2;l++)
  16. printf(" ");
  17. }
  18. if(k>1)
  19. m = k-1;
  20. for(j=ch-1-m;j>=65;j--)
  21. printf("%c ",(char)j);
  22. printf("\n");
  23. }
  24. return 0;
  25. }

2. For pattern:
A B C D E F G F E D C B A
A B C D E F___F E D C B A
A B C D E_______E D C B A
A B C D___________D C B A
A B C_______________C B A
A B___________________B A
A_______________________A

  1. int main(void)
  2. {
  3. char ch;
  4. int i=0,j=0,k=0,l=0,h=0,m=0;
  5.  
  6. printf("\n Enter a CAPS charachter: ");
  7. scanf("%c",&ch);
  8. for(k=0;k<=ch-65;k++)
  9. {
  10. for(i=65;i<=ch-k;i++)
  11. printf("%c ",(char)i);
  12. if(k>0)
  13. {
  14. h = 5*k;
  15. for(l=0;l<h-1;l++)
  16. printf(" ");
  17. }
  18. if(k>1)
  19. m = k-1;
  20. for(j=ch-1-m;j>=65;j--)
  21. printf("%c ",(char)j);
  22. printf("\n");
  23. }
  24. return 0;
  25. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 754 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC