Hi all, I want to question way do following exercise
Enter a number is a, check number a divisible by 3 and put the results on the screen. Don't use operator +,-,*/, %, ++ .... in c++ programming
Hi all, I want to question way do following exercise
Enter a number is a, check number a divisible by 3 and put the results on the screen. Don't use operator +,-,*/, %, ++ .... in c++ programming
Last edited by hoinongdan; 09-16-2014 at 04:30 AM.
Is this a homework assignment? If it is, then your teacher is a jerk. I'm not sure it's even possible to do this without those operators.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
I suppose it might be possible by using some bit-shifting trickery (maybe not ... I haven't really thought about it).
The only way I can think of to do it is to use inline assembly, and of course that's not portable, nor is it an elegant solution. I think it's just ridiculous that you're not allowed to use mathematical operators to perform a task that is intrinsically mathematical. You will never encounter a real programming task where you would need to do something like this.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
> Don't use operator +,-,*/, %, ++ .... in c++ programming
What about += (and all the other assignment operators) ?
What about -- ?
The first thing you need to do is look up how addition and subtraction is implemented in computer hardware using a series of AND and OR gates. Then you can synthesise these operations in software. If you can synthesise subtracting 1, you're 90% done.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Can you use fmod()?
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Proplem here is to buildi their operator, so the operator don't use the programming language
These operators compile down to single assembly instructions. You can't write anything more fine grained then that. All you'd be doing is doing the same operation 10 times slower.
If you want to learn how divisibility checks can be implemented in hardware, C++ isn't the language to use. C++ doesn't offer good operators for modifying individual bits. You want a hardware description language like VHDL or Verilog. That would also allow you to implement an operation like this on simulated or real hardware.
Then of course you need an algorithm to compute the operation (divisibility by 3). A quick google suggests a simple algorithm for doing this given the number in binary (which is how numbers are generally stored).
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
I don't see any problem with implementing a class for manipulating bits. There's actually one already in the standard library, if you want to use it. it's called std::bitset.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
Ok, but I stand by the first part of what I said: "If you want to learn how divisibility checks can be implemented in hardware, C++ isn't the language to use."
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.