Use two pivot tables (for the first: Select your range, Insert, Tables, PivotTable, PivotTable, OK, click in displayed box, drag ID into Row Labels, Course into Column labels and grade into Σ values.)
Copy that pivot table to the right and for the left-hand one ensure Σ values has Count of, for the right hand one Sum of. Hide top row, and the Row Labels column for the right-hand table. Rename column labels as required.
To ensure 0 rather than blank in the left hand table, right click on it, PivotTable Options, Layout & Format, check For empty cells show and insert 0. Format of the grade numbers can be adjusted if necessary by left-clicking on the Σ values content, Value Field Settings (also where to change Sum to Count etc) and Number Format. (If formatting is adjusted directly in the pivot table it will undo at the next refresh).
Overwrite Row Labels with ID and hopefully you will get the result you seek.
Note that if you add data into your source range the pivot tables will not alter until you click on either of them and select Refresh.