Cheeko
Cheeko

Reputation: 31

Logic / Algorithm for this Series

I have an array of characters from a to z, my task is to extract the vowels out of this array, the series that will extract the indexes of vowels goes like this, 1, 5, 9, 15, 21, 25... i am unable to create this series through loops, these numbers are actually the vowels in the array.

I don't want Vowel Matching technique, i want the logic for the sequence given above.

Upvotes: 2

Views: 333

Answers (3)

James Kanze
James Kanze

Reputation: 153977

You can doubtlessly find a formula for the sequence, but is that the right approach? Whether a character is a vowel or not isn't determined by whether it is a member of a sequence or not; it is purely arbitrary, and not necessarily the same from one language to another: y is a vowel in some languages, a consonant in others, and can be either in English; 27, 28 and 29 don't exist in English, but are vowels in Swedish, etc.

Upvotes: 0

K-ballo
K-ballo

Reputation: 81379

I would consider the solution proposed by Greg Hewgill to be the more appropiate one, where you have int vowels[] = {1, 5, 9, 15, 21, 25}; and vowels[i] is your function. However, if you want a math function Wolfram Alpha proposes:

(z(z((9-5z)z-11)+5))/((z-1)^4)+1

http://www.wolframalpha.com/input/?i=1%2C+5%2C+9%2C+15%2C+21%2C+25

That said, I don't see a point in using it instead of the array.

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 993971

If you really want to do this algorithmically, you could:

int v = 1;
for (int i = 0; i < 6; i++) {
    printf("%d\n", v);
    v += 4 + (i & 2);
}

...but I'm not sure I see the point.

Upvotes: 3

Related Questions