if you want to use awk you wouldn't use a while loop, as awk is for processing text streams, so naturally iterates over its inputs. Additionally using ps will sort by uid with the right options so it's actually really simple from then on, to just keep track of the current uid and print a new header each time the uid of the current record changes.
Code:
{
if (userid != olduserid) {
# print the headers out
}
# print the process data
olduserid = userid
}
I'm a bit concerned that this is homework though, so i'm not going into any more detail at the moment. Should be fairly simple from here though.