create a 5x5 matrix using letters instead of numbers

alice21
I am not saying this to be rude or put you off, but you seem to be:
  • ignoring forum etiquette (spamming multiple forums and posts for the same problem)
  • asking others to do work for you (posting an assignment and the basic shell of a program
    given you by your professor)

Grown-up people aren’t typically interested in doing other people’s work for them.
People who frequent forums typically like to do so because they want to help others succeed. Not because they are too bored to figure out stuff they can do on their own time.*

Here is your other thread, to which you have already been linked back elsewhere:
http://www.cplusplus.com/forum/beginner/250031/

*Some forum posters are, however, and you may even convince them to do your work for you for some small exchange of money.
That's to bad. I had fun writing that program.

For something quick I wrote:
if (words[row][col] == 'a' || words[row][col] == 'e' || words[row][col] == 'i' || words[row][col] == 'o' || words[row][col] == 'u')
I have not worked on trying to shorten this yet, but I am open to any suggestions.

Andy
Write a function:

1
2
3
4
bool is_vowel( char c )
{
  return std::string( "AEIOUaeiou" ).find( c ) <> std::string::npos;
}

Now you can use it anywhere:

    if (is_vowel( words[row][col] ))

Hope this helps.
@Duthomhas,

Thank you. I knew there must be a better way.
One possibility:

1
2
char c = words[row][col];
if (c == 'a' || ...)

Another:

 
if (strchr("aeiou", words[row][col]))

More C++y:

1
2
string vowels("aeiou");
if (vowels.find(words[row][col]) != vowels.npos)

@dutch,

trying to stay away from C code, but I do like your last example. Both of you have shown me the ".find" that I was think of.

Since I was playing around it did not matter what I started with.

Thank you.

Andy
Of course. You rock!

C++ containers (including strings) are designed around these kinds of algorithms. You could do the same thing with, say, a std::vector.

1
2
3
4
5
6
7
  std::vector<int> primes{ 2, 3, 5, 7, 11, 13, 17 };

  int n = 12;
  if (std::find( std::begin(primes), std::end(primes), n ) != std::end(primes))
    std::cout << n << " is PRIME\n";
  else
    std::cout << n << " is NOT prime\n";

      std::cout << "(for primes <= 17)\n";

Enjoy!
Topic archived. No new replies allowed.