Topical Information

Here are a few questions to help you clarify your understanding of logical grouping.

(i.e. When does one task end and another begin? Or: How much is too much for one function to do?)

Question Set Information

Questions

  1. Given the following fragment of code, indicate logical groups of statements. (Hint: You might have to break some continued statements into pieces to make them more logical.)

        double fahren, celcius;
        cout << "Welcome to the Temperature Converter\n\n"
             << "Enter your temperature in Fahrenheit:  ";
        cin >> fahren;
        celcius = (fahren-32)/9*5;
        cout << "A temperature of " << fahren
             << "F would be the same as " << celcius
             << " degrees Celcius!\n\n"
             << "Thanks for using our products!\n\n"
             << "Have a terrific day!" << endl;
    
    
            Blank lines added between logical segments of the code:
    
                double fahren, celcius;
    
                cout << "Welcome to the Temperature Converter\n\n"
    
                     << "Enter your temperature in Fahrenheit:  ";
                cin >> fahren;
    
                celcius = (fahren-32)/9*5;
    
                cout << "A temperature of " << fahren
                     << "F would be the same as " << celcius
                     << " degrees Celcius!\n\n"
    
                     << "Thanks for using our products!\n\n"
                     << "Have a terrific day!" << endl;
    
    
  2. Given the following algorithm fragment, denote any logical groups of [pseudo]statements. Also note whether you see any groups which occur more than once.

    (You may have to insert certain assumed steps for good grouping.)

        prompt:  enter first data
        gather data for point one:  read time, read temp, read location
        prompt:  enter second data
        gather data for point two:  read time, read temp, read location
        prompt:  enter third data
        gather data for point three:  read time, read temp, read location
        # ...continue until the time is negative — indicates user is done
        calculate statistics:  average, standard deviation, min/max of temps
                               std dev of times 'distance' from min time
        print stats and summarize number (and names) of distinct locations
    
    
            Blank lines added between logical segments of the code:
    
                prompt:  enter first data
                gather data for point one:  read time, read temp, read location
    
                prompt:  enter second data
                gather data for point two:  read time, read temp, read location
    
                prompt:  enter third data
                gather data for point three:  read time, read temp, read location
    
                # ...continue until the time is negative — indicates user is done
    
                calculate statistics:  average, standard deviation, min/max of temps
                                       std dev of times 'distance' from min time
    
                print stats and summarize number (and names) of distinct locations
    
            The first group is repeated three times explicitly (and many times
            thereafter via the implied loop).
    
    
  3. Given the following algorithm, find any logical 'groups'. If any occur multiple times, so note.

        gather point info:  read x, read y, read z
        gather point info:  read x, read y, read z
        find x midpoint:  average of x's
        find y midpoint:  average of y's
        find z midpoint:  average of z's
        print first point, second point, and midpoint
    
    
            Blank lines added between logical segments of the code:
    
                gather point info:  read x, read y, read z
    
                gather point info:  read x, read y, read z
    
                find x midpoint:  average of x's
    
                find y midpoint:  average of y's
    
                find z midpoint:  average of z's
    
                print first point,
    
                      second point, and
    
                      midpoint
    
            The first group is repeated twice.  The second group is repeated three
            times.  And the third group is also repeated three times.
    
    
  4. Given the following set of data, break it into logical groups. Make sure to explain your reasoning!

        'T', 'g', '6', '$', 'h', 'u', 'Z', '*', ']', '\'', '\n',
        '5', '\t', ' ', '\a', '%', 'U'
    
    
            Letters (upper):  'T', 'Z', 'U'
    
            Letters (lower):  'g', 'h', 'u'
    
            Digits:           '6', '5'
    
            Punctuation:      '$', '*', ']', '\'', '%'
    
            Spacing:          '\n', '\t', ' '
    
            Other:            '\a'
    
    
  5. Break the following data into logical groups. Explain your reasons carefully!

        4, 5, -9, -2, 49, 81, 8, -25
    
    
                Positive:   4, 5, 49, 81, 8
    
                Negative:   -9, -2, -25
    
            OR...
    
                Absolute squares:  4, -9, 49, 81, -25
    
                Cubes:             8
    
                Absolute primes:   5, -2
    
            OR...
    
                Single digits:   4, 5, -9, -2, 8
    
                Double digits:   49, 81, -25