unsignedshortint multiplication ( unsignedshortint*x, unsignedshortint*y ) {
return *x * *y; // how to write this line properly? it is printing garbage value
}
The code is correct. However, you're returning a short int which is typically 16 bits and holds values 0-65535. So if your result would be larger than that, the result will overflow.
The problem might actually be in division(). Line 25 is wrong. You want to return *x / *y if y doesn't equal zero.
Finally, the code would be more efficient if you passed the unsigned int's directly instead of using pointers. In other words like this:
1 2 3
unsignedshortint multiplication ( unsignedshortint x, unsignedshortint y ) {
return x * y;
}
@dhayden So if your result would be larger than that, the result will overflow.
im aware of using short int, but i only input 1 and 2 when multiplied is 3, but it is printing 65535
The problem might actually be in division(). Line 25 is wrong. You want to return *x / *y if y doesn't equal zero.
oh thanks.
Finally, the code would be more efficient if you passed the unsignedint's directly instead of using pointers. In other words like this:
it still prints garbage values. base on my observation when i input 2nd number that is higher than first number , it is printing 65500+. otherwise it is not.
You're seeing the problem when you subtract a larger number from a smaller number. That of course results in a negative number. However, you are declaring all your values to be unsigned, which means that negative numbers can not be represented.
When you enter 3 and 4, the signed result of subtracting them would be -1. However, since you've declared your values to be unsigned, you get the twos complement equivalent, which is 65535 (all bits set).
Note also that at line 26 you're doing integer arithmetic. 3/4=0.
You're seeing the problem when you subtract a larger number from a smaller number
o yeah, i thought it was on multiplication function output . i was just too dizzy lol
thats all.
thank you :D
i wouldnt post like this if i noticed that lol xD
by the way, im just practicing to use pointeres
am i using the pointers right?