0

I'm trying to write a recursive program that makes/prints a string with each of all the possible combinations of vowels(A, E, I, O, U), that includes at least once every vowel. The number of elements is taken as input from the user (so to include each vowel at least once >=5). I really can't figure out the right way to do this.

Here's my take on it (not working though):

#include <stdio.h>
#include <stdlib.h>

int voc_gen(int n, char *array);
char rand_voc();

int main()
{
char *array, voc[] = "AEIOU";
int i, N;
printf("Insert number of elements for vocals string:\n");
scanf("%d", &N);

array = malloc(N * sizeof(char));

for(i=0; i<5; i++)
    array[i] = voc[i];

voc_gen(N-5, array);

return 0;
}

int voc_gen(int n, char *array){
if(n==0)
    return 0;
else{
    array[voc_gen(n, array)] = rand_voc();
    printf("%s\n", array);
    return voc_gen(n-1, array);
}
}

char rand_voc(){
char array[] = "AEIOU";
int i;

return array[rand()%5];
}
oz123
  • 27,559
  • 27
  • 125
  • 187
user3338768
  • 99
  • 1
  • 1
  • 6
  • What are you expecting and what output are you getting? – Rahul Feb 26 '14 at 09:04
  • 2
    Everything you write after a `return` statement is useless. You may try to switch lines in `voc_gen()`. Otherwise, `voc_gen()` will not print anything. – francis Feb 26 '14 at 09:20
  • 1
    Not exactly getting you. Also in `voc_gen` function you return before assignment and printf. So how you expect to execute it,it never executed. – Jayesh Bhoi Feb 26 '14 at 09:21
  • @Rahul, I'm expecting to write to string(printing is indispensable) the value of the combination of vowels at the time(i.e. AAEIOU, AEEIOU...) until all combinations are written. I'm getting an error atm. – user3338768 Feb 26 '14 at 09:24
  • @francis, You're right, I switched lines so i don't get an error anymore, still no output though. thx – user3338768 Feb 26 '14 at 09:26
  • 1
    @user3338768 i think you want some parmutation of string please check http://stackoverflow.com/questions/361/generate-list-of-all-possible-permutations-of-a-string and http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/ – Jayesh Bhoi Feb 26 '14 at 09:26
  • 1
    Also, shouldn't the condition `n==1` be `n==0` and return `0`? – M Oehm Feb 26 '14 at 09:28
  • @JKB, thx for both the answers, I edited the code as both you and francis suggested, now I don't get errors anymore, not output though, will check the links you've provided me and try to improve the code accordingly. – user3338768 Feb 26 '14 at 09:30
  • 1
    @user3338768 also try `printf("%s\n", array);return (array[voc_gen(n-1, array)] = rand_voc());` in only else part because you called multiple time `voc_gen` first from array index and second from return. – Jayesh Bhoi Feb 26 '14 at 09:34
  • @MOehm, You're right, considering I use the output as index for the resulting string. – user3338768 Feb 26 '14 at 09:37
  • @JKB, probably this is going to sound stupid but the function is declared as int so shouldn't I avoid returning char? array[voc_gen(n-1, array)] wouldn't return a char? – user3338768 Feb 26 '14 at 09:41
  • 1
    `voc_gen(int n, char *array)`calls `voc_gen(n,array)`. This is an infine recursion ! Isn't the name of this website stackoverflow ? http://en.wikipedia.org/wiki/Stack_overflow Moreover, using `rand()` to write ALL combinations is not the right way to go. Try to generate ALL combinations of strings and add a test to disqualify those which do not contain each vowel at least once. – francis Feb 26 '14 at 09:45
  • @user3338768 return `int` why? any specific reason? because you state `recursive program that makes/prints a string with each of all the possible combinations of vowels(A, E, I, O, U),` so you need only print right? – Jayesh Bhoi Feb 26 '14 at 09:49
  • @francis, I'm dumb, you're right. i completely missed that recursive call. I'm going to try to change array[voc_gen(n, array)] = rand_voc() to array[n] = rand_voc(), that should help. I do realize that probably most of the code is wrong/unprecise anyway so I'll try writing a newer version and edit the whole posted code then. – user3338768 Feb 26 '14 at 09:56
  • Thx @JKB for pointing out both the second recursive call I missed and the futility of the function as int, I just didn't get it right away. I will post a new version of the code since most of it seems to be wrong/not precise anyway. – user3338768 Feb 26 '14 at 09:58
  • For future reference it's a good idea to be more specific about "not working" in the original question. What are your error messages? Does it run but with different output than you expected? If so give expected and actual output. That sort of thing. – starsplusplus Feb 26 '14 at 09:59
  • @starsplusplus, sorry, not very accustomed to this, I got "Segmentation Fault: COre dumped" since there's an infinite recursion. It does run without any syntax errors but because of the infinite recursion, not the way I hoped. I get no output and the one I expected was somethign along the lines of "AAEIOU\n AEEIOU\n AIEOU...". Thx 4 the heads-up. – user3338768 Feb 26 '14 at 10:04
  • No worries, it looks like people have been helping out. Just giving you some tips for next time. – starsplusplus Feb 26 '14 at 10:09

0 Answers0