T4000
T4000

Reputation: 231

Boolean function does not work properly

I have trouble to come up with a Boolean function as expected by the assignment. I just need some explanations as I want to write my own code. I am supposed to write a MIPS code that actually counts the number of Uppercase, Lowercase, Vowels, Consonants and Digits in a string. I am writing the code in C++, then I will translate into MIPS assembly. I pasted the requirement below followed by the way the function bool consonant(char c) should look like (idea given by my professor). The problem is that it seems I am missing some information to make that function work. Can anyone provide me more information regarding that function? I don't need a code, just the missing details. Your help will be very appreciated.

//requirement of assignment bellow

To determine if an ASCII character c is a vowel or a consonant, write two functions bool vowel(char c) and bool consonant(char c). Use the stack to pass the character argument to these functions. Avoid long conditional expressions when testing a character for being a vowel and a consonant. Instead, use two global arrays (tables) containing Boolean flags to implement vowel() and consonant(). For example, an array named is_vowel would have true for characters ’a’ and ’A’ but false for ’b’ and ’B’.

// function that returns 0 if c is a consonant, or 1 if c is not a consonant
bool consonant(char c)
{
const bool is_conson[30]={0,0,...1,1,1,0,0...};

return is_conson[c];

}

//Here is The Code (C++) that I wrote for testing purpose only using Dev-C++

#include <iostream>
#include <math.h>
#include <cstdlib>

using namespace std;

bool consonant(char c)
{
const bool is_conso[30]= {1,1,1,1,0,0,0,0,0,1,0,1,1,0,0,0,1,0,1,0,1,1,1,1,0,1,0,1,0,1};

return is_conso[c];

}


int main()
{
int i;
 bool result;
char c;
char sentence[]="aaaabbbbb";

cout<<"the array: ";
cout<<sentence<<endl;
for (i=0; i<9; i++)
{
    c=sentence[i];

    result=consonant(c);
    if (result==0)

        cout<<c<<"  is a Consonant"<<endl;      
}

return 0;
}

Upvotes: 0

Views: 1737

Answers (3)

Sebastian Mach
Sebastian Mach

Reputation: 39089

If you want to call bool consonant(char c) like consonant('a'), then you need to translate c into an index first (because 'a' != 0) or use another approach.

In portable C++, you could do it with a big switch:

switch(c) {
case 'b': case'B': case 'c': case 'C': .... return true;
default: return false;
}

In non-portable C++, you could offset c:

c = lower_case(c); // <-- left as exercise
const auto array_size = std::end(is_conson) - std::begin(is_conson);
if (c>=0 && c<array_size)
    return is_conson[c - 'a']

throw std::logic_error(...);

This is non-portable because the C++ standard does not require the characters [a..z] to be contiguous. I don't know if your compiler at hands does support this.

A third, non-portable variant requires separate some special initialization, but allows direct indexing:

std::array<bool,std::numeric_limits<char>::max()> meh() {
    std::array<bool,std::numeric_limits<char>::max()> ret;
    ret['a'] = true;
    ret['A'] = true;
    ...
    return ret;
}

....

    static const auto is_conson = meh();
    if (c >= begin(is_conson) && c<= end(is_conson))
        return is_conson[c];
    throw std::logic_error(....);

Non-portable, because it assumes that all consonants are positive. It is, however, more portable than the previous variant. You could make it portable by also introducing std::numeric_limits<char>::min().

Upvotes: 2

Detheroc
Detheroc

Reputation: 1921

Letters are represented by values 65 through 90 (uppercase) and 97 through 122 (lowercase) in ASCII. You're using the array as if they were starting at index 0. Do something like this:

if (c >= 'A' && c <= 'Z')
    return is_conson[c-'A'];
if (c >= 'a' && c <= 'z')
    return is_conson[c-'a'];
return false;

Also, you should declare the is_conson array as static, so that it's only constructed once and not each time the function is called.

Upvotes: 0

parkydr
parkydr

Reputation: 7784

As it stands your function won't work because ASCII A is 65, you'll need to increase your array to cover all ASCII characters or subtract 'A' (which gives the ASCII value of A) to scale the number to your array. ASCII a is 97, so lowercase letters would have to be scaled by subtracting 'a'

Upvotes: 0

Related Questions