Computer Science

CS  171  Computer  Programming  1   Winter  2017

Assignment  3:    Loops  and  Reference  Parameters

Purpose:

After  completing  this  assignment  you  will  have  designed,  implemented,  debugged,  compiled   and  provided  documentation  for  a  C++  program  that  involves  loops  and  reference  parameters

 

The  Assignment:

This  assignment  consists  of:

1. A  custom  problems  requiring  you  to  write  functions  that  may  include  branches,   functions,  loops  and  reference  parameters.

In  addition  to  the  problem  specifications,  this  document  contains:

• What  to  Submit   • Grading  Rubric     • Academic  Honesty

 

Make  sure  you  read  everything!

 

 

 

 

 

 

 

 

 

Problem  1

A  person’s  Blood  Alcohol  Concentration  (BAC)  has  been  strongly  correlated  with  driving   impairment  to  the  extent  that  there  are  legal  designations  regarding  level  of  impairment.  See   charts  such  as  http://www.brad21.org/bac_charts.html  for  examples  -­‐  this  page  contains  links   for  female  and  male  BAC  charts.  While  actual  blood  alcohol  level  varies  with  the  individual  and   the  situation,  the  mathematical  model  explained  below  yields  fairly  accurate  predictors  for   most  people.

There  are  several  factors  involved  in  computing  a  person’s  BAC  -­‐  including  amount  of  alcohol   consumed,  duration  of  the  drinking  session,  the  person’s  weight,  and  their  gender.  (Other   factors,  such  as  age,  physical  condition,  amount  of  food  consumed,  etc.  are  not  taken  into   account.  Your  mileage  may  vary.)

For  our  purposes:

• The  amount  will  be  measured  in  number  of  drinks,  which  is  equivalent  to  a  12  oz.  beer,   1.25  oz.  of  80  proof  liquor,  or  5  oz.  of  table  wine;

• The  duration  will  be  measured  in  minutes.  Our  calculations  will  assume  BAC  reduces  by   .01  for  each  40  minutes  spent  drinking.  What  this  means  is  that  if  you  have  five  drinks   but  do  this  over  two  hours,  your  BAC  is  reduced  compared  to  what  would  be  if  you   drank  five  drinks  quickly  in  a  row  and  then  immediately  measured  your  BAC;

• The  weight  will  be  measured  in  pounds   • The  formulas  vary  according  to  gender.

So,  how  do  we  determine  a  person’s  BAC?  This  is  the  volume  of  alcohol  in  the  bloodstream   divided  by  the  volume  of  blood.  The  volume  of  alcohol  is  proportional  to  the  amount  ingested   and  the  volume  of  blood  is  proportional  to  the  person’s  weight,  so  that  the  BAC  can  be  found   by  dividing  the  number  of  drinks  by  the  person’s  weight  and  multiplying  by  a  constant.  The   constant  is  4.5  for  females  and  3.8  for  males.  The  BAC  gets  reduced  by  the  time  spent  after  the   last  drink  by  .01  for  each  40  minutes.  For  example,  for  a  130  pound  woman  who  has  consumed   3  drinks  and  then  waited  80  minutes  to  take  her  blood  alcohol  concentration  would  have  a  BAC   of  approximately  0.084.    A  160  pound  woman  who  consumed  3  drinks  then  waited  300  minutes   to  take  her  blood  alcohol  concentration  would  have  a  BAC  of  approximately  0.0094.

The  BAC  model  predicts  certain  effects  according  to  BAC.  DUI  laws  specify  a  penalty  for  higher   concentrations  of  BAC.  The  following  table  describes  notable  relationships:

BAC   0.00   0.00–0.04   0.04–0.08   0.08–0.10   ≥0.10

Description   Safe  to   Drive

Some   Impairment

Driving  Skills   Significantly   Affected

Criminal  Penalties   in  Most  US  States

Legally  Intoxicated  -­‐   Criminal  Penalties  in  All  US   States

For  each  category,  the  lower  bound  is  strict;  that  is  a  driver  with  BAC=0.08  is  in  the  “Criminal   Penalties  in  Most  US  States”  category.  The  only  exception  is  “Some  Impairment”,  which  begins   at  any  value  above  0.

To  the  above  table  we  add  the  additional  information  that  if  the  concentration  is  above  0.30,   death  is  possible.

Our  ultimate  goal  is  to  write  a  program  that  allows  the  user  to  enter  the  weight,  sex  and  time   since  last  drink,  and  then  prints  out  a  table  of  information  showing  the  status  according  to  the   BAC

Below  is  a  sample  output  (with  proper  formatting):

Enter your weight (in lbs): 150 How many minutes has it been since your last drink? 0 Enter your sex as M or F: F 150 pounds, female, 0 minutes since last drink # drinks BAC Status 0 0.000 Safe To Drive 1 0.030 Some Impairment 2 0.060 Driving Skills Significantly Affected 3 0.090 Criminal Penalties in Most US States 4 0.120 Legally Intoxicated – Criminal Penalties in All US States 5 0.150 Legally Intoxicated – Criminal Penalties in All US States 6 0.180 Legally Intoxicated – Criminal Penalties in All US States 7 0.210 Legally Intoxicated – Criminal Penalties in All US States 8 0.240 Legally Intoxicated – Criminal Penalties in All US States 9 0.270 Legally Intoxicated – Criminal Penalties in All US States 10 0.300 Legally Intoxicated – Criminal Penalties in All US States 11 0.330 Death is Possible!

We  help  you  develop  this  program  by  describing  a  series  of  functions.  You  will  have  additional   latitude  in  design,  so  will  also  submit  your  source  code  and  output  on  Blackboard  to  be  looked   at  by  the  graders.

Using  the  information  given  above,  design  and  implement  a  function  that  has  the  following   prototype:

void computeBloodAlcoholConcentration(int numDrinks, int weight, int duration, double &maleBAC, double &femaleBAC);

This  function  computes  the  blood  alcohol  concentration  for  the  given  number  of  drinks,  the   given  weight,  and  the  given  elapsed  time  since  the  last  drink  (duration).  It  returns  via  reference   parameters  both  the  male  and  female  blood  concentrations  for  that.

 

 

Note:    Error  tolerance  in  answers  returned  by  computeBloodAlcoholConcentration

As  we  (hopefully)  recall,  floating  point  arithmetic  introduces  little  errors  in  calculations   because  of  the  limited  precision  of  the  numbers.    It  is  not  true  that  basic  mathematical   facts  such  as  the  distributive,  associative,  or  commutative  laws  hold  for  computations   done  with  floats  or  double.        At  any  rate,  the  errors  introduced  by  using  limited-­‐ precision  arithmetic  means  that  two  formulas  that  are  mathematically  equivalent  may   not  produce  exactly  the  same  results  when  float  or  double  data  and  arithmetic  is   used  in  the  calculation.  We  aren’t  forcing  you  to  write  the  formula  you  use  in  the  most   accurate  way  (which  would  be  to  find  an  integer-­‐based  way),  so  we  should  be  willing  to   accept  any  value  within  a  few  rounding  errors  of  what  we  get.

Problem  2

Using  the  information  given,  design  and  implement  a  function  with  the  prototype:

string impairment(double bac);

that  returns  (as  a  string)  the  appropriate  message  for  the  blood  alcohol  concentration.  You   should  use  the  global  constant  declarations  as  described  below.

const double safe = 0.00; const double someImpairment = 0.04; const double significantAffected = 0.08; const double someCriminalPenalties = 0.10; const double deathPossible = 0.30; const string SAFE = “Safe To Drive”; const string SOMEIMPAIR = “Some Impairment”; const string SIGNIFICANT = “Driving Skills Significantly Affected”; const string MOST_STATES = “Criminal Penalties in Most US States”; const string ALL_STATES = “Legally Intoxicated – Criminal Penalties in All US States”; const string YOURE_DEAD = “Death is Possible!”;

Problem  3

Once  you  have  these  two  functions  working,  write  the  following  three  functions  whose   prototypes  are:

• int promptForInteger(const string &message, int lower, int upper); • char promptForMorF(const string &message); • void showImpairmentChart(int weight, int duration, bool isMale);

Descriptions  of  the  functions  are  as  follows:

int promptForInteger(const string &message, int lower, int upper);

When  called  this  function  prints  out  the  message  and  gets  an  integer.  If  the  integer  is   greater  than  or  equal  to  lower  and  less  than  or  equal  to  upper  then  it  returns  the   integer.  Otherwise,  it  loops,  asking  for  an  integer  again.

(Note:  this  is  a  good  place  to  use  the  do{…}while ( ) ;  version  of  a  while  loop.)

char promptForMorF(const string &message);

This  function  prints  out  the  message  and  returns  the  value  entered  as  its  result.  The   function  checks  that  the  input  character  entered  is  an  M  or  an  F.  If  it  isn’t  it  just  prompts   again.  You  can  program  the  function  to  check  for  exactly  an  upper  case  ‘M’  and  ‘F’,  or   you  can  do  the  extra  programming  to  handle  things  more  liberally,  as  you  prefer.

void showImpairmentChart(int weight, int duration, bool isMale);

This  function  prints  a  chart  similar  to  that  shown  in  Problem  1  for  up  to  and  including   ten  drinks  and  with  nicely  formatted  output.  We  expect  a  sensible  number  of  decimal   digits  commensurate  with  the  expected  accuracy  of  the  measurements.  You  may  need   to  stop  and  think  a  moment  about  how  much  accuracy  is  warranted.  If  you  recall  your   high  school  physics,  it’s  difficult  to  justify  using  more  digits  than  the  accuracy  of  the   measurements  warrant.  If  we  are  thinking  of  a  sobriety  test  administered  by  the  side  of   the  road,  it’s  unlikely  that  they’d  have  a  scale  accurate  to  the  nearest  .001  pound  for   weighing  people,  nor  measure  the  time  since  the  last  drink  more  closely  than  to  the   nearest  minute.  Your  program  comments  for  this  function  should  justify  why  you  used   the  accuracy  you  did.

 

 

 

Finally,  you  must  write  a  main  program  that  uses  the  code  you  wrote  and  prompts  the  user  for:

1. Weight  in  pounds   2. Time  since  last  drink  (in  minutes)   3. M  or  F  to  indicate  that  the  information  is  for  a  male  or  female.

The  program  then  prints  out  a  single  column  of  the  blood  alcohol  chart  in  a  format  similar  to   that  in  Problem  1

 

Submission

All  homework  for  this  course  must  be  submitted  electronically  using  Blackboard  Learn.  Do  not   e-­‐mail  your  assignment  to  a  TA  or  Instructor!  If  you  are  having  difficulty  with  your  Blackboard   Learn  account,  you  are  responsible  for  resolving  these  problems  with  a  TA,  an  Instructor,  or   someone  from  IRT,  before  the  assignment  it  due.  It  is  suggested  you  complete  your  work  early   so  that  a  TA  can  help  you  if  you  have  difficulty  with  this  process.

For  this  assignment,  you  must  submit:

Problems  1-­‐2:    NOTHING  (kind-­‐of)!!

• The  work  you  do  will  contribute  to  the  last  question.

Problem  3:

• Your  C++  source  code  file  that  contains  all  the  functions  of  Programs  1-­‐3.  Just  submit   your  “.cpp”  file,  NOT  your  .dsp,  .dsw,  or  similar  IDE  project  files.

• A  PDF  document  with  your  User  Manual   • A  PDF  document  with  your  System  Manual   • For  this  assignment  we  will  ask  for  a  test  log  file  in  the  form  of  a  PDF  document

containing  screen  shots  of  your  program  running  over  several  tests.    Two  or  three  tests   should  be  suffice.    We  encourage  you  to  try  “odd”  tests.

 

Remember,  now  that  we  have  functions  in  our  code  they  all  must  have  proper  internal  and   external  documentation

 

 

 

 

 

 

 

 

 

Grading  Rubric:

Note  that  all  homework  assignments  will  be  given  the  same  weight.    The  points  assigned  in  a   given  assignment  are  simply  to  provide  a  natural  level  of  resolution  for  grading.    You  can  always   rescale  it  to  out  of  100%  if  you  want.

1. Function  from  Problem  1  Correct             20pts   2. Function  from  Problem  2  Correct             20pts   3. Program  3  doesn’t  have  any  syntax  errors           10pts   4. Program  3  produces  correct  result  (proper  logic)         20pts   5. Program  3  produces  correctly  formatted  output         10pts

In  addition,  for  Program  3  (which  includes  Program  1-­‐3)

4. Coding  style:  consistent  adherence  to  coding  standards  described  in  assignment  2  (the   previous  assignment).  Through  use  of  variable  names,  indentation,  and  comments  it   makes  itself  easy  to  comprehend.    Those  who  want  to  adopt  variations  need  justify  their   divergences  and  must  provide  in  your  written  comments  a  justification  that  is   persuasive  and  convincing  to  the  grader.  (You  may  wish  to  talk  to  the  staff  about  this   rather  than  assuming  that  you  know  what  they  like.)  Coding  styles  reflect  what  code   readers  want  to  read,  not  just  what  programmers  want  to  write.

o 5  points  =  excellent.  Code  is  included  in  a  .cpp  file  submitted  to  blackboard,  as   well  in  readable  form  in  the  .pdf.

o 4  points  =  good  but  a  few  improvements  are  in  order   o 2  points  =  many  improvements  needed  for  code  to  be  readable  for  a  general

audience  of  programmers,  or  the  formatting  of  the  code  in  the  .pdf  or  .cpp   makes  it  unpleasant  and  difficult  to  read  (grader’s  opinion  is  what  matters  here  -­‐   if  you  have  any  doubts,  you  should  ask  before  submitting.).

o 1  point  =  missing  major  principles,  should  talk  to  staff  about  this  to  do  better   next  time.  This  is  the  maximum  number  of  points  you  can  get  in  this  category   regardless  of  style  quality  if  your  program  does  effectively  print  out  the  table  it   was  designed  to  do.

5. Program  construction

o 5  points  =  excellent.  Code  is  not  needlessly  complex.  Functionality  is  encapsulate   d  in  the  mandated  functions  and  used  in  that  fashion  in  the  program  submitted.   The  program  is  written  in  a  way  that  makes  extension  or  changes  easy  to  do.   Global  constants  are  used  as  mandated.  Computations  should  not  be  needlessly   complex,  use  math  to  simplify  calculational  expressions  or  logic  to  simplify  if   statements  or  complicated  Boolean  expressions.  The  grader’s  judgment  is  the   relevant  measure  here.

o 4  points  =  good,  but  a  few  improvements  are  in  order.

o 2  points  =  many  improvements  needed  for  code  to  come  up  to  expectation  for   coding  quality.

o 1  point  =  many  flaws,  should  talk  to  staff  about  this.  This  is  the  maximum   number  of  points  you  can  get  in  this  category  if  your  program  does  effectively   print  out  the  table  it  was  designed  to  do.

6. External  Documentation   This  includes  content  in  the  User  Manual,  System  Manual,  and  for  this  assignment   the  Test  Log  (via  screen  shots  assembled  into  a  PDF)

o 10  points  =  function  definitions  are  commented  with  full  explanation  of  purpose,   parameters,  results,  and  side  effects,  in  the  style  of  section  4.3  of  the  textbook   “Function  Comments”.

o 8  points  =  good,  but  some  significant  missing  information  that  would  ordinarily   be  expected

o 4  points  =  needs  significant  improvement.  Major  miss.   o 2  point  =  see  the  instructional  staff  to  do  better  next  time.

 

 

 

Academic  Honesty

You  must  compose  all  written  material  yourself,  including  answers  to  book  questions.  All   material  taken  from  outside  sources  must  be  appropriately  cited.  If  you  need  assistance  with   this  aspect  of  the  assignment,  see  a  consultant  during  consulting  hours.

To  ensure  that  assignments  are  done  independently,  in  addition  to  human  observation,  we  will   be  running  all  assignments  through  a  plagiarism  detection  system.    This  program  uses  compiler   techniques  which  are  invariant  of  syntax  and  style.      It  has  a  very  high  accuracy  rate.

 

 

Order now and get 10% discount on all orders above $50 now!!The professional are ready and willing handle your assignment.

ORDER NOW »»