2016-Programmieren in C/ C++

PfN mit mathem. Anwendung

PfN mit mathem. Anwendung


Fichier Détails

Cartes-fiches 21
Langue Deutsch
Catégorie Informatique
Niveau Université
Crée / Actualisé 09.04.2015 / 09.06.2023
Lien de web
https://card2brain.ch/box/z4_mappe
Intégrer
<iframe src="https://card2brain.ch/box/z4_mappe/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

Was fehlt?

______ small_to_large(const void * first, const void * second)
{
    assert(first != NULL);
    assert(second != NULL);
    
    IntUlongPair *a = (IntUlongPair *) first;
    IntUlongPair *b = (IntUlongPair *) second;
    
    if(a->first > b->first)
    {
        return 1;
    }
    else if(a->first < b->first)
    {
        return -1;
    }
    else
    {
        if(a->second < b->second)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
}

void sort_int_ulong_pairs(IntUlongPair *tab, unsigned long len)
{
    assert(tab != NULL);
    
    qsort((void *) tab, len, sizeof(*tab), &small_to_large);
}

int

Was ist richtig?

C++

Wie kann man auf alle Elemente eines Containers zugreifen?

Per Direktzugriff über eckige Klammern kann auf die Elem. zugegriffen werden.

Für den Zugriff auf die Elem. der Container verwendet die STL sog. Iteratoren.
- sind Zeigern ähnlich, aber an ihren Container angepasst
- bilden Glied zwischen Container und Anwendung

Zugriff über die Elementfunktion at() wird geprüft und löst bei Fehlern eine Ausnahme out_of_range aus.

[ ]-Zugriff ist schneller, als at()

Nennen Sie die zwei Präprozessor-Variablen, die für den Dateinamen und für die Zeile in der Datei stehen?

Zwei vordefinierte Makros sind __FILE__ (aktueller Dateiname) und __LINE__ (aktuelle Zeile innerhalb der Datei)

Was wird  'asserted'?

int small_to_large(const void * first, const void * second)
{
    assert(_______);
    assert(_______);
    
    IntUlongPair *a = (IntUlongPair *) first;
    IntUlongPair *b = (IntUlongPair *) second;
    
    if(a->first > b->first)
    {
        return 1;
    }
    else if(a->first < b->first)
    {
        return -1;
    }
    else
    {
        if(a->second < b->second)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
}

void sort_int_ulong_pairs(IntUlongPair *tab, unsigned long len)
{
    assert(tab != NULL);
    
    qsort((void *) tab, len, sizeof(*tab), &small_to_large);
}

Was fehlt?

int small_to_large(const void * first, const void * second)
{
    assert(first != NULL);
    assert(second != NULL);
    
    _______*a = (_______*) first;
    _______*b = (_______*) second;
    
    if(a->first > b->first)
    {
        return 1;
    }
    else if(a->first < b->first)
    {
        return -1;
    }
    else
    {
        if(a->second < b->second)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
}

void sort_int_ulong_pairs(IntUlongPair *tab, unsigned long len)
{
    assert(tab != NULL);
    
    qsort((void *) tab, len, sizeof(*tab), &small_to_large);
}

IntUlongPair

Was ist richtig?

int small_to_large(const void * first, const void * second)
{
    assert(first != NULL);
    assert(second != NULL);
    
    IntUlongPair *a = (IntUlongPair *) first;
    IntUlongPair *b = (IntUlongPair *) second;
    
    if(a->first > b->first)
    {
        return __;
    }
    else if(a->first < b->first)
    {
        return __;
    }
    else
    {
        if(a->second < b->second)
        {
            return __;
        }
        else
        {
            return -1;
        }
    }
}

void sort_int_ulong_pairs(IntUlongPair *tab, unsigned long len)
{
    assert(tab != NULL);
    
    qsort((void *) tab, len, sizeof(*tab), &small_to_large);
}

Was fehlt?

int small_to_large(const void * first, const void * second)
{
    assert(first != NULL);
    assert(second != NULL);
    
    IntUlongPair *a = (IntUlongPair *) first;
    IntUlongPair *b = (IntUlongPair *) second;
    
    if(a->first > b->first)
    {
        return 1;
    }
    else if(a->first < b->first)
    {
        return -1;
    }
    else
    {
        if(a->second < b->second)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
}

void sort_int_ulong_pairs(IntUlongPair *tab, unsigned long len)
{
    assert(tab != NULL);
    
    qsort((void *) tab, len, sizeof(*tab), _________);
}

Der Funktionszeiger '&small_to_large'

Was muss verändert werden?

const unsigned long * logsearch_sm_geq(const unsigned long *leftptr,
                                            const unsigned long *rightptr,
                                            unsigned long key)
  {
1    const unsigned long *midptr;
2    const unsigned long *end = rightptr;
3   
4    while(leftptr <= rightptr)
5    {
6        midptr = leftptr + (unsigned long) (rightptr - leftptr)/2;
7        if(key < *midptr)
8        {
9            rightptr = midptr - 1;
10        }
11        else
12        {
13            if(key > *midptr)
14            {
15               leftptr = midptr + 1;
16            }
17            else
18            {
19                {
20                    return midptr;
22                }
23            }
24        }
25    }
26    return NULL;
    }

Consider a complex number c and the series z0, z1, z2, . . . of complex
numbers, where
z_0 = 0
z_n+1 = z_n *z_n + c
The Mandelbrot set M is the set of all complex numbers c such that

Erkläre gelb Markiertes!

static unsigned long mandelbrot_iter ( const Complex c,
unsigned long nmax )
{
Complex z = c;
unsigned long n;
for (n = 0; n < nmax ; n++)
{
const double a = z.real ,
b = z. imag ;
if (a * a + b * b > 4.0) /* |z| > 2 */
{ /* equiv . to sqrt (a * a + b * b) > 2.0 */
return n;
}

z. real = a * a - b * b + c. real ; /* z = z * z + c */
z. imag = 2.0 * a * b + c. imag ;
}
return nmax ;
}

to save the expensive application of the sqrt-function we replace the
condition \(|z| = {\sqrt{b^2+a^2} > 2}\) by the equivalent condition \({b^2+a^2} >4\)

remember \(z = {a+bi}\)

Erkläre gelb Markiertes!

static unsigned long mandelbrot_iter ( const Complex c,
unsigned long nmax )
{
Complex z = c;
unsigned long n;
for (n = 0; n < nmax ; n++)
{
const double a = z.real ,
b = z. imag ;
if (a * a + b * b > 4.0) /* |z| > 2 */
{ /* equiv . to sqrt (a * a + b * b) > 2.0 */
return n;
}

z. real = a * a - b * b + c. real ; /* z = z * z + c */
z. imag = 2.0 * a * b + c. imag ;
}
return nmax ;
}

computation of \(z*z+c = {a^2-b^2}+2abi+c\) is split on two lines

Was fehlt?

static void estimate_size_of_area ( unsigned long maxsteps )
{
unsigned long steps ;
for ( steps = 1; steps <= maxsteps ; steps *= 10)
{
unsigned long s, inside = 0;
for (s = 0; s < steps ; s++)
{
double a = _______; /* random in range [0 ,1) */
double b = _______; /* random in range [0 ,1) */
if ( inside_area (a,b))
{
inside ++;
}
}
printf (" steps  = %9lu ,  inside  = %9lu ,  ratio  =  %.6f\n",
steps , inside ,( double ) inside / steps );
}

drand48()

Approximation of line integrals

\( {\sum^k_{i=1} \sqrt{d^2 + ( f(start*d*i)-f(start*d*(i-1)))^2}} \)

static double approx_line_length ( double (*f)( double ),
double start , double stop ,
unsigned long k)
{
what's first?
for (i = 1; i <= k; i++)
{
double fx2 = f(x1 + dx), /* x1 + dx = start + d * i */
dy = fx2 - fx1;
total += sqrt ( dx_square + dy * dy );
fx1 = fx2; /* reuse fx2 as fx1 in next iteration */
x1 += dx; /* x1 becomes start + d * i */
}
return total ;
}

Approximation of line integrals

\( {\sum^k_{i=1} \sqrt{d^2 + ( f(start*d*i)-f(start*d*(i-1)))^2}} \)

static double approx_line_length ( double (*f)( double ),
double start , double stop ,
unsigned long k)
{

double total = 0.0 , x1 = start , fx1 = f( start );
const double dx = ( stop - start )/k, dx_square = dx * dx;
unsigned long i;

for (i = 1; i <= k; i++)
{
what now?
}
return total ;
}

Simulation of a game

Was fehlt?

static Score play_single_game ( bool silent , size_t start_cash ,
size_t threshold )
{
Score score ;
______ = start_cash ;
for ( score . throws = 1;
score . cash > 0 && score . cash <= threshold ;
score . throws ++)
{
size_t dice1 , dice2 ;
score .cash --; /* player pays one unit */
dice1 = 1 + drand48 () * 6; /* simulation : roll 1st dice */
dice2 = 1 + drand48 () * 6; /* simulation : roll 2nd dice */
if ( dice1 + dice2 == 12) score . cash += 6;
else
{
if ( dice1 + dice2 >= 8) score . cash += 2;
}
if (! silent )
printf ("%3 lu  throw %c |%*s*\n",score .throws ,
score . throws == 1 ? ’ ’ : ’s’,
(int) score .cash ,"");
/* show * with indendation of width = cash */
}
return score ;
}

score.cash

Simulation of a game

Was fehlt?

static Score play_single_game ( bool silent , size_t start_cash ,
size_t threshold )
{
Score score ;
score . cash = start_cash ;
for ( ________ = 1;
score . cash > 0 && score . cash <= threshold ;
_______++)
{
size_t dice1 , dice2 ;
score .cash --; /* player pays one unit */
dice1 = 1 + drand48 () * 6; /* simulation : roll 1st dice */
dice2 = 1 + drand48 () * 6; /* simulation : roll 2nd dice */
if ( dice1 + dice2 == 12) score . cash += 6;
else
{
if ( dice1 + dice2 >= 8) score . cash += 2;
}
if (! silent )
printf ("%3 lu  throw %c |%*s*\n",score .throws ,
score . throws == 1 ? ’ ’ : ’s’,
(int) score .cash ,"");
/* show * with indendation of width = cash */
}
return score ;
}

score.throws

Simulation of a game

Was fehlt?

static Score play_single_game ( bool silent , size_t start_cash ,
size_t threshold )
{
Score score ;
score . cash = start_cash ;
for ( score . throws = 1;
score . cash > 0 && score . cash <= threshold ;
score . throws ++)
{
size_t dice1 , dice2 ;
score .cash --; /* player pays one unit */
dice1 = 1 + ________ * 6; /* simulation : roll 1st dice */
dice2 = 1 + ________ * 6; /* simulation : roll 2nd dice */
if ( dice1 + dice2 == 12) score . cash += 6;
else
{
if ( dice1 + dice2 >= 8) score . cash += 2;
}
if (! silent )
printf ("%3 lu  throw %c |%*s*\n",score .throws ,
score . throws == 1 ? ’ ’ : ’s’,
(int) score .cash ,"");
/* show * with indendation of width = cash */
}
return score ;
}

drand48()

Simulation of a game

Was fehlt?

static Score play_single_game ( bool silent , size_t start_cash ,
size_t threshold )
{
Score score ;
score . cash = start_cash ;
for ( score . throws = 1;
score . cash > 0 && score . cash <= threshold ;
score . throws ++)
{
size_t dice1 , dice2 ;
score .cash --; /* player pays one unit */
dice1 = 1 + drand48 () * 6; /* simulation : roll 1st dice */
dice2 = 1 + drand48 () * 6; /* simulation : roll 2nd dice */
if ( dice1 + dice2 == 12) score . cash += 6;
else
{
if ( dice1 + dice2 >= 8) score . cash += 2;
}
if (! silent )
printf ("%3 lu  throw %c |%*s*\n",score .throws ,
score . throws == 1 ? ’ ’ : ’s’,
(int) score .cash ,"");
/* show * with indendation of width = cash */
}
______ _______ ;
}

return score

Schreib in C++ ein Funktionstemplate getmin für die Berechnung des Minimums von zwei Werten, deren Typ als Template-Parameter deklariert wird.

template<typename Type>

Type getmin(Type a, Type b) {return a<b?  a : b ;}

Implementieren Sie in C++ eine template-basierte Funktion distance_template‚ die vier Parameter x1, y1, x2, y2 eines unbekannten aber gleichen numerischen Typs erhält. Diese repräsentieren zwei Punkte (x1,y1) und (x2,y2) im Euklidschen Raum. Die genannte Funktion soll die Distanz der beiden Punkte als double-Wert liefern.

template<typename Type>

double distance_template(Type x1, Type y1, Type x2, Type y2) {
double dist = (double) (y1- y2)/ ( x1- x2);
return dist;
}