Have you had your dog checked by your vet for a possible health condition? Or has there been a major change in your dog's life? You must make sure that the carpet is thoroughly cleaned to remove the poop smell or your dog will continue to go potty on the carpet.
You should never hit your dog. If you catch your dog in the act of pooping on your carpet, you should say "No" in a stern voice and carry him to the appropriate spot. When he goes potty in the correct location, you should praise him to the hilt.