Dies ist eine alte Version des Dokuments!


Mögliche Lösungen um Funktionswerte in Tabellen ausgeben zu können

Fortran

table.F90
program main
  implicit none
 
  call tabelle (-20, 20, 1);
end program
 
subroutine tabelle ( start, endw, step )
  implicit none
  integer start, endw, step
  integer k 
 
  if (step .LT. 1) then
    step = 1
  endif
 
  ! Tabellenkopf
  write (*,*) "+-------------------+"
  write (*,*) "|    x    |    y    |"
  write (*,*) "+-------------------+"
 
  ! x-y-Wertepaare ausgeben
  do k=start, endw, step
    write (*, '(1X,2H| ,I7,3H | , I7, 2H |)') k, (2*(k**2) + 4*k + 3)
  enddo
 
  write (*,*) "+-------------------+"
end subroutine

Zusatzaufgabe

table2.F90
program main
  implicit none
  external xsquare
 
  call tabelle (xsquare, -20, 20, 1);
end program
 
integer function xsquare ( x )
  integer x;
  xsquare = x**2;
  return;
end function
 
integer function myfunc ( k )
  implicit none
  integer k
 
  myfunc = (2*(k**2) + 4*k + 3)
end function
 
subroutine tabelle ( f, start, endw, step )
  implicit none
  integer start, endw, step, f
  integer k
 
  if (step .LT. 1) then
    step = 1
  endif
 
  ! Tabellenkopf
  write (*,*) "+-------------------+"
  write (*,*) "|    x    |    y    |"
  write (*,*) "+-------------------+"
 
  ! x-y-Wertepaare ausgeben
  do k=start, endw, step
    write (*, '(1X,2H| ,I7,3H | , I7, 2H |)') k, f(k)
  enddo
 
  write (*,*) "+-------------------+"
end subroutine

C

table.c
#include <stdio.h>
 
void table ( int start, int endw, int step )
{
    printf ("+-------------------+\n");
    printf ("|    x    |    y    |\n");
    printf ("+---------+---------+\n");
 
    if (step < 1)
        step=1;
    for (int i=start; i<=endw; i+=step)
    {
        printf ("| %7d | %7d |\n", i, (2*(i*i)+4*i+3));
    }
    printf ("+---------+---------+\n");
}
 
int main ()
{
    table ( -20, 20, 1);
}

Zusatzaufgabe

table2.c
#include <stdio.h>
 
int xsquare ( int i )
{
    return i*i;
}
 
void table ( int start, int endw, int step, int (*myfunc) (int) )
{
    printf ("+-------------------+\n");
    printf ("|    x    |    y    |\n");
    printf ("+---------+---------+\n");
 
    if (step < 1)
        step=1;
    for (int i=start; i<=endw; i+=step)
    {
        printf ("| %7d | %7d |\n", i, myfunc(i));
    }
    printf ("+---------+---------+\n");
}
 
int main ()
{
    table ( -20, 20, 1, xsquare);
}