Should be exactly the same method you use to extract a value from a table cell but a "get" rather than "set".
The "get" is something like:
somevar = somecell.innerHTML;
So the "set" should be:
somecell.innerHTML = somevar;
In your case you have
<td id="totalP"></td>
<td id="totalC"></td>
So the last two lines of your script should be:
document.getElementById('totalP').innerHTML=countP;
document.getElementById('totalC').innerHTML=countC;
One other suggestion. Since you intend the countP and countC as numbers but you extract the values from innerHTML which is a string and at that html, you may want to explicitly convert the innerHTML to Number just to be on the safe side. Something like:
countP+=Number(theCells[3].innerHTML);
hth 