• Keine Ergebnisse gefunden

How did you test the correctness of your code? Save your source code as scanfpositive.cinto the directoryserie05

N/A
N/A
Protected

Academic year: 2021

Aktie "How did you test the correctness of your code? Save your source code as scanfpositive.cinto the directoryserie05"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Dirk Praetorius, Sommersemester 2018

Gregor Gantner 17.04.2018

Ubungen zur Vorlesung¨

Einf¨uhrung in das Programmieren f¨ur TM Serie 5

Aufgabe 5.1. Write a function scanfpositive, which asks the user for a positive number τ > 0 and then returns it. The input request is repeated as long as τ ∈ R is strictly positive, i.e., if the given value satisfies τ ≤ 0, then the input request is repeated. Additionally, write a main program, which calls scanfpositive. How did you test the correctness of your code? Save your source code as scanfpositive.cinto the directoryserie05.

Aufgabe 5.2. Write anon-recursive functionpower, which, given two real numbersx >1 andC >0, determines the smallest integern∈N such thatxn > C. In your implementation, you are not allowed to use the functionlog. Useassertto ensure the conditionsx >1 andC >0. Write a main program, which reads in x and C and prints out the corresponding n. Save your source code as power into the directoryserie05.

Aufgabe 5.3. Write a function lcm, which computes for two given natural numbersa, b∈Nthe least common multiple and returns it. Additionally, write a main program, which reads in the numbersa, b∈N and prints the least common multiple on the screen. How did you test the correctness of your code? Save your source code aslcm.cinto the directoryserie05.

Aufgabe 5.4. The quotient sequence (an+1/an)n∈Ncorresponding to the Fibonacci sequence (an)n∈N, a0:= 1, a1:= 1, an:=an−1+an−2 f¨urn≥2,

converges towards thegolden ratio (1 +√

5)/2. In particular, the difference sequence bn:= an+1

an

− an an−1

converges towards 0. Write anon-recursive functioncauchythat returns, for given k∈N, the smallest n ∈N such that |bn| ≤ 1/k. Moreover, write a main program that reads in k ∈ N and prints out the index n∈N. How did you test the correctness of your code? Save your source code as goldenRatio.c into the directoryserie05.

Aufgabe 5.5. Forx >0, the recursively defined sequence x1:= 1

2(1 +x), xn+1:= 1 2

xn+ x xn

forn≥1 converges towards √

x. Write a nonrecursive function sqrt new which computes for given x > 0 and τ >0 thefirstelementxn of the sequence that satisfies

|xn−xn+1|

|xn| ≤τ or |xn| ≤τ.

Check viaassert, that x >0. Moreover, write a main program which reads in xandτ, computes the approximationxn of√

xand compares it to the exact value, i.e. prints out the absolute error|xn−√ x|.

How did you test the correctness of your code? Save your source code assqrt new.cinto the directory serie05.

Aufgabe 5.6. The cosine function can be represented as a series via cos(x) =

X

k=0

(−1)k x2k (2k)!.

(2)

The correspondingn-th partial sum is given by Cn(x) =

n

X

k=0

(−1)k x2k (2k)!.

Write a nonrecursive functioncos new, which, givenx∈Randτ >0, returns the first value ofCn(x) such that

|Cn(x)−Cn−1(x)|/|Cn(x)| ≤τ or |Cn(x)| ≤τ.

Then, write a main program, which reads x∈R andτ > 0 from the keyboard, calls the function and displays the computed valueCn(x), as well as the value cos(x), the absolute error|Cn(x)−cos(x)|and the relative error|Cn(x)−cos(x)|/|cos(x)|(provided cos(x)6= 0). Write the function in such a way that only one loop is needed and thatx2k and (2k)! are computed in a cheap way. This means, you must not use specific functions to compute the power or the factorial. How did you test the correctness of your code? Save your source code ascos.cinto the directoryserie05.

Aufgabe 5.7. Consider the two series aN :=

N

X

n=0

1

(n+ 1)2 und bN :=

N

X

n=0 n

X

k=0

1

(k+ 1)2(n−k+ 1)2.

Write two functions that, givenN ∈N, measure the time used for the computation of (aN)2 resp.bN. What is the computational complexity (Aufwand) for the computation of (aN)2resp.bN? For instance:

if the functions run 3 seconds forN = 103, how long does the computation take for in case ofN= 104? Write a main program that, for different values of N, prints out the results tabularly. Do the results meet your expectations? How have you tested your implementation? Save your source code astiming.c into the directoryserie05.

Aufgabe 5.8. Which types of comments do you know? What is the output of the following code and why?

#include <stdio.h>

/*int f(double x) { return (int) x;

}

*/

main() { int x = 4;

int y = 2*x*/* f(0.1)+3

*/1/4;

// y = 1;

printf("y = %d\n",y); // Print out result }

Referenzen

ÄHNLICHE DOKUMENTE

Mit Hilfe des DRG-Browsers 2006 und den darin enthaltenen Kostendaten, die nach Kostenstellen und Kostenarten aufgeteilt sind, lässt sich mit den DRG-Fallzahlen des eigenen

In contrast to the usual indexing in C (e.g., the indexing considered in the lecture), let the indices for the matrix entries a jk of your structure SquareMatrix go from j, k = 1 to

Write a function int anagram(char* firstStr, char* secondStr) which checks if a given word is an anagram of a second given word.. An anagram of a word is a letter sequence which

Write a main program, which reads the vector x and its length n from the keyboard, sorts it with mergesort and prints to the screen the sorted vector.. Test your

Check via assert if b ≤ c Write a main program which reads in the bounds and calls the function armstrong.. How did you test the correctness of

Additionally, write a main program that reads in the number x, then calls the function divisor, and prints out the result.. Save your source code as teiler.c into the

Assume that an average of 4 marked exercises per EPROG exercise class yields 22.5 points and an average of 7 yields 37.5 points for the computation of your final grade (without

The secondary burial at Qatna marks a modification of the primary burial, accompanied by a series of regular rites that took place after the dissolution of the corpse w h e n