943,686 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 944
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 7th, 2009
0

Need help to use loop

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asharrajpoot is offline Offline
3 posts
since Jan 2009
Jan 7th, 2009
0

Re: Need help to use loop

  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asharrajpoot is offline Offline
3 posts
since Jan 2009
Jan 7th, 2009
0

Re: Need help to use loop

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asharrajpoot is offline Offline
3 posts
since Jan 2009
Jan 7th, 2009
0

Re: Need help to use loop

  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jan 7th, 2009
0

Re: Need help to use loop

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Jan 7th, 2009
0

Re: Need help to use loop

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 9
Solved Threads: 11
Junior Poster
zhelih is offline Offline
114 posts
since Sep 2007
Jan 7th, 2009
0

Re: Need help to use loop

@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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Jan 8th, 2009
0

Re: Need help to use loop

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. }
Reputation Points: 0
Solved Threads: 1
Light Poster
nitu_thakkar is offline Offline
25 posts
since Jan 2009
Jan 8th, 2009
0

Re: Need help to use loop

  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. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jan 21st, 2009
0

Re: Need help to use loop

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. }
Reputation Points: 13
Solved Threads: 5
Light Poster
me_ansh is offline Offline
44 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: trying to get text from file in function with malloc
Next Thread in C Forum Timeline: Is array name equivalent to a pointer??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC