Post #120,391
10/7/03 10:25:03 PM
|
Thanks guys
I actually got that one; something dredged up from the murky depths of the java I studied last year, I think. Now, I'm having a problem doing some comparisons. I have a function that loops over an io series if the input is outside a valid range: if ((value1 < -127) || (value1 > 127))\n value1 = inputOnError(); However, even if I get a value1 that's less than -127 or greater than 127 the function inputOnError is never getting called. This is true if I use AND (&&) instead of OR too. Along the way I also discovered that I need to reinitialise the "instring" character array to zero between each call to my ascToInt function, or if the new number was shorter than the other one it wouldn't clear out past the new null character terminating the string. I was getting some strange values on subsequent calls to the function before I did that....
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #120,397
10/7/03 10:54:30 PM
|
sheesh, wackiness
if (val1 < -127 || val1 > 127) works, but ((val1 < -127) || (val2 > 127)) doesn't.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #120,399
10/7/03 11:01:11 PM
|
operator precedence
-drl
|
Post #120,412
10/8/03 12:11:44 AM
|
According to my text
they should be equivalent. Anyhoo... this seems hacky at best: sum = value1 + value2;\n\nif (value1 < 0)\n outval1 = value1 - 0xFFFFFF00;\nelse\n outval1 = value1;\nif (value2 < 0)\n outval2 = value2 - 0xFFFFFF00;\nelse\n outval2 = value2;\n\nif (sum < 0)\n outsum = sum - 0xFFFFFF00;\nelse\n outsum = sum;\n\n\nprintf("%x + %x = %x, sum = %d, v = %d\\n", \n outval1, \n outval2, \n outsum, \n sum, \n (sum < -128 || sum > 127)); The idea being that I have to represent a negative value as an eight bit value, minus the leading 'FFFFFF' that's in an int in two's complement form. Further, I have to output the sum as a decimal value, though there are no conditions on the range. I also have to put out the value of the carry bit as it would exist if it were an eight bit signed value, hence the truth value if it falls outside the range. Anyone who can see a more elegant way to do this?
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #120,416
10/8/03 12:25:50 AM
|
use "unsigned char"
..which goes 0..255 instead of char (or short or whatever) which goes -128..127.
I checked and < and >, || do have different precedence but || is *lower* than < and > so your grouping should have had no effect :/
When comparing things of different type, use a cast if you want to be certain you are comparing things of the same type, i.e.
int A = 258; if ((unsigned char) A == 2) puts("Whoa\\n");
-drl
|
Post #120,431
10/8/03 3:31:57 AM
|
Re: According to my text
Something along the lines of: \n#include <stdlib.h>\n#include <stdio.h>\n\nint main(int argc, char **argv)\n{\n unsigned char v1, v2, sum;\n unsigned int largeSum;\n \n v1 = atoi(argv[1]);\n v2 = atoi(argv[2]);\n \n sum = v1 + v2;\n largeSum = (unsigned int)v1 + (unsigned int)v2;\n \n printf("%02X + %02X = %02X (carry is %d)\\n", \n\t (unsigned int)v1, (unsigned int)v2,\n\t (unsigned int)sum, (largeSum & 0x100) != 0);\n}\n \n\n Here is the output \nC:\\cygwin\\home\\Arkadiy>.\\test 12 13\n0C + 0D = 19 (carry is 0)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 255 255\nFF + FF = FE (carry is 1)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 255 254\nFF + FE = FD (carry is 1)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 126 127\n7E + 7F = FD (carry is 0)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 127 127\n7F + 7F = FE (carry is 0)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 127 128\n7F + 80 = FF (carry is 0)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 128 128\n80 + 80 = 00 (carry is 1)\n Commentaries: unsigned char is used for arithmetics as it gives you modulo 255 for free. To compute carry, I use unsigned int (thus preserving the carry) and test for the value of the ninth bit using bitwise and (signed int would work too). Note how unsigned char is cast to unsigned int in printf. I think it's necessary, because printf uses ellipsis (...) and ellipsis converts anything smaller than int to int. In the process, it may cause sign expansion (converting 0xFF to 0xFFFFFFFF). So , to be safe, we explicitly tell the compiler _how_ to convert to the int size using cast. Since we are casting from unsigned to unsigned, sign expansion does not happen. It may be that the above is just my paranoia. But better safe than sorry. It's also possible to acheve the same result by simple bitwise anding with 0xFF in the printf statement - bitwise and with an integer converts to integer, and does it properly. Note that I do not check if argv[1] or [2] are present. Not a good thing to do for real world.
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #120,429
10/8/03 3:09:52 AM
|
Should be equivalent indeed
Except the first one tests val1 only, and the second one tests val1 and val2
What us the type of those values?
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #120,432
10/8/03 3:53:42 AM
|
Well, interestingly enough
I get correct results when I do if (val1 < -127 || val1 > 127), but not when I do if ((val1 < -127) || (val1 > 127)). Which is strange; it's not what you'd think it should do.
val1 is an int.
The val2 in my earlier post was a typo when putting it into here; by that time I was getting squirrely.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #120,439
10/8/03 8:49:08 AM
|
Re: Well, interestingly enough
Are you sure you're not using '|' instead '||' ?
Copy/paste the code...
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #120,539
10/8/03 8:48:57 PM
|
Yes, I was sure.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #120,591
10/9/03 7:07:56 AM
|
copy/paste the full code, then
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #120,675
10/9/03 7:41:26 PM
10/10/03 12:59:17 PM
|
Sure, here's my final version
\n/* Source file for the C program in Assignment 1 of CISC221 - Computer\n * Architecture, for Dr. Clive Dove. */\n\n/* Header Declarations */\n\n#include <stdio.h>\n\n#define SIZE 80\n\nint value1, value2, sum; // variables to hold values entered by user after \n // conversion to numbers.\n\nchar instring[SIZE]; // String to hold number entered by user. Size is large \n // to ensure that there is no buffer overflow.\n\nchar error[20]; // string to hold error messages.\n\n/* Here there be function declarations. There are two functions; one to\n * convert an ascii string into an int value, and one to handle erroneous\n * input by the user. */\n\nint ascToInt(char []); // Converts ascii strings to ints.\n\nint inputOnError(char []); // Handles input outside specified range.\n\nint main()\n{\n\n while (1)\n // this loops until user puts sentinal value into value1 that causes program\n\n // to exit.\n {\n printf("Please enter a number between -128 and 127.\\nEnter zero to exit:");\n scanf("%s", instring);\n value1 = ascToInt(instring);\n if (value1 == 0)\n return 0; // Exit the program when user inputs 0 into first value.\n\n printf("Please enter another number between -128 and 127:");\n scanf("%s", instring);\n value2 = ascToInt(instring);\n\n sum = value1 + value2;\n\n printf("%x + %x = %x, sum = %d, v = %d\\n", value1 & 0xFF, value2 & 0xFF, sum & 0xFF, sum, (sum < -128 || sum > 127));\n }\n}\n\n\nint ascToInt(char *s)\n{\n int result = 0;\n int sign = 1;\n\n if (*s == '-') // Test for a sign in the first character of the string.\n\n {\n s++;\n if (!*s) // if next character is null, - is only character.\n result = inputOnError("not a number");\n else\n sign *= -1;\n }\n while (*s >= '0' && *s <= '9') // while there are digits in the string.\n\n {\n result *= 10;\n result += *s - '0';\n s++;\n }\n result *= sign; // apply the sign\n\n if (*s) // if there is anything other than a digit in the string.\n result = inputOnError("not a number");\n\n\n if (result < -128 || result > 127) // if result is outside of range.\n\n result = inputOnError("outside the range specified");\n return result;\n}\n\n/* inputOnError is the error handler for invalid input. It takes one argument\n * of a string indicating the nature of the error, prompts again for input,\n * and then uses the ascToInt function to value it, and returns the result to\n\n * the calling function. */\n\nint inputOnError(char *error)\n{\n int output;\n do\n {\n printf("The value you entered was %s.\\nPlease re-enter a number between -128 and 127:", error);\n scanf("%s", instring);\n output = ascToInt(instring);\n }\n while (output < -128 || output > 127);\n\n return output;\n} |
edit: I just wrote it... again:) This time I'm done 'cause after this I'm going to have a beer. I'd esp. like to thank Todd for giving me the knife and fork and inviting me to dine. edit2: took out my student number.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
Edited by jake123
Oct. 9, 2003, 10:27:30 PM EDT
Edited by jake123
Oct. 10, 2003, 12:59:17 PM EDT
|
Post #120,746
10/10/03 8:45:17 AM
|
Works with or without parens for me
VC 6 compiler
BTW, is it OK to criticize the code? Or would you rather hear from your Professor first?
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #120,768
10/10/03 10:49:06 AM
|
Feel free
Do realise that certain things were written in stone... like the output.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #120,824
10/10/03 4:09:28 PM
|
Re: Feel free
Well, you have a function \nint ascToInt(char []); // Converts ascii strings to ints.\n In reality, that function does nothing of the sort. It parses the string into integer, but then it checks that the integer is in range, handles the range errors, reads the string again (indirectly) an so on. How about clearly separating string to integer conversion from reading, validation and error handling? See where that takes you.
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #121,056
10/13/03 11:59:46 AM
|
You're suggesting that
I should break the function up two or three others?
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #121,062
10/13/03 12:48:50 PM
|
Basically, yes
Make the content really match the name.
--
OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie. [link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
|
Post #120,808
10/10/03 1:50:05 PM
|
Works here too, both ways. gcc 3.3.2
|
Post #120,818
10/10/03 3:25:25 PM
|
It is working here
which basically means I screwed up somewhere, but since that version of the program is long dead (I wrote it about five times), I can't really go back and see where.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #120,485
10/8/03 1:28:43 PM
|
What is the type of 'value1' ?
If it is 'char', there is only one value that will cause this expression to be true: 0x80 (or -128). Doing something like this:
char value1;
value1 = 4261; if (value1 < -127 || value1 > 127) ...
will still result in false, because 4261 won't resolve to -128 when truncated.
jb4 Boy I'd like to see those words on a PR banner behind [Treasury Secretary John] Snow at the podium: Jobs and Growth: Just Wait. John J. Andrew, unemployed programmer; see jobforjohn.com
|
Post #120,538
10/8/03 8:48:05 PM
|
Nah, it's an int
We had to store them as ints, and then juggle them to get good output in hex as if it was a char.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|