The installed() motor class member function will tell you if a motor is installed on a particular port. example of use.
while(1) {
for(int port=PORT1;port<PORT22;port++) {
vex::motor m(port);
if( m.installed() ) {
// do something with a detected motor
}
else {
// no motor on this port
}
}
this_thread::sleep_for(100);
}