PG1 WS23 Exercise9

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

PG1 - Exercise 9 and 2 Extra tasks

E 9.1) 50 points

Write a program that calls the functions sumIter() and sumRec() once each. These
functions should output the sum of numbers in a fixed interval [n, m].

Read the start and end values in main().


Write the iterative function sumIter().
Write the recursive function sumRec().

Example of a program run:

Enter the start value: -3


Enter the end value: 10
Sum using iteration: 49
Sum using recursion: 49

E 9.2) 50 points

Write a program that outputs the uppercase alphabet in such a way that the first
and last letters are separated by a tab, followed by the second and second-to-
last letter, and so on:

A Z
B Y

M N

Solve the task using a recursive function. Note that the sequence stops when
reaching the pair "M N".

--------------------------------------------------------------------------------

Extra task: ET1) optional – 50 points

Given is the following program excerpt:

#include <stdio.h>

void upAndDown(int, int);

int main()
{
int nStart = 0, nEnd = 0;
printf("Start value (integer): ");
scanf("%d", &nStart);
printf("End value (> Start value): ");
scanf("%d", &nEnd);
upAndDown(nStart, nEnd);
return 0;
}

Write the missing recursive definition.

Example of a program run:

Start value (integer): 3


End value (> Start value): 8
3 4 5 6 7 8 7 6 5 4 3
Extra task: ET2) optional – 50 points

Given is the following program excerpt:

/*
Calculation of the first x Fibonacci numbers
1. using non-recursive function
2. using recursive function
*/

#include <stdio.h>

int fibNonRec(int);
int fibRec(int);

int main() {
int nNum = 0, i = 0;

printf_s("How many Fibonacci numbers should be output? ");


scanf("%d", &nNum);

printf_s("\nOutput using non-recursive function:\n\n");


do
printf_s("%d ", fibNonRec(i++));
while (i < nNum);

printf("\n\nOutput using recursive function:\n\n");

for (i = 0; i < nNum; i++)


printf_s("%d ", fibRec(i));

puts("");
return 0;
}

Write the missing function and recursion definition. Note that each function
call should calculate only a single Fibonacci number at the specified position
passed as a parameter. For the recursion, first, consider the mathematical
formula.

https://en.wikipedia.org/wiki/Fibonacci_sequence

You might also like