C/C++ Programmierung

Aufgabe 6

Schreiben Sie eine Funktion CountIterations, welche die Anzahl der Iterationen der Folge

     zi+1 = zi2, mit c, z komplex und z0 = 0

bis zum Divergieren (hier: ||zi||2 = zrei2 + zimi2 >= 4) als Ergebnis zurückliefert.

Hinweis Beachten Sie, daß ANSI-C keine komplexen Zahlen unterstützt. Verwenden Sie daher folgende Umformung: zrei+1 = zrei * reqi - zimi * zimi + cre und zimi+1 = 2 * zrei * zimi + cim.

Brechen Sie die Auswertung spätestens nach 255 Iterationen ab.

Werten Sie diese Funktion im Bereich von cre = -3.2 ... 3.2 und cim = -2.4 ... 2.4 grafisch aus, indem c auf Bildschirmkoordinaten transformiert wird (xScreen=(x-xMax/2)*0.01, yScreen=(y-yMax/2)*0.01)) und der Farbwert des dort zu setzebdeb Pixels das jeweilige Funktionsergebnis ist.
Verwenden Sie folgendes Programmskelett:

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{int x, y, xMax, yMax;
 int gdriver = DETECT, gmode;
  initgraph(&gdriver, &gmode, "e:\\bc\\bgi"); /* Pfad von egavga.bgi */
  if (graphresult() != grOk)  return 1; / *error */
  xMax = getmaxx(); yMax = getmaxy();
  for /* alle erlaubten x-Koordinaten */
    for /* alle erlaubten y-Koordinaten */
      putpixel( x, y, CountIterations((x-xMax/2)*0.01, (y-yMax/2)*0.01) );
  getch(); closegraph();
 return 0;
}

Lösung für die Aufgabe

/* Header-Datei einbinden */
#include <graphics.h>
#include <stdio.h>
#include <conio.h>

#define MAX_ITERATIONS 255 /* Max. Anzahl der Iterationen */

int CountIterations(double cre, double cim)
{
  double zre, zim, zredummy;
  int i;

  zre = 0; /* Realteil auf 0 setzen */
  zim = 0; /* Imaginaerteil auf 0 setzen */

  for (i = 1; i <= MAX_ITERATIONS; i++) /* Iterationen */
  {
    zredummy = zre; /* Realteil merken, wird spaeter noch benoetigt */
    zre = zre * zre - zim * zim + cre; /* neuen Realteil berechnen */
    zim = 2 * zredummy * zim + cim;  /* neuen Imaginaerteil berechnen */

    if (zre * zre + zim * zim >= 4) /* Abbruchbedingung */
      return(i); /* Anzahl der Iteration zurueckgeben */
  }

  return(MAX_ITERATIONS); /* Max. Anzahl der Iterationen zurueckgeben */
}

int main(void) /* hier beginnt das Hauptprogramm */
{
  int x, y, xMax, yMax;
  int gdriver = DETECT, gmode;

  initgraph(&gdriver, &gmode, "\\tc\\bgi"); /* Pfad von egavga.bgi */
  if (graphresult() != grOk)
    return(1); /* error */

  xMax = getmaxx();
  yMax = getmaxy();

  for (x = 0; x < xMax; x++) /* alle erlaubten x-Koordinaten */
  {
    for (y = 0; y < yMax; y++) /* alle erlaubten y-Koordinaten */
    {
      putpixel(x, y, CountIterations((x - xMax / 2) * 0.01, (y - yMax / 2) * 0.01));
    }
  }

  getch();
  closegraph();

  return(0); /* Das Hauptprogramm gibt am Ende 0 zurück, da kein Fehler aufgetreten ist. */
}


Zurück zur Übersicht