output of a pattern of letters in C programming

2008-12-01 3:52 am
void letters(char c)
// c is one of the characters 'A' through 'Z'.
// The function needs to print a pattern of letters as follows:
// 1. If the parameter c is 'A', then the output is 'A'.
// 2. For other values of c, the output consists of three parts:
// -- the output for the previous letter (c-1);
// -- followed by the letter c itself;
// -- followed by a second copy of the output for the previous letter (c-1).
// There is no '\n' printed at the end of the output.
/* Example output:
letters('D') will print:
ABACABADABACABA
*/
The program that I have written is
#include<stdio.h>
#include<stdlib.h>
char pat(char);

int main()
{
char n,a;
printf("Please enter a capital English character A-Z:");
scanf("%c",&n);

if(n>=65 && n<=90)
{
a=pat(n);
printf("%c%c%c",a,n,a);
}
else
printf("Invalid selection");

system("pause");
return 0;
}

char pat(char n)
{
if (n==65)
return n;
else
{
return pat(n-1) ;
}
}

But the output is wrong. How to fix the program?

回答 (3)

2008-12-01 8:27 pm
✔ 最佳答案
The question requires the output to be
-- the output for the previous letter (c-1);
-- followed by the letter c itself;
-- followed by a second copy of the output for the previous letter (c-1).

I agree the question itself is quite ambiguous. However, it shows the example that everybody can understand:
The output for letter ‘D’ is
-- the output for letter ‘C’
-- followed by the letter ‘D’
-- followed by the output for letter ‘C’

In other words, the answer should have a recursive function.
Pseudo code:
void F(char c){
If (c == ‘A’){
Output ‘A’;
return;
}
F(c - 1);
Output ‘c’;
F(c - 1);
}

Of course, you have to convert pseudo code to real c code.

2008-12-03 11:40:08 補充:
I guess it is a homework question. Using string in standard C program is an advanced topic, which would involve using malloc() function to dynamically allocate memory space for any dynamic string.

2008-12-03 11:40:57 補充:
I believe the problem is to test student whether they understand recursive function or not, and using string is not required.

2008-12-03 11:41:24 補充:
My previous solution have some problem, here is the correct pseudo code:

void letters(char c){
If (c == ‘A’){
Output ‘A’;
return;
}
letters(c - 1);
Output c;
letters(c - 1);
}

You have to change it to real C code, and add some constraint checking if you like.

2008-12-03 11:41:42 補充:
P.S. If you do not understand how my solution works, you could never be a good programmer.
2008-12-02 11:45 pm
Requirement :
----------------------
If the input is A, the output is 'A'
If the input is B, the output is'ABA'
If the input is C, the output is ABACABA
If the input is D, the output is ABACABADABACABA

Analysis:
-----------------------
Output is f(x-1) x f(x-1) where x is an input parameter in CHAR type

Note:
1) f() should return a STRING instead of CHAR !!
2) f() should be an recursive function obviously.


Verify:
------------------
f(A)
= A

f(B)
= f(B-1) + B + f(B-1)
= f(A) + B + f(A)
= ABA

f(C)
= f(C-1) + C + f(C-1)
= f(B) + C + f(B)
= ABA + C + ABA
= ABACABA

f(D)
= f(D-1) + D + f(D-1)
= f(C) + D + f(C)
= ABACABA + D + ABACABA
= ABACABADABACABA
............
...................

Recomended Code: (if threr is syntax error, pls correct yourself ^^")
-----------------------
int main()
{
char n,a;
printf("Please enter a capital English character A-Z:");
scanf("%c",&n);

if(n>=65 && n<=90)
{
printf("%s",pat(n));
}
else
printf("Invalid selection");

system("pause");
return 0;
}

string pat(char n)
{

string temp; // internal string variable to store immediate result.

if (n==65)
return n;
else
{

temp = pat(n-1);
return temp n temp ; // form the pattern f(x-1) x f(x-1)
}
}

Comment:
---------------
1) the pattern should be formed inside recursive function NOT inside main()
2) the recursive function should return STRING instead of CHAR



Hope this help

2008-12-02 15:54:15 補充:
if (n==65)
return n;
else
{

temp = pat(n-1);
return temp+n+temp ; // form the pattern f(x-1) + x + f(x-1)
}
參考: me
2008-12-01 4:36 am
The program posted correctly follows the instruction, the output for the letter 'D' is simply
CDC
and it is correct according to the instructions.

Could you please explain step by step how to get
ABACABADABACABA
from the single letter 'D'?


收錄日期: 2021-04-13 16:17:11
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20081130000051KK01825

檢視 Wayback Machine 備份