Intro. to Programming I CS-141 (Test #2 - October 26, 2004)

PLEDGE: I have not violated the honor code on this test. __________________________________

Sign the pledge above and initial every other sheet. Show all your work.
You may omit ONE question but you must indicate which one you want omitted by writing OMIT next to it and by circling the question number. You may do the omitted questions for extra credit.
All questions count the same. If you have trouble with a question, skip it and come back to it later.
You may explain your answers on multiple choice questions if you choose to.
  1. Create a truth table for the expression p and (q or r) to determine what inputs for p, q and r yield a true result and which yield a false result.










  2. Write C++ instructions that will input from the user a temperature in fahrenheit and do the following








  3. Write C++ instructions that will input from the user an integer between 10 and 13 and write out ten, eleven, twelve or thirteen as appropriate. If the user's input is not in the range from 10 to 13 then the program will write "error - input must be an integer from 10 to 13."
    You may NOT use an if statement.










  4. Write a C++ while loop that will add the even integers from 0 to N, keep the running total in a variable called sum and write out sum after the loop ends. For example, if N=6 then the output would be 2+4+6 = 12.









  5. Write a C++ for loop that will do exactly what the while loop above does.








  6. Describe in plain English the output from the following program.
    int main() {
    	char instring[81]; int pos=0;
    	//read in a string of characters
    	cout << "Enter a string of characters." << endl;
    	cin.getline(instring, 80);
    	while (instring[pos] != 0) pos++;
    	cout << pos << endl;
    return 0;
    }//end main
  7. Insert a for loop into the code below so that when the for loop ends the variable result will contain the value of base raised to power. For example if base = 2 and power=3 then result should equal 8.
    int main() {
    	float base; int power; float result=1; int i;
    	cout << "Enter base: "; cin >> base;
    	cout << "Enter power: "; cin >> power;
    //insert a for loop below to compute result = base raised to the power
    
    
    
    
    
    
    
    	cout << "result = " << result << endl;
    return 0;
    }//end main

  8. Explain why (i.e., under what circumstances) a programmer would choose to use a do loop rather than a while or a for loop.







  9. Write C++ instructions that will input from the user an integer from 1 to 12 and then write out the number the user has chosen. The program must not write anything until it has received a valid input. I.e., the program must continue to request input until the user enters an integer from 1 to 12.










  10. What will be the output from the following code segment? _____
    sum=0; n=1;
    while(n != 9) {
        sum = sum + n;
        n = n + 3;
    }
    cout << sum;
    A) 12
    B) 22
    C) 10
    D) 7
    E) nothing - the while loop will never end.
  11. A) Give a declaration for an enumeration named ClassRank that consists of 5 items called Fr, So, Jr, Sr and Other.



    B) Based upon your enum declaration above, What will be the output from the following?
    cout << Fr << So << Jr << Sr << Other << endl;


  12. Write declarations for 3 C++ (new style) strings named city, state and citystate.




  13. Give C++ statements that will do the following
    A) input data from the keyboard into city and state.


    B)Concatenate the strings city and state putting a comma and a space in between (for ex., Lynchburg, VA) and put the resulting string into the string variable named citystate.


    C)Write the length of citystate.
    D)Write the position of the ',' (comma) in citystate.



    E)Extract and write the "state" from citystate.



  14. A) Give a declaration for an old style string variable (array/sequence of characters) called productName to hold the name of a product. productName should be able to hold up to 80 characters.


    B) Write a while loop that will write the contents of productName character by character separating each character with an *. So, for example, if productName is "YooHoo", then the output will be Y*o*o*H*o*o.
    HINTS:
  15. Write each character of the productName array.
  16. Think about how you will detect the end of the string.