C++ Assignment #8
Screen Saver


Repetition (“Loops”)

Programmers talk a lot about loops. This means repetition, doing the same thing over and over again. C++ has three different repetition structures: the for loop, the while loop and the do-while loop. Here are three loops that do the same thing; count from 1 to 10.

//for loop
for(int nNum1 = 1; nNum1 <= 10; nNum1++)
{
     cout<<nNum1<<endl;
}

//do-while loop
int nNum2 = 1;
do
{
     cout<<nNum2<<endl;
     nNum2++;
}while(nNum2 <= 10);

//while loop
int nNum3 = 1;
while(nNum3 <= 10)
{
    cout<<nNum3<<endl;
    nNum3++;
}

You can also use other data types in a loop. The following for loop will display the letters of the alphabet seperated by commas:


for(char cLetter = 'a'; cLetter<='z'; cLetter++)
    cout<<cLetter<<", ";

Notice that you can declare and initialize a variable in a for loop. Also notice that if you are only including one line of code inside the loop, you don't need curly braces.

While all three loops perform the same basic task, they each have their strengths. If you know how many times you want a loop to repeat, then use a for loop. If you don't know how many times it should repeat, then use a while or a do-while. A do-while loop will always execute at least one time, where a while loop may never execute.

Your assignment is to write four programs. Three of the programs will do exactly the same thing, but each program will use one of the c++ repetition structures. You are to use the circle() function from winbgim.h to generate circles of different sizes and colors. The following program produces a series of concentric magenta circles that appear to "grow". The circles have a center at (300, 250) and a radius of nRadius. Try this out with different colors (BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE). Make sure you read the handout on how to make a graphics program using Dev C++. You can use this program as a starting point for your assignment.

#include "winbgim.h"	

void DrawCircles ();

int main ()
{
  initwindow(640, 480);
  DrawCircles();
  getch();
  closegraph();
  return 0; 
}

void DrawCircles ()
{
  setcolor(MAGENTA);
  for (int nRadius = 0; nRadius<=200; nRadius = nRadius + 20)
  {
	delay(1000);			//delay in milliseconds, 1000ms = 1 second
	circle(300,250,nRadius);	//make a circle with center 300,250 and radius nRadius
  }
}

Part One:

You are to write three programs that all produce the same output, but use the three different repetition structures in c++. The programs should make concentric circles in the four corners of the screen, and then have them erased. Have the radii of the circles increase from 0 to 700 by multiples of 10. Have the circles grow and disappear 8 times. If you are clever, you can have the circles cycle through all sixteen colors for a rainbow effect. Submit all three programs to mrsimon@lycos.com.

Part Two:

Design you own pattern of circles and show it to the instructor. Be creative! You may want to have the circles move across the screen, similar to the old Macintosh screen saver "moire". Note: You do not need to submit the code for Part Two.