Test 2 CSCI 130 Summer 2003


  1. (6 points) State the syntax and flow of control of the while, for, and if control structures.
  2. (3 points) Show how to rewrite the following as a for loop.
    i = (x+4)/2;
    j = 3*y+7;
    while (j<i+4) {
        body
        j = j + 3;
    } 
  3. (4 points) What will be the result of executing the following code with the input below.
    int a,i;
    float b;
    char c;
    string d;
    
    for(i=0;i<4;i++) {
        cin >> c;
        if (c=='n') {
           cin >> b;
        } else {
           cin >> d;
        }
        cin >> a;
    }
    
    n 12.34 1 yHello4 2n54.89 0 a_very_long_word 3 4 5 6 7 8 9 0 
    
    
  4. (4 points) Describe what happens when each of the following statements are executed
    1. ifstream infile;
    2. infile.open("hello.file");
    3. while (infile) {
    4. infile >> astring;
  5. (10 points) Write a program that will read words from a file and perform the following:
  6. (10 points) Write a program that reads a line from standard input (cin) and prints "The line is a palindrome" if the line is the same backwards and forwards. (For example "123 321" is a palindrome, "abc" is not)
  7. (3 points) Discuss the importance of white space in a C++ program.