Thursday 9 February 2012

We were posed the following question in OOP344 today:


bool validMonth(int mon, char* errmes){
  bool res = false;
  if(mon > 0 && mon <=12){
    res  = true;
  }
  else{
    strcpy(errmes, "Invalid month (1<=month<=12)");
  }
  return res;
}
write the above function in one line:
bool validMonth(int mon, char* errmes){
  return yada yada;
}
yada yada can have only operators and one function call (no ?: operator allowed)

Here is my answer to the question:
bool validMonth(int mon, char* errmes){
  return (mon>0 && mon<=12) || !(strcpy(errmes,"Invalid month (1<=month<=12)"));
}


No comments:

Post a Comment