| | |
Need help to use loop
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
i want to show output like this
i write a program to out put this but it contaions some error
please help me to solve this problem
it output is
C Syntax (Toggle Plain Text)
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
i write a program to out put this but it contaions some error
please help me to solve this problem
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> void main(void) { int row=0,col=0,sp=0,a=65,b,mid=1,st=71,end=64; clrscr(); for (row=1;row<=7;row++) { for(col=65;col<=st;col++) { printf("%c ",col); } for(b=1;b<mid;b++) { printf(" "); } for(sp=71;sp>end;sp--) { printf("%c ",sp); } st--; mid+=2; a++; end++; printf("\n\n"); } getche(); }
it output is
C Syntax (Toggle Plain Text)
A B C D E F G G F E D C B A A B C D E F G F E D C B A B C D E G F E D C A B C D G F E D A B C G F E A B G F A G
Last edited by Ancient Dragon; Jan 7th, 2009 at 1:58 pm. Reason: add code tags
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
A B C D E F G G F E D C B A A B C D E F G F E D C B A B C D E G F E D C A B C D G F E D A B C G F E A B G F A G
Last edited by Ancient Dragon; Jan 7th, 2009 at 1:59 pm. Reason: add code tags to preserve spacing
•
•
•
•
why it donot show spaces between this outputC Syntax (Toggle Plain Text)
A B C D E F G G F E D C B A A B C D E F G F E D C B A B C D E G F E D C A B C D G F E D A B C G F E A B G F A G
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.
•
•
Join Date: May 2008
Posts: 639
Reputation:
Solved Threads: 103
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:
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.
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.
C++ Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> int main(void) { int row=0,col=0,sp=0,b,mid=1,st=71,end=64; //clrscr(); int column_num=0; for (row=1;row<=7;row++) { for(col=65;col<=st;col++) { printf("%c ",col); } for(b=1;b<mid;b++) { printf(" "); } for(sp=70;sp>end;sp--) { printf("%c ",sp-column_num);//start with less symbol } st--; mid+=2; column_num++; end++; printf("\n\n"); } getche(); return 1; }
Last edited by zhelih; Jan 7th, 2009 at 4:59 pm. Reason: sorry, small syntax mistake.
An Apple a Day keeps a Doctor away!
•
•
Join Date: May 2008
Posts: 639
Reputation:
Solved Threads: 103
@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.)
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.
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.)
c Syntax (Toggle Plain Text)
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.
i have do some modifiaction so try this....
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> void main(void) { int row=0,col=0,sp=0,a=65,b,mid=1,st=71,end=64; clrscr(); for (row=1;row<=7;row++) { for(col=65;col<=st;col++) { printf("%c ",col); } for(b=1;b<mid;b++) { printf(" "); } for(sp=col-1;sp>end;sp--) { printf("%c ",sp); } st--; mid+=2; a++; /*end++;*/ printf("\n\n"); } getche(); }
c Syntax (Toggle Plain Text)
void Pyr(int N) { if (N >= 0 && N < 26) { const char* z2a = "ZYXWVUTSRQPONMLKJIHGFEDCBA" + (26 - N); int i, j, d, dist, mid = N+N - 2, n = 4*N - 1; for (i = 0; i < N; ++i) { dist = i + i - 1; for (j = 0; j < n; ++j) { d = abs(j-mid); putchar((j&1)||d < dist ?' ':z2a[d>>1]); } putchar('\n'); } } } int main() { Pyr(7); return 0; }
•
•
Join Date: Jan 2009
Posts: 44
Reputation:
Solved Threads: 5
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
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) 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
C Syntax (Toggle Plain Text)
int main(void) { char ch; int i=0,j=0,k=0,l=0,h=0,m=0; printf("\n Enter a CAPS charachter: "); scanf("%c",&ch); for(k=0;k<=ch-65;k++) { for(i=65;i<=ch-k;i++) printf("%c ",(char)i); if(k>0) { h = 4*k; for(l=0;l<h-2;l++) printf(" "); } if(k>1) m = k-1; for(j=ch-1-m;j>=65;j--) printf("%c ",(char)j); printf("\n"); } return 0; }
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
C Syntax (Toggle Plain Text)
int main(void) { char ch; int i=0,j=0,k=0,l=0,h=0,m=0; printf("\n Enter a CAPS charachter: "); scanf("%c",&ch); for(k=0;k<=ch-65;k++) { for(i=65;i<=ch-k;i++) printf("%c ",(char)i); if(k>0) { h = 5*k; for(l=0;l<h-1;l++) printf(" "); } if(k>1) m = k-1; for(j=ch-1-m;j>=65;j--) printf("%c ",(char)j); printf("\n"); } return 0; }
![]() |
Similar Threads
- Help with gui loop. (C)
- Loop...without the loop (Java)
Other Threads in the C Forum
- Previous Thread: trying to get text from file in function with malloc
- Next Thread: Is array name equivalent to a pointer??
Views: 754 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab win32 windows.h






