Hi Dhivya,
I recommended not to write explicit coding for Total . Please use SAP Sum button in alv toolbar on alv display screen . Please Save your layout with this functionality and make it global layout.
So each and every time you will run this report you will get grand total at the end. No need to write coding for Sum and sub sum functionality.
Don't forgot to take your layout in Transport request in which your report is exist . You can use standard program to take layout of any report in Transport request.
Hope this will work.
Regards,
Sagar.