-
October 8th, 2009, 04:37 AM
#1
C - <= and >= Problems
I have to convert an array of characters to a Binary value. Without using any C functions the only way that I know to check if something is an integer or letter is to use the ASCII chart. I followed the numbers in my functions but somehow it is not working out at all. The program executes and falls into every if statement even when it shouldnt.
Code:
int convert( const char Val[], int Base , int * InValue )
{
int i;
int count = 0;
bool negFlag = 0;
for( i = 0 ;; i++ )
{
if ( Val[i] == '\0' )
{
break;
}
printf ( "%i\n" , Val[i] );
if (Val[i]<=90 || Val[i]>=65);
{
printf ( "%i\n" , count );
count++;
}
}
i = 0;
int Base10Value = 0;
int Base10Count = count;
printf ( "%i\n" , count );
for( count ; count >= 0 ; )
{
if ((65<=Val[i]<=90)or(97<=Val[i]<=122));
{
int BaseToPower = 1 ;
for ( Base10Count ; ( Base10Count - 1 ) > 0 ; Base10Count-- )
{
BaseToPower *= Base;
}
if ( 65 <= Val[i] <= 90 )
{
Base10Value += ( ((Val[i] - 'A')+10) * BaseToPower);
}
if (Val[i]<= 97 )
{
Base10Value += ( ((Val[i] - 'a')+10) * BaseToPower);
}
count--;
}
if ( ( 48 <= Val[i] <= 57 ));
{
int BaseToPower = 1 ;
for ( Base10Count ; ( Base10Count - 1 ) > 0 ; Base10Count-- )
{
BaseToPower *= Base;
}
Base10Value += ( (Val[i] - '0') * BaseToPower);
count--;
}
i++;
}
thanks in advance
-
October 8th, 2009, 05:58 AM
#2
Re: C - <= and >= Problems
Originally Posted by boydale1
Code:
if (Val[i]<=90 || Val[i]>=65);
There are not many numbers which are not either smaller or equal than 90 or greater or equal than 65. For which number do you expect the above to be false?
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
October 8th, 2009, 08:14 AM
#3
Re: C - <= and >= Problems
can you provide your input string also.
-
October 8th, 2009, 08:22 AM
#4
Re: C - <= and >= Problems
Originally Posted by treuss
There are not many numbers which are not either smaller or equal than 90 or greater or equal than 65. For which number do you expect the above to be false?
Agreed. But you also have a ; at the end of your if statement!!
Meaning the code in the { } will always run.
Edit: In fact, you do this numerous times.
-
October 8th, 2009, 08:31 AM
#5
Re: C - <= and >= Problems
Originally Posted by boydale1
Code:
if ( 65 <= Val[i] <= 90 )
This might not do what you expect it to do either.
it basically evaluates to
Code:
if (( true or false ) <= 90)
you could as well write
try
Code:
if ( Val[i] >= 65 && Val[i] <= 90 )
Kurt
-
October 8th, 2009, 10:53 AM
#6
Re: C - <= and >= Problems
Originally Posted by ZuK
Code:
if ( Val[i] >= 65 && Val[i] <= 90 )
This is correct in C++, but not mathematics, so even though it would work, I would consider this ambiguous code. I much prefer to use parenthesis in situations like that.
-
October 8th, 2009, 11:07 AM
#7
Re: C - <= and >= Problems
Originally Posted by ninja9578
This is correct in C++, but not mathematics, so even though it would work, I would consider this ambiguous code.
Personally, I feel that it is not ambiguous because the relative precedence of >= and && should be well known, thus grouping with parentheses clutters the code. This is also a matter of taste, since one might think that despite the clutter ensuring against misinterpretation by a reader is more important. As for mathematics: I recall that in the mathematical notation that I was taught, the relative precedence of these operators is the same as in C++; but then mathematics should not be a factor here anyway.
-
October 8th, 2009, 12:02 PM
#8
Re: C - <= and >= Problems
The input to the function is as follows.
Code:
char A[] = "100";
int * Value;
convert( A, 10, Value );
Thanks for the help. I cleaned up my code a lot.
Things like:
( 65 <= Val[i] <= 90 )
I picked that up in python which is correct, but in C doesn't work.
thanks for the help.
Last edited by boydale1; October 8th, 2009 at 12:06 PM.
-
October 8th, 2009, 12:12 PM
#9
Re: C - <= and >= Problems
Originally Posted by ninja9578
This is correct in C++, but not mathematics, so even though it would work, I would consider this ambiguous code. I much prefer to use parenthesis in situations like that.
That's not even slightly ambiguous. Why clutter it up with unnecessary parenthesis?
-
October 8th, 2009, 04:03 PM
#10
Re: C - <= and >= Problems
Originally Posted by boydale1
The input to the function is as follows.
Code:
char A[] = "100";
int * Value;
convert( A, 10, Value );
Thanks for the help. I cleaned up my code a lot.
Things like:
( 65 <= Val[i] <= 90 )
I picked that up in python which is correct, but in C doesn't work.
thanks for the help.
You only created a pointer to an int, not an actual int.
Your program will most likely crash if you try to assign a value to it.
It should read:
Code:
char A[] = "100";
int Value;
convert(A,10,&Value);
Edit: Actually you never even touch *InValue so why are you passing it?
Nor are you returning any values. I have no idea how to help you.
-
October 9th, 2009, 03:27 AM
#11
Re: C - <= and >= Problems
Originally Posted by GCDEF
That's not even slightly ambiguous. Why clutter it up with unnecessary parenthesis?
I find the extra parenthesis help with the visual grouping of conditions. More complicated boolean conditions can be fiddly to disassemble without the visual help that parenthesis provide. It also helps when the coder got the operator precedence wrong. The parenthesis force the condition to mean what they actually intended.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
October 9th, 2009, 09:02 AM
#12
Re: C - <= and >= Problems
I agree that its not ambiguous at all.
How do the parenthesis force anything?
-
October 9th, 2009, 09:24 AM
#13
Re: C - <= and >= Problems
Originally Posted by JohnW@Wessex
I find the extra parenthesis help with the visual grouping of conditions. More complicated boolean conditions can be fiddly to disassemble without the visual help that parenthesis provide. It also helps when the coder got the operator precedence wrong. The parenthesis force the condition to mean what they actually intended.
But that case is really simple. To me, the parenthesis imply there's more going on than is readily obvious. Simple is always best IMHO.
-
October 9th, 2009, 09:34 AM
#14
Re: C - <= and >= Problems
A simple example
A && B || C
You could assume that the coder knows the operator precedence and it does what he intended.
If he were to type A && (B || C) then it doesn't matter whether the coder remembers && or || has the higher precedence, it will work as intended.
Now imagine a much longer and more complicated logic test.
I stick to this principle because it alleviates having to debug silly mistakes and lets me concentrate on the bigger issues.
If parenthesis are superfluous, then what about spaces and carriage returns?
Code:
#include<iostream>
#include<string>
int main(){std::string name;std::cout<<"Name?";std::cin>>name;std::cout<<"Hello "<<name;}
It's not about whether things are superfluous or not, it's about readability and conciseness of meaning;
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
October 9th, 2009, 09:46 AM
#15
Re: C - <= and >= Problems
Originally Posted by JohnW@Wessex
A && (B || C)
Sure, but the question is whether it's worthwhile to type
(A) && ((B) || (C)).
I'd say no; everyone should know that the logical operators have lower precedence than comparison operators.
If you had a bitwise operator in there, then it might make sense for clarity.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|