Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how i can write a function that will return 0 if letter between (a,i)and returns 2 if charactar between (j,r)..
i can't use if statement..

What I have tried:

C
int rangelndex(char c)
{
if(c>='a'&&c<='i')
return(0)
Posted
Updated 29-Apr-16 4:45am
v2
Comments
nv3 29-Apr-16 10:05am    
Basically the same question as in http://www.codeproject.com/Questions/1096602/How-can-I-write-a-function-that-returns-true-only.

Why didn't you follow my advice on the last question?
[no name] 29-Apr-16 10:10am    
i did , but the problem now if i add more than 2 conditions
[no name] 29-Apr-16 10:12am    
for example: if i add also that between (r and z)i want to return foe example 3 ...
how can i add more without using if statement
PIEBALDconsult 29-Apr-16 10:09am    
It's _your_ homework; _you_ do it.
[no name] 29-Apr-16 10:14am    
i am doing it myself but i want to understand functions and how to use return corectly before even try to solve my homework.

Start off by always using curly brackets, even when it's only a single statement:
C++
if (condition)
   {
   doSomething();
   }
It can prevent a lot of head scratching when you edit your code and it looks like it should work:
C++
if (condition)
   doSomething();
   doSomethingElse();
That's not the same as:
C++
if (condition)
   {
   doSomething();
   doSomethingElse();
   }

Then look at what you have written: it's about right for starters!
C++
int rangeIndex(char c)
   {
   if (c >= 'a' && c <= 'i')
      {
      return 0;
      }
   ...
   }
That code returns zero if the character is your "Low range".
So all you have to do is duplicate it, change the range, and change the value you return.
I'd add a third return statement at the end in case it's not in either range myself, perhaps returning -1?

Give it a try: it's pretty simple!

"i did what you wrote, but again my main problem is i can't use if statement"
"what return-1 mean ?"



Last one first: return -1 means to return a negative value. Think about it:
If you pass 'a' you want it to return 0.
If you pass 'r' you want it to return 1.
If you pass 'x' what is it going to return? It can't return "nothing"!

Now the first: I assumed when you said "I can't use if statement" that you meant you don't know how to! I take it this is your tutor restricting your answer?

OK, the other form you describe can do it - you can "nest" them:
C#
return (condition1 ? 0 : (condition2 ? 1 : 2));

So yes, that will work - I'll tidy it up for you:
C#
return ((c >= 'a' && c <= 'i') ? 0 : ((c >= 'j' && c <= 'r') ? 1 : -1));

But... technically, that's still an if (well...two ifs :laugh:)
Have you considered a switch?
C++
switch (c)
   {
   case 'a':
   case 'b':
   case 'c':
   case 'd':
   ...
   case 'h':
   case 'i':
      return 0;
   case 'j':
   case 'k':
   ...
   case 'r':
      return 1;
   default:
      return -1;
   }
It's a lot more long-winded, but it's also a lot more readable and much more flexible. And it definitely doesn't use an if!
 
Share this answer
 
v2
Comments
[no name] 29-Apr-16 10:27am    
thank you , i did what you wrote, but again my main problem is i can't use if statement ,

int rangelndex(char c)
{
return(c>='a'&&c<='i'?0:((c>='j'&&c<='r')?2...)
what about something like this ?
[no name] 29-Apr-16 10:29am    
and what return-1 mean ?
OriginalGriff 29-Apr-16 10:57am    
Answer updated
[no name] 29-Apr-16 11:19am    
wow thanks alot for the reply, i can't use switch eaither lol(i know how to but i can't use it in this question:/) but i guess i can use return (condition1 ? 0 : (condition2 ? 1 : 2));
i mean there is no an actuall (if) there , and i don't think there is another way lol so yeah i am gonna do ot this way :)
OriginalGriff 29-Apr-16 11:30am    
Well... there is another way, that involves no if at all - in fact it's the most processor efficient way to do it, but it takes effort to set up.
Think about it: how many different values can you get in a character?
the answer is 128, because it's a signed eight bit value and we can ignore negatives.
So if you set up an array of integers, so that you could access the array using the char value as an index...you could return your value with just:

return charTranslate[c];

It's called a "lookup table" and it's very, very efficient!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900