We human beings have always realized that there is a relationship between cause and effect: there is no effect without a cause, even though the cause may not be totally intelligible to us. The Bible was composed during ages when human beings still had a very pre-scientific view of the world and produced rather simplistic explanations of the cause of events.
Taking the story of David's census too literally will make God seem cruel in punishing the people merely because of David's census. To understand a story like this we must get beyond the details to the teaching.
Objectively speaking, a mere administrative matter like holding a census would not be a grave sin. However, when Joab reported the number of men "fit for military service", David seems to have realized that knowing the strength of his army was a matter of pride and a failure to put all his trust in God and so he immediately regretted his action. He should have thought of the consequences of his action.
In having David choose between three options, the writer wishes mostly to teach that God is most merciful and deals with us in a kindlier way than our fellow human beings do.
Father in Heaven, grant us the grace of prudence that we will always think of the consequences of our actions before we act.