Introduction to Programming

profilegkurbaji
ec2.pdf

C++  Quiz   QUIZ  2   (Exam  Credit)  

March  2nd,  2015  

Name:      

Page  |  1  

 

 

On  My  Honor  –  I  did  not  receive  any  help  or  used  any  unauthorized  materials   during  this  quiz/exam.  

Name:          

Signature:        

5  Points  Extra  Credit  on  Midterm  Exam  (Optional)    

Program  Trace  Description    

You  are  given  the  following  program.  There  are  no  syntax  errors,  or  logical  errors  in  the  program.  Write   an  explanation  of  how  each  line  the  output  of  this  program  was  produced  by  the  program  starting   with  each  statement  in  main()  and  step  by  step  (output  trace).  

This  quiz  is  OPEN  BOOK  AND  IS  A  TAKE  HOME  QUIZ  (my  advice  is  not  to  waste  time  trying  to  look  up   things  in  the  book  –  it  will  not  help.  

Hints  

Start  from  main()  -­‐  There  are  some  very  easy  questions  –  start  with  them  –  separate  the  page  for  main,   and  the  page  for  functions  and  workout  the  different  function  calls).  

The  void  s(string  ss)  function  is  a  gift  –  and  it  repeats  throughout  the  program.  Make  sure  you  do  all  of   them  and  understand  what  it  does  as  it  repeats.  Get  some  points  at  least  by  following  this  function   through  if  you  are  unable  to  do  the  others.  

What  I  am  looking  for:  You  need  to  provide  the  output  (you  can  easily  get  that  by  running  the  code   through  any  compiler).  But  what  you  will  get  credit  for,  is  a  full  explanation  of  how  each  line  of  main()   ended  up  producing  such  a  line  or  lines  of  output.  In  other  words  write  how  you  traced  each  statement   in  main  to  the  output  it  produces.  

What  do  I  turn  in?   As  you  work  through  the  code,  type  your  explanation  of  the  program  trace,   and  submit  it  with  this  sheet  signed  by  3/4/2015  by  11:59pm.  No  late  submissions  will  be  accepted.  

You  must  include  debugger  screen  shots  of  at  least  5  statements  showing  the  step  by  step  value   passing  and  changes  in  main  and  in  the  called  functions.   Your  Blackboard  submission  means  you  signed  the  Honor  statements  above.  

This  is  an  Individual  assignment  that  is  part  of  the  exam.    

Copying  or  Sharing  your  code  means  you  get  a  zero  on  both  this  quiz  and  the  midterm.  

Also,  if  you  are  absent  from  class  on  Wednesday,  you  will  not  get  credit  for  the  quiz.  I  will  be  taking  attendance.

C++  Quiz   QUIZ  2   (Exam  Credit)  

March  2nd,  2015  

Name:      

Page  |  2  

 

 

Example:   #include  <iostream>   #include  <string>  

    using  namespace  std;   void  foo(int  x,  string  a,  string  b)   {  

  for  (int  i=0;  i<x;  i++)  

cout<<a<<endl;   cout<<"I  printed  ";   cout<<x<<"  times"+  b;  

  }  

  int  main(){  

 

foo(3,  "Hello  World...",  "Thank  you.");   return  0;  

}        

 

     

Explanation:  The  call  to  foo  foo(3,  "Hello  World...",  "Thank  you.")  in  main()  passes  an  integer  3,   and  two  strings  to  foo  –  a  void  function  that  takes  one  int  parameter  argument  x  and  two   string  parameter  agruments  a  and  b.  a  copy  of  “Hello  World…”  is  passed  to  a  and  a  copy  of   (Thank  you.)  is  passed  to  b  –  x  takes  on  the  value  of  3  (x  is  a  local  variable  of  function   foo).  

  Once  in  foo  –  it  repeats  a  loop  3  times  –  only  the  first  statement  after  the  for  loop  header   is  taken  because  there  are  no  braces.  So,  it  prints  a  three  times.  And  then  it  executes  the   other  two  statements  printing  I  printed  (the  value  of  a)  the  value  of  x  times  and  the  value  of   b.  

C++  Quiz   QUIZ  2   (Exam  Credit)  

March  2nd,  2015  

Name:      

Page  |  3  

 

 

#include  <iostream>   #include  <string>   using  namespace  std;  

void  s(string  s){cout<<s<<endl;}   //Watch  for  this  function  -­‐  it  is  everywhere  

void  f1(int  a,  int  b){   int  x=0;   if  (a>b){x=a;  a=b;  b=x;}   for  (int  i=a;  i<b;  i++)  {   cout  <<  !a<<"\t"<<!!a<<"\t"<<i;  cout<<endl;}  

}  

void  f2(int  b,  int  a)   {f1(a,b);}  

bool  f3(){   const  int  max  =  5;   int  a[max];   int  q=5;  

  for  (int  i=0;  i<q;  i++){  

if(i%2==0)   a[i]  =  q+1;   else   a[i]  =  (q+2)  *(-­‐1);  

}   q-­‐-­‐;   while  (q){  

if(a[q]  >  0)   cout<<!a[q];   else   cout<<!!(a[q]-­‐3*q);   q-­‐-­‐;  

}   cout<<endl;   return  q;}  

  int  f4(char  ch)  {  return  (ch>=  'A'  &&  ch  <='I')  ?  1:0;}  

int  f5(int  b,  int  a)  {  return  (b  -­‐  a);}  

int  question_0(string  ss){  s(ss);  return  45/9.0;}     void  question_1(string  ss){  

s(ss);   f1(5,7);   f2(7,6);}  

  void  question_2(string  s)  {cout<<s;}  

void  question_3(string  ss){  

s(ss);   if  (f3())   cout<<  "Great!";   else   cout<<  "Terrific!";   cout  <<"\n";}  

void  question_4(string  ss){  

s(ss);   char  c[]={'M','I','A','M','I'};   for(int  i  =  0;  i<5;  i++)  

cout<<  f4(c[i]);   cout<<endl;}  

  int  question_5(int  a,  int  b,  string  ss)  {s(ss);  return  a+b;}   void  question_6(string  ss)  {  string  tyc="\nThank  you  C++";  s(ss);  cout<<  tyc<<endl;}  

C++  Quiz   QUIZ  2   (Exam  Credit)  

March  2nd,  2015  

Name:      

Page  |  4  

 

 

 

//Our  main()     int  main()   {  

string  ss="-­‐-­‐-­‐";    

cout<<question_0("q0\n")<<endl;   s(ss);   question_1("q1\n");   s(ss);   question_2("q2\n");   s(ss);  question_3("q3\n");   s(ss);   question_4("q4\n");   s(ss);   cout  <<endl<<">>"<<  question_5(f5(5,'b'-­‐'a'),f5('a'-­‐'b',5),"q5\n");   s(ss);  question_6("q6\n");   s(ss);s("END");   s(ss);   return  0;  

}  

C++  Quiz   QUIZ  2   (Exam  Credit)  

March  2nd,  2015  

Name:      

Page  |  5  

 

 

Extra  WorkSheet