Session 9

Design and Control Structures


CS 1510
Introduction to Computing


Opening Exercise: Growing the Odds

What does this program print?

    for i in range(10):
        print(i+1)

Let's modify this program incrementally to (by demo)...

That third one is complex enough to use some of the ideas you read in Chapter 3 about designing algorithms and developing programs. We need to think about what we have, and what we would like it to be.

One technique is almost always useful: Break the problem into smaller steps. We have a program that prints the even numbers up to a limit.

This program is more complex than many of us want to write from scratch:

    for counter in range(10):
        n = counter + 1
        if n % 2 == 0:
            result = ''
            for i in range(n):
                value = i + 1
                if value % 2 == 0:
                    result += (str(value) + ' ')
            print(result)

But by breaking complex steps down into smaller, more manageable steps, we were able to design a solution. That process also enabled us to grow the program slowly and have a working solution at each point along the way.

With practice, you can do the same.

(Try to think this way on Homework 4.)



Homework 3 Review, Part 1

Debug these programs.

A look at my solution.

Notes.



Homework 3 Review, Part 2

Debug these programs.

A look at my solution.

Notes.

More general notes.

A big one: Do your own work. Sharing. Finding solutions.



Wrap Up



Eugene Wallingford ..... wallingf@cs.uni.edu ..... September 23, 2014