CS 2073
Quiz 1
Answers
Last Name:First Name and Initial:

  1. Start with variables wife_age and husband_age. Then fill in code in the program below to do the following:

    1. Give the first variable the value 32.
    2. Give the second variable the value 35.
    3. Increase the values of each of these two variables by 1.
    4. After increasing, find the total age (sum of ages) and print this value, using printf. (Don't add the new ages yourself; your program should add the ages.)

    
    #include <stdio.h>
    
    int main() {
       int wife_age;
       int husband_age;
       int total_age;
       wife_age = 32;
       husband_age = 35;
       wife_age = wife_age + 1;       /* or wife_age++ */
       husband_age = husband_age + 1; /* or husband_age++ */
       total_age = wife_age + husband_age;
       printf("Total age: %i\n", total_age);   
    }
    
    /* Output (1 run):
    Total age: 69
     */
    

  2. Fill in code in the program below, so that it will use scanf to read a value for the variable watertemp. Then use one or more if-else statements (by any method), along with printf statements to print the following:

    1. In case watertemp is less than or equal to 32, print SOLID,
    2. In case watertemp is greater than or equal to 212, print GAS, and
    3. In case watertemp is strictly between 32 and 212, print LIQUID.

    
    #include <stdio.h>
    
    int main() {
       int watertemp;
       scanf("%i", & watertemp);
       /* must have else's below */
       if (watertemp <= 32) printf("SOLID\n");
       else if (watertemp >= 212) printf("GAS\n");
       else printf("LIQUID\n"); 
    }
    
    /* Output (separate runs):
    32
    SOLID
    
    33
    LIQUID
    
    211
    LIQUID
    
    212
    GAS
     */
    

    Other answers:

    
    #include <stdio.h>
    
    int main() {
       int watertemp;
       scanf("%i", & watertemp);
       if (watertemp <= 32) printf("SOLID\n");
       if (watertemp > 32 && watertemp < 212) printf("LIQUID\n");
       if (watertemp >= 212) printf("GAS\n"); 
    }
    
    
    #include <stdio.h>
    
    int main() {
       int watertemp;
       scanf("%i", & watertemp);
       /* optional else's below */
       if (watertemp <= 32) printf("SOLID\n");
       else if (watertemp > 32 && watertemp < 212) printf("LIQUID\n");
       else if (watertemp >= 212) printf("GAS\n"); 
    }