06/10/2015

Planning Programs

Planning Programs is an important skill to develop. Consider the problem below.

Problem: A glass of warm coke contains 50 bacteria. At the end of each hour the number of bacteria in the glass is double the number that was in the glass at the beginning of the hour. Write a program to find the number of hours that must go by until the glass contains more than 500000 bacteria.

We need to figure out what variables we need, and what the program will do to the variables.

Variables. We need to deal with time (number of hours) and with the population count of the bacteria. That looks like two variables. Both numbers are whole numbers so we can write:

int hours;
int pop;  
Initial Values. Before worrying about how the program might change variables hours and pop, let's think about their starting values. At the beginning no time has gone by and the problem says that there 50 bacteria in the glass. So we can write:
int hours = 0;
int pop = 50;  
So this is the beginning of the program:
#include<stdio.h>
int main() {

  int hours = 0;
  int pop = 50;  

Final Values. What do we want the program to tell us? We want to know the number of hours until the population goes over 500000. So we also have the end of the program: a print statement:

#include<stdio.h>
int main() {

  int hours = 0;
  int pop = 50;  

  .      //to be filled in see below
  .      //here the program keeps track of changes to hours and pop
  .      //and stops when the pop is finally greater than 500000

  printf("%d\n", hours);
}

Variable Updates. Now let's think about what the program should do to the variables. The problem says, "At the end of each hour the number of bacteria in the glass is double the number that was in the glass at the beginning of the hour".

If an hour has gone by, the value in hours should be one larger and the quote says that the population has doubled. We write:

    hours += 1;
    pop *= 2;  //* is multiplication

Stopping The Repetitions. We want to stop the repetitions of the variable updates when the population is finally above 500000. But we don't have a keyword "when" What we have is a keyword "while" So we write:

  while ( ????) {
    hours += 1;
    pop *= 2;  //* is multiplication
  }

We need to figure out what goes inside the parentheses. But first let's talk about the braces { and }. The while applies to whatever single statement comes after the parentheses. But we need to repeat both variable updates. So we enclose the updates in the braces because that makes them into a single compound statement. The computer executes a compound statement by executing each statement inside it from top to bottom.

The Parentheses. we want to stop when the population is greater than 500000.

    pop>500000
So in terms of the while, we want to continue the repetitions so long as the population is NOT greater than 500000. So if the population is not greater than 500000, it must be less than or equal to 500000:
    while (pop<=500000)
Complete Program.
#include<stdio.h>
int main() {

  int hours = 0;
  int pop = 50;  

  while (pop<=500000) {
    hours += 1;
    pop *= 2;  
  }

  printf("%d\n", hours);
}

Problem: Blue-green-violet light is gradually absorbed by sea water (unlike red-orange-yellow light that is rapidly absorbed). At the end of each meter of depth, the intensity of blue-green-violet light is 97% (.97) of what the intensity was at the beginning of the meter. Write a program to find the depth (number of meters) that blue-green-violet light must go through until its intensity less than 1% of the intensity at the water's surface. The name of the program file will be pp1.c.

Programs 02