Tuesday, 10 September 2013

Find all possible interpretations of an array of digits

Find all possible interpretations of an array of digits

Consider a coding system for alphabets to integers where 'a' is
represented as 1, 'b' as 2, .. 'z' as 26. Given an array of digits (1 to
9) as input, write a function that prints all valid interpretations of
input array.
/*Examples
Input: {1, 1}
Output: ("aa", 'k")
[2 interpretations: aa(1, 1), k(11)]
Input: {1, 2, 1}
Output: ("aba", "au", "la")
[3 interpretations: aba(1,2,1), au(1,21), la(12,1)]
Input: {9, 1, 8}
Output: {"iah", "ir"}
[2 interpretations: iah(9,1,8), ir(9,18)]*/
My c code for this
#include<iostream>
using namespace std;
#include<string.h>
int a[10]={2,3,4,4,2,4,2,8,9};
char
c[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
void func(int i,char result[10])
{
if(i==10)
{
for(int j=0;j<10;j++)
cout<<result[j];
}
else
{
if(10*a[i]+a[i+1]<26)
{
strcat(result,"c[10*a[i]+a[i+1]]");
func(i+2,result);
}
strcat(result,"c[a[i]]");
func(i+1,result);
}
}
int main()
{
func(0,"");
}
I am not able to find out the errors. can you help me out??

No comments:

Post a Comment