Thread: arrays-idea to solve program

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    17

    Question arrays-idea to solve program

    hi,
    i have this program which read 20 char and count the number of small vowel letters then change them to capital

    so i start 2 arrays so i have this idea to solve it
    it is right or wrong please help me to understand the wrong part of it
    Code:
    int i,v=0;
    char a[20]
    char v[5]={'e','a','u','i','o'}
    
    for(i=0;i<20;i++)
    cin>>a[i];
    
    for(i=o;i<20;i++)
    if(a[i]==v[5])
    {
    v++;
    a[i]=a[i]-'A'+'a';
    }
    
    for(i=0;i<20;i++)
    cout<<a[i];
    cout<<"number of small vowel is ="<<v;
    Last edited by CinQabas; 04-08-2016 at 02:28 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    What's up with the gigantic font? Are you trying to be annoying?

    On line 9, I'm guessing that you're trying to compare a character to the whole array of vowels. C++ doesn't work that way. You'll need to loop through the vowel array, just like you're looping through the input string. Even better yet, break that out into a function called IsVowel or something.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    17
    sorry a bout the font size i didn't mean it

    but why i need vowels loop when the vowels loop are known just in 5 numbers of all

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    17
    can i say this in line 9
    Code:
    if(a[i]=='a'||'e'||'u'||'i'||'o')

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by CinQabas View Post
    can i say this in line 9
    Code:
    if(a[i]=='a'||'e'||'u'||'i'||'o')
    No, you need to spell out each comparison separately, like this:
    Code:
    if (foo == 'b' || foo == 'a' || foo == 'r')
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    char v[5]={'e','a','u','i','o'}
    
    // ...
    
    if(a[i]==v[5])
    Apart from the comments in the above posts, you're accessing the array out of bounds here, since with a size of 5, the valid indices are 0 - 4.

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    17
    Quote Originally Posted by GReaper View Post
    No, you need to spell out each comparison separately, like this:
    Code:
    if (foo == 'b' || foo == 'a' || foo == 'r')
    yes i get you

  8. #8
    Registered User
    Join Date
    Mar 2016
    Posts
    17
    Quote Originally Posted by Matticus View Post
    Code:
    char v[5]={'e','a','u','i','o'}
    
    // ...
    
    if(a[i]==v[5])
    Apart from the comments in the above posts, you're accessing the array out of bounds here, since with a size of 5, the valid indices are 0 - 4.


    i didn't get you
    do you mean that my vowel array need to be same size the array of a[20] so he can compere between them ?


    or my work is right ?

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    No, what they meant was that you don't use the "v" array properly here. You access one element past the array, and out-of-boundary accesses invoke undefined behavior.

    What you're trying to do is to compare each character in the string with all those lowercase vowels, and replace them with uppercase ones. Now, there are multiple ways you can do this, here are some( but there are more ):
    Solution #1:
    Code:
    bool isLowerVowel = false;
    for (int j = 0; j < 5; j++) {
        if (a[i] == v[j]) {
            isLowerVowel = true;
            break;
        }
    }
    if (isLowerVowel)
    Solution #2:
    Code:
    if (a[i] == 'a' || a[i] == 'e' || a[i] == 'u' || a[i] == 'i' || a[i] == 'o')
    Devoted my life to programming...

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    One way could be
    Code:
    if ( strchr("aeiou",c) != NULL ) {
      // c is a vowel
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-24-2012, 10:05 AM
  2. Replies: 9
    Last Post: 11-29-2011, 03:09 AM
  3. plz help me to solve this c++ program
    By umar123 in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2011, 06:00 AM
  4. Need help to solve this program
    By kitymarine in forum C Programming
    Replies: 2
    Last Post: 06-20-2011, 11:10 PM
  5. can u solve this program :P
    By bawen in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2006, 12:16 PM