CIS 1.5 Test 1 Spring, 2007 Page 1 of 6 Name_________________________


1. Find and correct 10 compilation errors in the following section of code. Write the corrected line to the right of the original. Do not change anything that is already correct. (10 points)


include <iostreem> _________________________________


using manespace std; _________________________________


int Main() _________________________________

{

     ints num=9,val,sum=2,j; _________________________________


     cin > val; __________________________________


     for(j = 5; j < num; j+1) { _________________________________


           num =+ val; _________________________________


           if (num = j + 5); _________________________________

              

               j++5; _________________________________

 

           else _________________________________

   

               sum + num; __________________________________


     } 

      retrun 0; _________________________________


}


2.a. (1 pt) Write a declaration for a variable temperature that can hold the current indoor temperature, measured to the half degree (like 75.0 or 66.5).



b. (2 pts) Write a statement that declares a constant integer SIZE and gives it the value 100.



c. (1 pt) Write a statement to subtract the values of dues from the value of grosspay, storing the result in takehome. (The variables have already been declared and dues and grosspay have already been initialized.)



d. (2 pts) Given the variables ItemPrice and OrderAmt, already declared, write an expression that gives OrderAmt a value corresponding to the price of 8 items.



e. (3 pts) Write a statement that assigns 1 to the variable late if the variable arrival_time is greater than 9; otherwise it assigns 0 to late. (The variables have already been declared and arrival_time has already been initialized.)


f. (2 pts) Write the prototype for a function called findout which receives two parameters, an integer and a double, and returns nothing.



3. Evaluate the following according to the C++ precedence rules. Show what is printed in each case. Make sure that you show your work; make it clear which operation is done first, then second, etc., until you show the final answer. To get credit, you must show your work. (4 pts)


      int x=6,y=5,z=9;

      

    if (x - 3 * y > z % y + 2)

        cout << "day";

    else

         cout << "night"; 



4. Trace the following section of code. Under OUTPUT, clearly show what is displayed on the screen, in order, as the program executes. Under TRACE, show how the variables change. (10 points)

 

    int a, b = 9,c = 9 ;

 

     cout << "ready" << endl;

     for (a = 2; a < b; a += 3) {

          cout << "first: a = " << a << " b = " << b << " c = " << c << endl;

          if (a < c - 2)

              c -= 2;

          else

               c += 5;

          a++;

          cout << "now: a = " << a << " c = " << c << endl;

     }

     cout << "last: a = " << a << " b = " << b << " c = " << c << endl;

 

TRACE: OUTPUT:


5. (10 points)

a. Write a function findSmaller, which receives two double parameters and returns a double. The function returns the smaller value of the two parameters.


b. Write the prototype for the function, and write the call to the function from the main program. Clearly identify which is the prototype and which is the call.

    You do not need to write the complete main program, but write a declaration for any variables you use in the main program.

 

6. Trace the following program and show exactly what is printed. Assume that the data values shown below are typed in as needed, in response to the prompts: 13 15 18 18 16 4 (12 pts)


#include <iostream>

using namespace std;

int doit(int, int);

int main()

{

      int a,b,ans;

 

      cin >> a;

     cin >> b;

      while(b > a){

           ans = doit(a,b); 

           a = a - 10;

           cout << "a = " << a << " b = " << b << " ans = " << ans << endl;

           cin >> a;

           cin >> b;

      }

     cout << "a is now " << a << " and b is now " << b << endl;

      return 0;

}


int doit(int x, int y)

{

     int w;


      cout << "in doit: x = " << x << " and y = " << y << endl;

      if (x + 2 < y)

           w = x + y;

      else

           w = 5 * y;

      return w;

}


TRACE:                                                                                      OUTPUT:


7. Write a complete C++ program, including comments (worth 2 pts--be sure to include a good comment at the top and at least one more good comment later in the program), to do the following: (33 pts)


1. You will process orders for an on-line coffee store. Read in the information on an order, which includes a number that identifies the type of coffee ordered, the number of pounds of coffee ordered, the price per pound of coffee. Print the original data, with identifying messages, right after you read it in. Don't bother to include prompts.


      A typical set of values for an order could be these: 23 5 8.99


This means that the customer ordered 5 pounds of coffee #23, at $8.99 per pound


2. Compute the base price for the order. This is simply the number of pounds of coffee ordered times the cost per pound. Print the base price.


3. Determine the shipping cost. If the number of pounds of coffee ordered is 10 or fewer, the shipping charge is $10; otherwise the shipping charge is $10 plus $1 for each pound over 10 (so 13 pounds would cost $13 shipping.)


4. Determine the order's final price. The final price is the base price for the order plus the shipping charge. Print the shipping charge and print the final price for the order.


5. If the final price of the order is $100 or more, print a message saying "Great choice!" Otherwise, print a message saying “Shop again soon!”


6. Repeat the entire series of calculations for the next order, and the next, until all of the orders have been processed. You must decide how to tell when all orders have been processed. Make sure that this method is described in a comment.


7. At the end, print the total number of orders. Also, print how many customers placed orders worth more than $100. Use two separate counters.


Print all dollar amounts with a $, but don't worry about the decimal places. Send all output to the screen.